Java 多叉树的实现,完成树的初始化和遍历

来源:互联网 发布:mac safari 插件管理 编辑:程序博客网 时间:2024/05/16 01:31
Java 多叉树的实现,完成树的初始化和遍历。包括两个文件(TreeNode.java和TreeHelper.java) 
TreeNode类完成树节点的数据结构,TreeHelper类通过输入一个TreeNode列表,生成一颗有一个树根的树!其它函数接口自己看看就明白了,希望对你有帮助。 

一:树节点的定义(TreeNode.java) 
Java代码  收藏代码
  1. package com.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. }  


二:TreeHelper.java 
Java代码  收藏代码
  1. package com.tree;  
  2.   
  3. import java.util.List;  
  4. import java.util.Iterator;  
  5. import java.util.ArrayList;  
  6. import java.util.HashMap;  
  7.   
  8. public class TreeHelper {  
  9.   
  10.     private TreeNode root;  
  11.     private List<TreeNode> tempNodeList;  
  12.     private boolean isValidTree = true;  
  13.   
  14.     public TreeHelper() {  
  15.     }  
  16.   
  17.     public TreeHelper(List<TreeNode> treeNodeList) {  
  18.         tempNodeList = treeNodeList;  
  19.         generateTree();  
  20.     }  
  21.   
  22.     public static TreeNode getTreeNodeById(TreeNode tree, int id) {  
  23.         if (tree == null)  
  24.             return null;  
  25.         TreeNode treeNode = tree.findTreeNodeById(id);  
  26.         return treeNode;  
  27.     }  
  28.   
  29.     /** generate a tree from the given treeNode or entity list */  
  30.     public void generateTree() {  
  31.         HashMap nodeMap = putNodesIntoMap();  
  32.         putChildIntoParent(nodeMap);  
  33.     }  
  34.   
  35.     /** 
  36.      * put all the treeNodes into a hash table by its id as the key 
  37.      *  
  38.      * @return hashmap that contains the treenodes 
  39.      */  
  40.     protected HashMap putNodesIntoMap() {  
  41.         int maxId = Integer.MAX_VALUE;  
  42.         HashMap nodeMap = new HashMap<String, TreeNode>();  
  43.         Iterator it = tempNodeList.iterator();  
  44.         while (it.hasNext()) {  
  45.             TreeNode treeNode = (TreeNode) it.next();  
  46.             int id = treeNode.getSelfId();  
  47.             if (id < maxId) {  
  48.                 maxId = id;  
  49.                 this.root = treeNode;  
  50.             }  
  51.             String keyId = String.valueOf(id);  
  52.   
  53.             nodeMap.put(keyId, treeNode);  
  54.             // System.out.println("keyId: " +keyId);  
  55.         }  
  56.         return nodeMap;  
  57.     }  
  58.   
  59.     /** 
  60.      * set the parent nodes point to the child nodes 
  61.      *  
  62.      * @param nodeMap 
  63.      *            a hashmap that contains all the treenodes by its id as the key 
  64.      */  
  65.     protected void putChildIntoParent(HashMap nodeMap) {  
  66.         Iterator it = nodeMap.values().iterator();  
  67.         while (it.hasNext()) {  
  68.             TreeNode treeNode = (TreeNode) it.next();  
  69.             int parentId = treeNode.getParentId();  
  70.             String parentKeyId = String.valueOf(parentId);  
  71.             if (nodeMap.containsKey(parentKeyId)) {  
  72.                 TreeNode parentNode = (TreeNode) nodeMap.get(parentKeyId);  
  73.                 if (parentNode == null) {  
  74.                     this.isValidTree = false;  
  75.                     return;  
  76.                 } else {  
  77.                     parentNode.addChildNode(treeNode);  
  78.                     // System.out.println("childId: " +treeNode.getSelfId()+" parentId: "+parentNode.getSelfId());  
  79.                 }  
  80.             }  
  81.         }  
  82.     }  
  83.   
  84.     /** initialize the tempNodeList property */  
  85.     protected void initTempNodeList() {  
  86.         if (this.tempNodeList == null) {  
  87.             this.tempNodeList = new ArrayList<TreeNode>();  
  88.         }  
  89.     }  
  90.   
  91.     /** add a tree node to the tempNodeList */  
  92.     public void addTreeNode(TreeNode treeNode) {  
  93.         initTempNodeList();  
  94.         this.tempNodeList.add(treeNode);  
  95.     }  
  96.   
  97.     /** 
  98.      * insert a tree node to the tree generated already 
  99.      *  
  100.      * @return show the insert operation is ok or not 
  101.      */  
  102.     public boolean insertTreeNode(TreeNode treeNode) {  
  103.         boolean insertFlag = root.insertJuniorNode(treeNode);  
  104.         return insertFlag;  
  105.     }  
  106.   
  107.     /** 
  108.      * adapt the entities to the corresponding treeNode 
  109.      *  
  110.      * @param entityList 
  111.      *            list that contains the entities 
  112.      *@return the list containg the corresponding treeNodes of the entities 
  113.      */  
  114.     public static List<TreeNode> changeEnititiesToTreeNodes(List entityList) {  
  115.         OrganizationEntity orgEntity = new OrganizationEntity();  
  116.         List<TreeNode> tempNodeList = new ArrayList<TreeNode>();  
  117.         TreeNode treeNode;  
  118.   
  119.         Iterator it = entityList.iterator();  
  120.         while (it.hasNext()) {  
  121.             orgEntity = (OrganizationEntity) it.next();  
  122.             treeNode = new TreeNode();  
  123.             treeNode.setObj(orgEntity);  
  124.             treeNode.setParentId(orgEntity.getParentId());  
  125.             treeNode.setSelfId(orgEntity.getOrgId());  
  126.             treeNode.setNodeName(orgEntity.getOrgName());  
  127.             tempNodeList.add(treeNode);  
  128.         }  
  129.         return tempNodeList;  
  130.     }  
  131.   
  132.     public boolean isValidTree() {  
  133.         return this.isValidTree;  
  134.     }  
  135.   
  136.     public TreeNode getRoot() {  
  137.         return root;  
  138.     }  
  139.   
  140.     public void setRoot(TreeNode root) {  
  141.         this.root = root;  
  142.     }  
  143.   
  144.     public List<TreeNode> getTempNodeList() {  
  145.         return tempNodeList;  
  146.     }  
  147.   
  148.     public void setTempNodeList(List<TreeNode> tempNodeList) {  
  149.         this.tempNodeList = tempNodeList;  
  150.     }  
  151.   
  152. }  


三 实体OrganizationEntity 
Java代码  收藏代码
  1. package com.tree;  
  2.   
  3. public class OrganizationEntity {  
  4.     public int parentId;  
  5.     public int orgId;  
  6.     public String orgName;  
  7.     public int getParentId() {  
  8.         return parentId;  
  9.     }  
  10.     public void setParentId(int parentId) {  
  11.         this.parentId = parentId;  
  12.     }  
  13.     public int getOrgId() {  
  14.         return orgId;  
  15.     }  
  16.     public void setOrgId(int orgId) {  
  17.         this.orgId = orgId;  
  18.     }  
  19.     public String getOrgName() {  
  20.         return orgName;  
  21.     }  
  22.     public void setOrgName(String orgName) {  
  23.         this.orgName = orgName;  
  24.     }  
  25. }  
原创粉丝点击