java实现树(链式存储)

来源:互联网 发布:七日杀不让刷js 编辑:程序博客网 时间:2024/06/05 11:56

java实现树,采用链式存储,父节点记录子节点的存储位置。

首先定义一个用于存储子节点位置的节点类

[java] view plaincopy
  1. package my.tree.link;  
  2.   
  3. public class SubNode {  
  4.     private int location;  
  5.     private SubNode next;  
  6.       
  7.     public SubNode(){  
  8.         this.location = 0;  
  9.         this.next = null;  
  10.     }  
  11.     public SubNode(int location){  
  12.         this.location = location;  
  13.         this.next = null;  
  14.     }  
  15.       
  16.     public SubNode(int location, SubNode next){  
  17.         this.location = location;  
  18.         this.next = next;  
  19.     }  
  20.       
  21.     public void setLocation(int location){  
  22.         this.location = location;  
  23.     }  
  24.       
  25.     public int getLocation(){  
  26.         return this.location;  
  27.     }  
  28.       
  29.     public void setNext(SubNode next){  
  30.         this.next = next;  
  31.     }  
  32.       
  33.     public SubNode getNext(){  
  34.         return this.next;  
  35.     }  
  36. }  


然后定义一个用于存储节点信息的节点类

[java] view plaincopy
  1. package my.tree.link;  
  2.   
  3. public class Node<T> {  
  4.     private T data;  
  5.     private SubNode son;  
  6.       
  7.     public Node(){  
  8.           
  9.     }  
  10.       
  11.     public Node(T data){  
  12.         this.data = data;  
  13.         this.son = null;  
  14.     }  
  15.       
  16.     public Node(T data, SubNode son){  
  17.         this.data = data;  
  18.         this.son = son;  
  19.     }  
  20.       
  21.     public void setData(T data){  
  22.         this.data = data;  
  23.     }  
  24.       
  25.     public T getData(){  
  26.         return this.data;  
  27.     }  
  28.       
  29.     public void setSon(SubNode son){  
  30.         this.son = son;  
  31.     }  
  32.       
  33.     public SubNode getSon(){  
  34.         return this.son;  
  35.     }  
  36.       
  37.     @Override  
  38.     public String toString(){  
  39.         return "节点:" + this.data;  
  40.     }  
  41. }  




编写链式存储的树类,这里采用递归求解树的深度(貌似有问题,在求树的深度是,很迷糊)

[java] view plaincopy
  1. package my.tree.link;  
  2.   
  3. import java.util.LinkedList;  
  4. import java.util.List;  
  5.   
  6. public class MyLinkTree<T> {  
  7.     private final int DEFAUL_SIZE = 10;  
  8.     private int size;  
  9.     private int count;  
  10.   
  11.     private Node<T>[] nodes;  
  12.   
  13.     @SuppressWarnings("unchecked")  
  14.     public MyLinkTree() {  
  15.         this.size = this.DEFAUL_SIZE;  
  16.         this.nodes = new Node[this.size];  
  17.         this.count = 0;  
  18.     }  
  19.   
  20.     @SuppressWarnings("unchecked")  
  21.     public MyLinkTree(int size) {  
  22.         this.size = size;  
  23.         this.nodes = new Node[this.size];  
  24.         this.count = 0;  
  25.     }  
  26.   
  27.     public MyLinkTree(T data) {  
  28.         this();  
  29.         Node<T> node = new Node<T>(data);  
  30.         this.nodes[0] = node;  
  31.         this.count++;  
  32.     }  
  33.   
  34.     public MyLinkTree(Node<T> root) {  
  35.         this();  
  36.         this.nodes[0] = root;  
  37.         this.count++;  
  38.     }  
  39.   
  40.     public void add(Node<T> node, Node<T> parent) {  
  41.         SubNode son = new SubNode();  
  42.         for (int i = 0; i < this.size; i++) {  
  43.             if (this.nodes[i] == null) {  
  44.                 this.nodes[i] = node;  
  45.                 son.setLocation(i);  
  46.                 break;  
  47.             }  
  48.         }  
  49.   
  50.         // 往链表中添加子节点位置  
  51.         SubNode next = parent.getSon();  
  52.         if (next != null) {  
  53.             while (next.getNext() != null) {  
  54.                 next = next.getNext();  
  55.             }  
  56.             next.setNext(son);  
  57.         } else {  
  58.             parent.setSon(son);  
  59.         }  
  60.   
  61.         this.count++;  
  62.     }  
  63.   
  64.     public int size() {  
  65.         return this.count;  
  66.     }  
  67.   
  68.     public Node<T> getRoot() {  
  69.         return this.nodes[0];  
  70.     }  
  71.   
  72.     // 获取指定节点的子节点  
  73.     public List<Node<T>> getSon(Node<T> parent) {  
  74.         List<Node<T>> list = new LinkedList<Node<T>>();  
  75.         SubNode son = parent.getSon();  
  76.         while (son != null) {  
  77.             list.add(this.nodes[son.getLocation()]);  
  78.             son = son.getNext();  
  79.         }  
  80.         return list;  
  81.     }  
  82.   
  83.     // 获取树的深度,通过递归的方式来解决  
  84.     public int getDepth(Node<T> node) {  
  85.         SubNode son = node.getSon();  
  86.         if(son == null){  
  87.             return 1;  
  88.         }else{  
  89.             int max = 0;  
  90.             while(son != null){  
  91.                 int temp = this.getDepth(this.nodes[son.getLocation()]);  
  92.                 max = temp > max ? temp : max;  
  93.                 son = son.getNext();  
  94.             }  
  95.             //为什么要max+1?  
  96.             return max+1;  
  97.         }  
  98.     }  
  99.   
  100.     public int deep() {  
  101.         int max = 0;  
  102.         for (int i = 0; i < this.count; i++) {  
  103.             int temp = this.getDepth(this.nodes[i]);  
  104.             max = max > temp ? max : temp;  
  105.         }  
  106.         return max;  
  107.     }  
  108. }  



最后编写一个测试端,用来测试功能是否基本实现

[java] view plaincopy
  1. package my.tree.link;  
  2.   
  3. public class MyLinkTreeClient {  
  4.     public static void main(String[] args) {  
  5.         Node<string> root = new Node<string>("A");  
  6.         Node<string> b = new Node<string>("B");  
  7.         Node<string> c = new Node<string>("C");  
  8.         Node<string> d = new Node<string>("D");  
  9.         Node<string> e = new Node<string>("E");  
  10.         Node<string> f = new Node<string>("F");  
  11.         Node<string> g = new Node<string>("G");  
  12.         Node<string> h = new Node<string>("H");  
  13.         MyLinkTree<string> tree = new MyLinkTree<string>(root);  
  14.         tree.add(b, root);  
  15. //      tree.add(c, root);  
  16. //      tree.add(d, root);  
  17. //      tree.add(e, b);  
  18. //      tree.add(f, b);  
  19. //      tree.add(g, f);  
  20. //      tree.add(h, g);  
  21. //      System.out.println(tree.size());  
  22.         System.out.println(tree.deep());  
  23. //      System.out.println(tree.getSon(b));  
  24.     }  
  25. }</string></string></string></string></string></string></string></string></string></string></string></string></string></string></string></string></string></
0 0
原创粉丝点击