"Java Data Structure" tree structure

Tree is a hierarchical nested structure. A tree structure of the outer and inner layers have a similar structure, so the structure can be recursive multi FIG. Various classical data structure is a tree diagram of a typical tree structure: can be simply expressed as a tree root, left subtree, right subtree. Left subtree and right subtree has its own sub-tree.

Schematic:

All in the code:

Import of java.util.ArrayList;
 Import java.util.List; 

public  class the TreeNode {
     Private  int Age;     // node attributes, age 
    Private String name;    // node attributes, the name of 

    the TreeNode proTreeNode;     // upper node 
    List <TreeNode> list = new new the ArrayList <the TreeNode> (); // child node 

    public  int getAge () {
         return Age; 
    } 

    public  void the setAge ( int Age) {
         the this .age = Age; 
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public TreeNode getProTreeNode() {
        return proTreeNode;
    }

    public void setProTreeNode(TreeNode proTreeNode) {
        this.proTreeNode = proTreeNode;
    }

    public void addTreeNode(TreeNode treeNode){
        list.add(treeNode);
        treeNode.proTreeNode = this;
    }

    public TreeNode getTreeNode(Integer i){
        return list.get(i);
    }

    public void removeTreeNode(TreeNode treeNode){
        list.remove(treeNode);
        treeNode.proTreeNode = null;
    }

    public String toString(){
        return "姓名:"+name+";年龄:"+age+"。";
    }
}
/ ** 
 * Create a tree: 
 * 
 * Lei 
 * | | 
 * Meimei Lily 
 * | 
 * Gang 
 * / 
public  class TreeStructure {
     public  static  void main (String [] args) { 
        the TreeNode treeNode0 = new new the TreeNode (); 
        treeNode0. the setAge ( . 11 ); 
        treeNode0.setName ( "Lei" ); 

        the TreeNode treeNode1 = new new the TreeNode (); 
        treeNode1.setAge ( 13 is ); 
        treeNode1.setName ("Han Meimei" ); 

        the TreeNode treeNode2 = new new the TreeNode (); 
        treeNode2.setAge ( 15 ); 
        treeNode2.setName ( "Lily" ); 

        the TreeNode treeNode3 = new new the TreeNode (); 
        treeNode3.setAge ( 15 ); 
        treeNode3.setName ( "Gang " ); 

        treeNode0.addTreeNode (treeNode1); // parent node associated with the child node 
        treeNode0.addTreeNode (treeNode2); // parent node associated with the child node 

        treeNode2.addTreeNode (treeNode3); // next node linked to Lily Li Gang 

        System.out. println (treeNode1);    //Printing node itself 
        System.out.println (treeNode2.getTreeNode (0));   // print tree node 
        System.out.println (treeNode1.getProTreeNode ());   // Print interfaces parent 
    } 
}

operation result:

 

Guess you like

Origin www.cnblogs.com/jssj/p/11617610.html