liujie
2026-01-24 13392606c16362d23478e84e0d43e869813d3095
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package com.estate.expert.admin.utils.tree;
 
import com.estate.expert.admin.enums.BizResultCode;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
 
import javax.persistence.Transient;
import java.util.*;
 
@ApiModel(value = "树节点")
@Data
public class TreeNode<T> {
 
    @ApiModelProperty(value = "节点id,在同一层级下唯一")
    private Integer id = 0;
 
    @ApiModelProperty(value = "节点名称,同一层级下唯一")
    private String name;
 
    @ApiModelProperty(value = "子节点Id计数器,自增")
    private int childIdCnt = 0;
 
    @ApiModelProperty(value = "节点中的数据")
    private T data;
 
    @ApiModelProperty(value = "节点的路径,全局唯一")
    private String path;
 
    @Transient
    @JsonIgnore
    private TreeNode<T> parent;
 
    @ApiModelProperty(value = "子节点")
    public SortedMap<Integer, TreeNode<T>> children = new TreeMap<>();
 
    public TreeNode(){
    }
 
    public TreeNode(String name) {
        this.name = name;
        this.id = 0;
    }
 
    public TreeNode(String name, T data) {
        this.name = name;
        this.data = data;
        this.id = 0;
    }
 
    public TreeNode<T> ShadowNode() {
        TreeNode<T> shadow = new TreeNode<>(this.getName(), this.getData());
        shadow.setId(this.getId());
        shadow.setPath(this.getPath());
        shadow.setData(this.getData());
        shadow.setName(this.getName());
        return shadow;
    }
 
    public TreeNode<T> addChild(TreeNode<T> child) {
        children.put(child.getId(), child);
        return child;
    }
 
    public TreeNode<T> addChild(String name) {
        return addChild(name, null);
    }
 
    public TreeNode<T> addChild(String name, T data) {
        for(TreeNode<T> child : children.values()) {
            BizResultCode.HIERARCHY_NODE_NAME_ALREADY_EXISTS.check(child.getName().equals(name));
        }
 
        TreeNode<T> child = new TreeNode<>(name, data);
        child.setId(childIdCnt++);
        child.setName(name);
        child.setData(data);
        child.setParent(this);
        this.children.put(child.getId(), child);
        return child;
    }
 
    public void removeChild(Integer id) {
        children.remove(id);
    }
 
    /**
     * 获取该节点下所有的路径
     * 以及所在分支的路径
     */
    public List<String> allPaths(){
        var list = new ArrayList<String>();
        for (var child : children.values()){
            list.addAll(child.allPaths());
        }
        list.add(getPath());
 
        var parent = getParent();
        while (parent != null){
            list.add(parent.getPath());
            parent = parent.getParent();
        }
        return list;
    }
 
    /**
     * 递归查找节点
     * 这里,由于树的根节点是单一的
     * 根节点的path= "/"
     */
    public void buildPath() {
        StringBuilder builder = new StringBuilder();
        TreeNode<T> node = this;
        while (true) {
            var parent = node.getParent();
            if(parent == null) {
                break;
            }
 
            builder.insert(0, node.getId());
            builder.insert(0, "/");
            node = parent;
        }
        if(builder.isEmpty()) {
            path = "/";
            return;
        }
        path = builder.toString();
    }
 
    @JsonIgnore
    public String getNamePath(){
        StringBuilder builder = new StringBuilder();
        TreeNode<T> node = this;
        while (true) {
            var parent = node.getParent();
            if(parent == null)
                break;
 
            builder.insert(0, node.getName());
            builder.insert(0, "/");
            node = parent;
        }
 
        if(builder.isEmpty())
            return "/";
        return builder.toString();
    }
 
    @JsonIgnore
    public List<String> getNames(){
        List<String> names = new ArrayList<>();
        TreeNode<T> node = this;
        while (true) {
            names.add(node.getName());
            var parent = node.getParent();
            if(parent == null)
                break;
 
            node = parent;
        }
        return names;
    }
 
    @JsonIgnore
    public int getDepth(){
        // 根节点的深度为0
        int depth = 0;
        TreeNode<T> node = this;
        while (true) {
            var parent = node.getParent();
            if(parent == null)
                break;
 
            depth++;
            node = parent;
        }
        return depth;
    }
 
    @JsonIgnore
    public List<TreeNode<T>> getLeafNodes() {
        List<TreeNode<T>> leafNodes = new ArrayList<>();
        getLeafNodes(this, leafNodes);
        return leafNodes;
    }
 
    private void getLeafNodes(TreeNode<T> node, List<TreeNode<T>> leafNodes) {
        if (node.getChildren().isEmpty()) {
            leafNodes.add(node);
        } else {
            for (TreeNode<T> child : node.getChildren().values()) {
                getLeafNodes(child, leafNodes);
            }
        }
    }
 
    @Override
    public String toString() {
        return "TreeNode{" +
                "path=" + path +
                ", name='" + name +
                "', data=" + data + "}" ;
    }
}