java树的查找_Java实现多叉树查找

1 package tree;

2

3 import java.util.List;

4 import java.util.ArrayList;

5 import java.io.Serializable;

6

7 public class TreeNode implements Serializable {

8     private int parentId;

9     private int selfId;

10     protected String nodeName;

11     protected Object obj;

12     protected TreeNode parentNode;

13     protected List childList;

14

15     public TreeNode() {

16         initChildList();

17     }

18

19     public TreeNode(TreeNode parentNode) {

20         this.getParentNode();

21         initChildList();

22     }

23

24     public boolean isLeaf() {

25         if (childList == null) {

26             return true;

27         } else {

28             if (childList.isEmpty()) {

29                 return true;

30             } else {

31                 return false;

32             }

33         }

34     }

35

36     /* 插入一个child节点到当前节点中 */

37     public void addChildNode(TreeNode treeNode) {

38         initChildList();

39         childList.add(treeNode);

40     }

41

42     public void initChildList() {

43         if (childList == null)

44             childList = new ArrayList();

45     }

46

47     public boolean isValidTree() {

48         return true;

49     }

50

51     /* 返回当前节点的父辈节点集合 */

52     public List getElders() {

53         List elderList = new ArrayList();

54         TreeNode parentNode = this.getParentNode();

55         if (parentNode == null) {

56             return elderList;

57         } else {

58             elderList.add(parentNode);

59             elderList.addAll(parentNode.getElders());

60             return elderList;

61         }

62     }

63

64     /* 返回当前节点的晚辈集合 */

65     public List getJuniors() {

66         List juniorList = new ArrayList();

67         List childList = this.getChildList();

68         if (childList == null) {

69             return juniorList;

70         } else {

71             int childNumber = childList.size();

72             for (int i = 0; i < childNumber; i++) {

73                 TreeNode junior = childList.get(i);

74                 juniorList.add(junior);

75                 juniorList.addAll(junior.getJuniors());

76             }

77             return juniorList;

78         }

79     }

80

81     /* 返回当前节点的孩子集合 */

82     public List getChildList() {

83         return childList;

84     }

85

86     /* 删除节点和它下面的晚辈 */

87     public void deleteNode() {

88         TreeNode parentNode = this.getParentNode();

89         int id = this.getSelfId();

90

91         if (parentNode != null) {

92             parentNode.deleteChildNode(id);

93         }

94     }

95

96     /* 删除当前节点的某个子节点 */

97     public void deleteChildNode(int childId) {

98         List childList = this.getChildList();

99         int childNumber = childList.size();

100         for (int i = 0; i < childNumber; i++) {

101             TreeNode child = childList.get(i);

102             if (child.getSelfId() == childId) {

103                 childList.remove(i);

104                 return;

105             }

106         }

107     }

108

109     /* 动态的插入一个新的节点到当前树中 */

110     public boolean insertJuniorNode(TreeNode treeNode) {

111         int juniorParentId = treeNode.getParentId();

112         if (this.parentId == juniorParentId) {

113             addChildNode(treeNode);

114             return true;

115         } else {

116             List childList = this.getChildList();

117             int childNumber = childList.size();

118             boolean insertFlag;

119

120             for (int i = 0; i < childNumber; i++) {

121                 TreeNode childNode = childList.get(i);

122                 insertFlag = childNode.insertJuniorNode(treeNode);

123                 if (insertFlag == true)

124                     return true;

125             }

126             return false;

127         }

128     }

129

130     /* 找到一颗树中某个节点 */

131     public TreeNode findTreeNodeById(int id) {

132         if (this.selfId == id)

133             return this;

134         if (childList.isEmpty() || childList == null) {

135             return null;

136         } else {

137             int childNumber = childList.size();

138             for (int i = 0; i < childNumber; i++) {

139                 TreeNode child = childList.get(i);

140                 TreeNode resultNode = child.findTreeNodeById(id);

141                 if (resultNode != null) {

142                     return resultNode;

143                 }

144             }

145             return null;

146         }

147     }

148

149     /* 遍历一棵树,层次遍历 */

150     public void traverse() {

151         if (selfId < 0)

152             return;

153         print(this.selfId);

154         if (childList == null || childList.isEmpty())

155             return;

156         int childNumber = childList.size();

157         for (int i = 0; i < childNumber; i++) {

158             TreeNode child = childList.get(i);

159             child.traverse();

160         }

161     }

162

163     public void print(String content) {

164         System.out.println(content);

165     }

166

167     public void print(int content) {

168         System.out.println(String.valueOf(content));

169     }

170

171     public void setChildList(List childList) {

172         this.childList = childList;

173     }

174

175     public int getParentId() {

176         return parentId;

177     }

178

179     public void setParentId(int parentId) {

180         this.parentId = parentId;

181     }

182

183     public int getSelfId() {

184         return selfId;

185     }

186

187     public void setSelfId(int selfId) {

188         this.selfId = selfId;

189     }

190

191     public TreeNode getParentNode() {

192         return parentNode;

193     }

194

195     public void setParentNode(TreeNode parentNode) {

196         this.parentNode = parentNode;

197     }

198

199     public String getNodeName() {

200         return nodeName;

201     }

202

203     public void setNodeName(String nodeName) {

204         this.nodeName = nodeName;

205     }

206

207     public Object getObj() {

208         return obj;

209     }

210

211     public void setObj(Object obj) {

212         this.obj = obj;

213     }

214 }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/** * 根据等级查询类目 * * @param level * @return */ @Override public List queryCategoryTree(Integer level) { //查询当前级别下类目 List list = categoryDAO.list(level); //组装好的类目,返回前端 List categoryTree = new ArrayList(); //所有类目 List allDTOList = new ArrayList(); if (CollectionUtils.isEmpty(list)) { return categoryTree; } for (CategoryDO categoryDO : list) { allDTOList.add(new CategoryTreeDTO().convertDOToDTO(categoryDO)); } //当前等级类目 categoryTree = allDTOList.stream().filter(dto -> level.equals(dto.getLevel())).collect(Collectors.toList()); for (CategoryTreeDTO categoryTreeDTO : categoryTree) { //组装类目为结构 assembleTree(categoryTreeDTO, allDTOList,Constants.CATEGORY_MAX_LEVEL - level); } return categoryTree; } /** * 组装 * * @param categoryTreeDTO * @param allList * @param remainRecursionCount 剩余递归次数 * @return */ public CategoryTreeDTO assembleTree(CategoryTreeDTO categoryTreeDTO, List allList, int remainRecursionCount) { remainRecursionCount--; //最大递归次数不超过Constants.CATEGORY_MAX_LEVEL-level次,防止坏数据死循环 if(remainRecursionCount < 0){ return categoryTreeDTO; } String categoryCode = categoryTreeDTO.getCategoryCode(); Integer level = categoryTreeDTO.getLevel(); //到达最后等级返回 if (Constants.CATEGORY_MAX_LEVEL == level) { return categoryTreeDTO; } //子类目 List child = allList.stream().filter(a -> categoryCode.equals(a.getParentCode())).collect(Collectors.toList()); if (null == child) { return categoryTreeDTO; } categoryTreeDTO.setChildren(child); //组装子类目 for (CategoryTreeDTO dto : child) { assembleTree(dto, allList,remainRecursionCount); } return categoryTreeDTO; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值