java的树(较完整)

用java实现的一个树的存储结构,以及存储和取出树信息

package pattern;

import java.util.List;
import java.util.ArrayList;

public class TreeNode implements java.io.Serializable
{
private int parentId;
private int selfId;
protected String nodeName; //protected Point p;
protected TreeNode parentNode;
protected List<TreeNode> childList;

public TreeNode()
{
   initChildList();
}

public TreeNode(String nodeName,int selfId,int parentId)
{
//   this.setParentNode(parentNode);
   this.setNodeName(nodeName);
   this.setParentId(parentId);
   this.setSelfId(selfId);
   initChildList();
}

public TreeNode(TreeNode parentNode,String nodeName,int selfId,int parentId)
{
   this.setParentNode(parentNode);
   this.setNodeName(nodeName);
   this.setParentId(parentId);
   this.setSelfId(selfId);
   initChildList();
}

public boolean isLeaf()
{
   if (childList == null)
   {
    return true;
   }
   else
   {
    if (childList.isEmpty())
    {
     return true;
    }
    else
    {
     return false;
    }
   }
}


public void addChildNode(TreeNode treeNode)
{
   initChildList();
   childList.add(treeNode);
}

public void initChildList()
{
   if (childList == null)
    childList = new ArrayList<TreeNode>();
}


public List<TreeNode> getElders()
{
   //havn't finish! 参见另一个版本。
   List<TreeNode> elderList = new ArrayList<TreeNode>();
   return elderList;
}


public List<TreeNode> getJuniors()
{
   //havn't finish!
   List<TreeNode> juniorList = new ArrayList<TreeNode>();
   return juniorList;
}


public List<TreeNode> getChildList()
{
   return childList;
}


public void deleteNode()
{
   TreeNode parentNode = this.getParentNode();
   int id = this.getSelfId();

   if (parentNode != null) {
    parentNode.deleteChildNode(id);
   }
}


public void deleteChildNode(int childId)
{
   List<TreeNode> childList = this.getChildList();
   int childNumber = childList.size();
   for (int i = 0; i < childNumber; i++) {
    TreeNode child = childList.get(i);
    if (child.getSelfId() == childId) {
     childList.remove(i);
     return;
    }
   }
}


public boolean insertJuniorNode(TreeNode treeNode)
{
   int juniorParentId = treeNode.getParentId();
   if (this.parentId == juniorParentId) {
    addChildNode(treeNode);
    return true;
   } else {
//    List<TreeNode> childList = this.getChildList();
//    int childNumber = childList.size();
//    boolean insertFlag;
//
//    for (int i = 0; i < childNumber; i++) {
//     TreeNode childNode = childList.get(i);
//     insertFlag = childNode.insertJuniorNode(treeNode);
//     if (insertFlag == true)
//      return true;
//    }
//    return false;
    TreeNode findnode=this.findTreeNodeById(juniorParentId);
    if(findnode!=null)
    {
     findnode.getChildList().add(treeNode);
     return true;
    }
    return false;
   }
}


public TreeNode findTreeNodeById(int id)
{
   if (this.selfId == id)
    return this;
   if (childList.isEmpty() || childList == null) {
    return null;
   } else {
    int childNumber = childList.size();
    for (int i = 0; i < childNumber; i++) {
     TreeNode child = childList.get(i);
     TreeNode resultNode = child.findTreeNodeById(id);
     if (resultNode != null) {
      return resultNode;
     }
    }
    return null;
   }
}


public void traverse()
{
   if (selfId < 0)
    return;
   print(this.selfId);
   if (childList == null || childList.isEmpty())
    return;
   int childNumber = childList.size();
   for (int i = 0; i < childNumber; i++)
   {
    TreeNode child = childList.get(i);
    child.traverse();
   }
}

public void print(String content)
{
   System.out.println(content);
}

public void print(int content)
{
   System.out.println(String.valueOf(content));
}

public void setChildList(List<TreeNode> childList)
{
   this.childList = childList;
}

public int getParentId()
{
   return parentId;
}

public void setParentId(int parentId)
{
   this.parentId = parentId;
}

public int getSelfId()
{
   return selfId;
}

public void setSelfId(int selfId)
{
   this.selfId = selfId;
}

public TreeNode getParentNode()
{
   return parentNode;
}

public void setParentNode(TreeNode parentNode)
{
   this.parentNode = parentNode;
}

public String getNodeName()
{
   return nodeName;
}

public void setNodeName(String nodeName)
{
   this.nodeName = nodeName;
}
}
/////////////////////////////////////////////////////////////////

package pattern;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class TreeHelper implements java.io.Serializable
{
private TreeNode root;
private List<TreeNode> tempNodeList;

public TreeHelper()
{
}

public TreeHelper(List<TreeNode> treeNodeList)
{
   tempNodeList = treeNodeList;
   generateTree();
}


public void generateTree()
{
   HashMap nodeMap = putNodesIntoMap();
   putChildIntoParent(nodeMap);
}


protected HashMap putNodesIntoMap()
{
   HashMap nodeMap = new HashMap<String, TreeNode>();
   Iterator it = tempNodeList.iterator();
   while (it.hasNext())
   {
    TreeNode treeNode = (TreeNode) it.next();
    int id = treeNode.getSelfId();
    String keyId = String.valueOf(id);
    nodeMap.put(keyId, treeNode);
   }
   return nodeMap;
}


protected void putChildIntoParent(HashMap nodeMap)
{
   Iterator it = nodeMap.values().iterator();
   while (it.hasNext())
   {
    TreeNode treeNode = (TreeNode) it.next();
    int parentId = treeNode.getParentId();
    if(parentId==0)
    {
     this.setRoot(treeNode);
    }
    else if(parentId!=treeNode.getSelfId())
    {
     String parentKeyId = String.valueOf(parentId);
     if (nodeMap.containsKey(parentKeyId))
     {
      TreeNode parentNode = (TreeNode) nodeMap.get(parentKeyId);
      if (parentNode == null) {
       return;
      }
      else
      {
       parentNode.addChildNode(treeNode);
       System.out.println("childId: " +treeNode.getSelfId()+" "+
         "parentId: "+parentNode.getSelfId());
      }
     }
    }
   }
}


protected void initTempNodeList()
{
   if (this.tempNodeList == null)
   {
    this.tempNodeList = new ArrayList<TreeNode>();
   }
}


public void addTreeNode(TreeNode treeNode)
{
   initTempNodeList();
   this.tempNodeList.add(treeNode);
}


public boolean insertTreeNode(TreeNode treeNode)
{
   boolean insertFlag = root.insertJuniorNode(treeNode);
   return insertFlag;
}

public TreeNode getRoot()
{
   return root;
}

public void setRoot(TreeNode root)
{
   this.root = root;
}

public List<TreeNode> getTempNodeList()
{
   return tempNodeList;
}

public void setTempNodeList(List<TreeNode> tempNodeList)
{
   this.tempNodeList = tempNodeList;
}

public static void main(String[] args)
{
  
}

}

 

你可能感兴趣的:(java)