JAVA Implementation tree (chained storage)

Source: Internet
Author: User

The Java implementation tree uses chained storage. The parent node records the storage location of the child node.

First, define a node class for storing the sub-node locations.

package my.tree.link;public class SubNode {private int location;private SubNode next;public SubNode(){this.location = 0;this.next = null;}public SubNode(int location){this.location = location;this.next = null;}public SubNode(int location, SubNode next){this.location = location;this.next = next;}public void setLocation(int location){this.location = location;}public int getLocation(){return this.location;}public void setNext(SubNode next){this.next = next;}public SubNode getNext(){return this.next;}}

Define a node class for storing node Information

Package my. tree. link; public class node <t> {private t data; private subnode son; Public node () {} public node (t data) {This. data = data; this. son = NULL;} public node (t data, subnode son) {This. data = data; this. son = Son;} public void setdata (t data) {This. data = data;} public t getdata () {return this. data;} public void setson (subnode son) {This. son = Son;} public subnode getson () {return this. son;} @ overridepublic string tostring () {return "node:" + this. data ;}}

Compile the tree class of chained storage. Here we use recursion to solve the depth of the tree (it seems that there is a problem. The depth of the tree is confusing)

Package my. tree. link; import Java. util. using list; import Java. util. list; public class mylinktree <t> {private final int defaul_size = 10; private int size; private int count; private node <t> [] nodes; @ suppresswarnings ("unchecked ") public mylinktree () {This. size = This. defaul_size; this. nodes = new node [this. size]; this. count = 0 ;}@ suppresswarnings ("unchecked") Public mylinktree (INT size) {This. size = size; thi S. nodes = new node [this. size]; this. count = 0;} public mylinktree (t data) {this (); node <t> node = new node <t> (data); this. nodes [0] = node; this. count ++;} public mylinktree (node <t> root) {this (); this. nodes [0] = root; this. count ++;} public void add (node <t> node, node <t> parent) {subnode son = new subnode (); For (INT I = 0; I <this. size; I ++) {If (this. nodes [I] = NULL) {This. nodes [I] = node; son. setlocation (I); break ;}}// Add the subnode location subnode next = parent. getson () to the linked list; If (next! = NULL) {While (next. getnext ()! = NULL) {next = next. getnext ();} next. setnext (son);} else {parent. setson (son);} This. count ++;} public int size () {return this. count;} public node <t> getroot () {return this. nodes [0];} // obtain the public list of subnodes of the specified node <t> getson (node <t> parent) {list <node <t> List = new partition list <node <t> (); subnode son = parent. getson (); While (son! = NULL) {list. add (this. nodes [son. getlocation ()]); son = Son. getnext () ;}return list ;}// obtain the depth of the tree. The public int getdepth (node <t> node) {subnode son = node is solved recursively. getson (); If (son = NULL) {return 1 ;}else {int max = 0; while (son! = NULL) {int temp = This. getdepth (this. nodes [son. getlocation ()]); max = temp> Max? Temp: Max; son = Son. getnext () ;}// why Max + 1? Return MAX + 1 ;}} public int deep () {int max = 0; For (INT I = 0; I <this. count; I ++) {int temp = This. getdepth (this. nodes [I]); max = max> temp? MAX: temp;} return Max ;}}

Finally, write a test terminal to test whether the function is basically implemented.

package my.tree.link;public class MyLinkTreeClient {public static void main(String[] args) {Node
 
   root = new Node
  
   ("A");Node
   
     b = new Node
    
     ("B");Node
     
       c = new Node
      
       ("C");Node
       
         d = new Node
        
         ("D");Node
         
           e = new Node
          
           ("E");Node
           
             f = new Node
            
             ("F");Node
             
               g = new Node
              
               ("G");Node
               
                 h = new Node
                
                 ("H");MyLinkTree
                 
                   tree = new MyLinkTree
                  
                   (root);tree.add(b, root);//tree.add(c, root);//tree.add(d, root);//tree.add(e, b);//tree.add(f, b);//tree.add(g, f);//tree.add(h, g);//System.out.println(tree.size());System.out.println(tree.deep());//System.out.println(tree.getSon(b));}}
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.