1 packagetree;2
3 importjava.util.List;4 importjava.util.ArrayList;5 importjava.io.Serializable;6
7 public class TreeNode implementsSerializable {8 private intparentId;9 private intselfId;10 protectedString nodeName;11 protectedObject obj;12 protectedTreeNode parentNode;13 protected ListchildList;14
15 publicTreeNode() {16 initChildList();17 }18
19 publicTreeNode(TreeNode parentNode) {20 this.getParentNode();21 initChildList();22 }23
24 public booleanisLeaf() {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 voidaddChildNode(TreeNode treeNode) {38 initChildList();39 childList.add(treeNode);40 }41
42 public voidinitChildList() {43 if (childList == null)44 childList = new ArrayList();45 }46
47 public booleanisValidTree() {48 return true;49 }50
51 /*返回当前节点的父辈节点集合*/
52 public ListgetElders() {53 List elderList = new ArrayList();54 TreeNode parentNode = this.getParentNode();55 if (parentNode == null) {56 returnelderList;57 } else{58 elderList.add(parentNode);59 elderList.addAll(parentNode.getElders());60 returnelderList;61 }62 }63
64 /*返回当前节点的晚辈集合*/
65 public ListgetJuniors() {66 List juniorList = new ArrayList();67 List childList = this.getChildList();68 if (childList == null) {69 returnjuniorList;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 returnjuniorList;78 }79 }80
81 /*返回当前节点的孩子集合*/
82 public ListgetChildList() {83 returnchildList;84 }85
86 /*删除节点和它下面的晚辈*/
87 public voiddeleteNode() {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(intchildId) {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 booleaninsertJuniorNode(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 booleaninsertFlag;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(intid) {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 returnresultNode;143 }144 }145 return null;146 }147 }148
149 /*遍历一棵树,层次遍历*/
150 public voidtraverse() {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 voidprint(String content) {164 System.out.println(content);165 }166
167 public void print(intcontent) {168 System.out.println(String.valueOf(content));169 }170
171 public void setChildList(ListchildList) {172 this.childList =childList;173 }174
175 public intgetParentId() {176 returnparentId;177 }178
179 public void setParentId(intparentId) {180 this.parentId =parentId;181 }182
183 public intgetSelfId() {184 returnselfId;185 }186
187 public void setSelfId(intselfId) {188 this.selfId =selfId;189 }190
191 publicTreeNode getParentNode() {192 returnparentNode;193 }194
195 public voidsetParentNode(TreeNode parentNode) {196 this.parentNode =parentNode;197 }198
199 publicString getNodeName() {200 returnnodeName;201 }202
203 public voidsetNodeName(String nodeName) {204 this.nodeName =nodeName;205 }206
207 publicObject getObj() {208 returnobj;209 }210
211 public voidsetObj(Object obj) {212 this.obj =obj;213 }214 }