您的位置:首页 > 其它

树结构中的节点增删改操作后,排序码的重新设置

2017-12-06 14:26 316 查看
package com.naydog.sort.bysortkey;

import java.util.ArrayList;
import java.util.List;

/**
* 目的:树结构中一个节点下的子节点非自然排序,而是按照排序码。当发生节点增删,或位置移动时,需要重新设置排序码
* 此为重新设置排序码的算法实现
*/
public class SortBySortKey {

/**
* 步长,相邻节点之间排序码相差10,即排序码是0,10,20,30...,而非0,1,2,3...
*/
private int SORT_STEP = 10;

/**
*
* @param id
* @param newPid 新的父节点
* @param position 插入的位置
*/
public void update(String id, String newPid, int position) {
// 从repository获得对象
E entity = null;//repository.findById(id);

String oldPid = entity.getPid();

entity.setPid(newPid);
entity.setSortKey(position * SORT_STEP - 1); // 这里设置为步长的整数倍少一点很重要,在后面排序的时候可少传一些参数,少一些判断

// 保存到repository
// repository.save(entity);

// 重新设置排序码
if (!oldPid.equals(newPid)) { // 换了父节点,原先所在父节点也需要重排序
sortBySortKey(oldPid);
}
sortBySortKey(newPid);
}

/**
* 重新设置某一节点下子节点的排序码。分四种情况:
* 原排序码:
* 0, 10, 20, 30, 40
* 插入后待排序的排序码(插入到第三个  +19):
* 0, 10, 19, 20, 30, 40    直接重新按步长设置排序码
* 移除后待排序的排序码(移除第三个 -20):
* 0, 10, 30, 40                      直接重新按步长设置排序码
* 从右往左移后待排序的排序码(第四个移到第二个 -30 +9):
* 0, 9, 10, 20, 40               直接重新按步长设置排序码
* 从左往右移动后待排序的排序码 (第二个移到第四个 -10 +29):
* 0, 20, 29, 30, 40     29和30应该交换顺序,然后按步长设置排序码
*
* @param pid
*/
public void sortBySortKey(String pid) {
// 某父节点下所有子节点(包括新插入的节点)
List<E> siblings = null; //repository.findByPid(pid);

// 只需要对增加节点之后的节点重新设置排序码
int unmatch = 0;
boolean flag = false;
List<E> needsSortList = new ArrayList<>();
for (int i = 0; i < siblings.size(); i++) {
E entity = siblings.get(i);
if (entity.getSortKey() != i * SORT_STEP) { // 排序码和索引不一致,需要处理
unmatch++;
// 若第一个不匹配的数大于预期排序码(i*步长)则说明是删除或者是从左向右移动节点,标记之
if (unmatch == 1 && entity.getSortKey() > i * SORT_STEP) {
flag = true;
}
// 当设置了flag后,发现不能被步长整除的节点则表示当前为向右移的节点,此时要同时更新i与i+1 节点
if (flag
&& (entity.getSortKey()) % SORT_STEP != 0 //当前为插入/修改位置的节点
&& i < siblings.size() - 1) { // 后面还有节点
entity.setSortKey((i + 1) * SORT_STEP); // 两者要交换顺序
needsSortList.add(entity);
E tmpCtg2 = siblings.get(i + 1);
tmpCtg2.setSortKey(i * SORT_STEP);  // 两者要交换顺序
needsSortList.add(tmpCtg2);
unmatch++;
i++;
} else { // 其余情况直接修改为步长的整数倍即可
entity.setSortKey(i * SORT_STEP);
needsSortList.add(entity);
}
}
}

// 更新
//repository.saveAll(needsSortList);
}

public static void main(String[] args) {

}

}

/**
* 节点
*/
class E {
private String id;
private String pid;
private int sortKey;

public E(String id, String pid, int sortKey) {
this.id = id;
this.pid = pid;
this.sortKey = sortKey;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public int getSortKey() {
return sortKey;
}
public void setSortKey(int
4000
sortKey) {
this.sortKey = sortKey;
}
@Override
public String toString() {
return "E [id=" + id + ", pid=" + pid + ", sortKey=" + sortKey + "]";
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 结构
相关文章推荐