Java???????????
???????????? ???????[ 2016/8/26 11:32:59 ] ??????????????????? 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<TreeNode> 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<TreeNode>();
????45 }
????46
????47 public boolean isValidTree() {
????48 return true;
????49 }
????50
????51 /* ?????????????????? */
????52 public List<TreeNode> getElders() {
????53 List<TreeNode> elderList = new ArrayList<TreeNode>();
????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<TreeNode> getJuniors() {
????66 List<TreeNode> juniorList = new ArrayList<TreeNode>();
????67 List<TreeNode> 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<TreeNode> 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<TreeNode> 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<TreeNode> 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<TreeNode> 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 }
??????
![](/images/ad-banner/ad-banner.png)
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11