java实现树

来源:互联网 发布:七日杀不让刷js 编辑:程序博客网 时间:2024/06/05 20:34
实现一颗树,采用数组的存储方式,将树中的节点用Node类表示,方便与操作。
首先,整棵树的数组结构如下表所示,根节点的无父节点,用“-1”表示。
indexDataparent0A-11B02C03D04E15F16G5

其次,定义一个节点Node类,用来存储每个节点的内容

[java] view plaincopy
  1. package my.tree;  
  2.   
  3. public class Node<T> {  
  4.     private T data;  
  5.     private int parent;  
  6.       
  7.     public Node(){  
  8.     }  
  9.       
  10.     public Node(T data){  
  11.         this.data = data;  
  12.     }  
  13.       
  14.     public Node(T data,int parent){  
  15.         this.data = data;  
  16.         this.parent = parent;  
  17.     }  
  18.       
  19.     public void setData(T data){  
  20.         this.data = data;  
  21.     }  
  22.       
  23.     public T getData(){  
  24.         return this.data;  
  25.     }  
  26.       
  27.     public void setParent(int parent){  
  28.         this.parent = parent;  
  29.     }  
  30.       
  31.     public int getParent(){  
  32.         return this.parent;  
  33.     }  
  34. }  


开始实现树,提供基本的插入节点、获取所有节点、获取树的深度操作(着重)这些将数组大小设置为2,主要是考虑数组能否自动扩容;

[java] view plaincopy
  1. package my.tree;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Arrays;  
  5. import java.util.List;  
  6.   
  7. public class MyTree<T> {  
  8.     private final int DEFAULT_SIZE = 2;  
  9.     private int size;  
  10.     private int count;  
  11.     private Object[] nodes;  
  12.   
  13.     public MyTree() {  
  14.         this.size = this.DEFAULT_SIZE;  
  15.         this.nodes = new Object[this.size];  
  16.         this.count = 0;  
  17.     }  
  18.   
  19.     public MyTree(Node<T> root) {  
  20.         this();  
  21.         this.count = 1;  
  22.         this.nodes[0] = root;  
  23.     }  
  24.   
  25.     public MyTree(Node<T> root, int size) {  
  26.         this.size = size;  
  27.         this.nodes = new Object[this.size];  
  28.         this.count = 1;  
  29.         this.nodes[0] = root;  
  30.     }  
  31.   
  32.     //添加一个节点  
  33.     public void add(Node<T> node) {  
  34.         for (int i = 0; i < this.size; i++) {  
  35.             if (this.nodes[i] == null) {  
  36.                 nodes[i] = node;  
  37.                 break;  
  38.             }  
  39.         }  
  40.         this.count++;  
  41.     }  
  42.   
  43.     public void check(){  
  44.         if(this.count >= this.size){  
  45.             this.enlarge();  
  46.         }  
  47.     }  
  48.     //添加一个节点,并指明父节点  
  49.     public void add(Node<T> node, Node<T> parent) {  
  50.         this.check();  
  51.         node.setParent(this.position(parent));  
  52.         this.add(node);  
  53.     }  
  54.   
  55.     //获取节点在数组的存储位置  
  56.     public int position(Node<T> node) {  
  57.         for (int i = 0; i < this.size; i++) {  
  58.             if (nodes[i] == node) {  
  59.                 return i;  
  60.             }  
  61.         }  
  62.         return -1;  
  63.     }  
  64.       
  65.     //获取整棵树有多少节点  
  66.     public int getSize(){  
  67.         return this.count;  
  68.     }  
  69.       
  70.     //获取根节点  
  71.     @SuppressWarnings("unchecked")  
  72.     public Node<T> getRoot(){  
  73.         return (Node<T>) this.nodes[0];  
  74.     }  
  75.       
  76.     //获取所以节点,以List形式返回  
  77.     @SuppressWarnings("unchecked")  
  78.     public List<Node<T>> getAllNodes(){  
  79.         List<Node<T>> list = new ArrayList<Node<T>>();  
  80.         for(int i=0;i<this.size;i++){  
  81.             if(this.nodes[i] != null){  
  82.                 list.add((Node<T>)nodes[i]);  
  83.             }  
  84.         }  
  85.         return list;  
  86.     }  
  87.       
  88.     //获取树的深度,只有根节点时为1  
  89.     @SuppressWarnings("unchecked")  
  90.     public int getDepth(){  
  91.           
  92.         int max = 1;  
  93.         if(this.nodes[0] == null){  
  94.             return 0;  
  95.         }  
  96.           
  97.         for(int i=0;i<this.count;i++){  
  98.             int deep = 1;  
  99.             int location = ((Node<T>)(this.nodes[i])).getParent();  
  100.             while(location != -1 && this.nodes[location] != null){  
  101.                 location = ((Node<T>)(this.nodes[location])).getParent();  
  102.                 deep++;  
  103.             }  
  104.             if(max < deep){  
  105.                 max = deep;  
  106.             }  
  107.         }  
  108.         return max;  
  109.     }  
  110.       
  111.     public void enlarge(){  
  112.         this.size = this.size + this.DEFAULT_SIZE;  
  113.         Object[] newNodes = new Object[this.size];  
  114.         newNodes = Arrays.copyOf(nodes, this.size);  
  115.         Arrays.fill(nodes, null);  
  116.         this.nodes = newNodes;  
  117.         System.out.println("enlarge");  
  118.     }  
  119. }  


最后,使用MyTreeClient来测试下,基本功能是否都已经实现;

[java] view plaincopy
  1. package my.tree;  
  2.   
  3. public class MyTreeClient {  
  4.     public static void main(String[] args){  
  5.         Node<String> root = new Node<String>("A",-1);  
  6.         MyTree<String> tree = new MyTree<String>(root);  
  7.         Node<String> b = new Node<String>("B");  
  8.         Node<String> c = new Node<String>("C");  
  9.         Node<String> d = new Node<String>("D");  
  10.         Node<String> e = new Node<String>("E");  
  11.         Node<String> f = new Node<String>("F");  
  12.         Node<String> g = new Node<String>("G");  
  13.         tree.add(b,root);  
  14.         tree.add(c,root);  
  15.         tree.add(d,root);  
  16.           
  17.         tree.add(e,b);  
  18.         tree.add(f,b);  
  19.         tree.add(g,f);  
  20.           
  21.           
  22.         System.out.println(tree.getSize());  
  23.         System.out.println(tree.getRoot().getData());  
  24.         System.out.println(tree.getAllNodes());  
  25.         System.out.println(tree.getDepth());  
  26.         tree.add(new Node<String>("H"),g);  
  27.         System.out.println(tree.getDepth());  
  28.         tree.enlarge();  
  29.     }  
0 0
原创粉丝点击