Tree structure of Java (binary tree)

Source: Internet
Author: User

Import java.util.*;p ublic class main{Chainbin bt;    Public main () {} public main (Chainbin BT) {THIS.BT=BT;        public static void Main (string[] args) {Chainbin root = null;        Char Select;            do{System.out.println ("1." Set the root element of the two fork tree, \n2. Add two fork tree nodes \n3. Sequence traversal \n4. Sequence traversal \n5 The traversal \n6 by layer traversal \n7 two fork tree depth, exit ");            Scanner s = new Scanner (system.in);            select = S.next (). charAt (0);            System.out.println (select+ "--");                Switch (SELECT) {case ' 1 ': root = Init (); System.out.println (Root.                Data.data);            Break                Case ' 2 ': System.out.println ("Add two Fork tree node"); New Chaintree ().                Binadd (root);            Break                Case ' 3 ': System.out.println ("First order traversal!"); New Chaintree ().                Bintree_baa (Root, new Meth (root));                System.out.println ();            Break Case ' 4 ': System.out.println ("Middle order traversal! "); New Chaintree ().                Bintree_aba (Root, new Meth (root));                System.out.println ();            Break                Case ' 5 ': System.out.println ("post-post traversal!"); New Chaintree ().                Bintree_aab (Root, new Meth (root));                System.out.println ();            Break                Case ' 6 ': System.out.println ("Traverse by layer!"); New Chaintree ().                Bintree_level (Root, new Meth (root));                System.out.println ();            Break                Case ' 7 ': System.out.println ("Binary tree depth!"); System.out.println ("depth is" +new chaintree ().                Bintreedepth (root));                System.out.println ();            Break            Case ' 0 ': break;            Default:break;        }}while (select!= ' 0 '); New Chaintree ().        Bintreeclear (root);    Root=null;        } static Chainbin Init () {chainbin node= new Chainbin (); System.out.println("Please input the node.");        Scanner s = new Scanner (system.in);        int A=s.nextint (); Node.        Data=new DATA (a);        Node.left = null;        Node.right = null;    return node;    }}interface method{void Fun (); void Fun (Chainbin bt);}    Class Meth implements method{Chainbin root =null;    Public Meth (Chainbin root) {this.root = root; } public void Fun (Chainbin bt) {System.out.println (BT).    Data.data);    } public void Fun () {}}class data{public int DATA;    Public DATA (int a) {this.data = A;    }}class chainbin{data data;    Chainbin left; Chainbin right;}    Class chaintree{Chainbin node=null;    Public Chaintree () {} public Chaintree (Chainbin node) {this.node = node;    } public Chainbin GetNode () {return node; }//input the Chainbin to the Chaintree a Boolean binadd (Chainbin bt, chainbin node, int n) {//add the Bin to Chaintre E//bt is the Father Bin, node is the child and N 1 is leFT Other 2 right if (Bt==null) {return false;        } System.out.println ("input data");        Scanner s = new Scanner (system.in); Node.        data = new data (S.nextint ());                Switch (n) {case 1:{if (bt.left!=null) {//left isn ' t exist return false;                    }else{Bt.left=node;                System.out.println ("left OK");        }}break;                Case 2:{if (bt.right!=null) {//right isn ' t exist return false;                    }else{Bt.right=node;                System.out.println ("left OK");        }}break;        Default:return false;    } return true;        } void Binadd (Chainbin bt) {//add the Bin to Chaintree Chainbin node=null, Parent=null;        DATA Data=null;        Char Select;        Node=new Chainbin ();   System.out.println ("Input root data..");     Scanner s = new Scanner (system.in);        data = new data (S.nextint ());        Parent = Bintreefind (BT, data);            if (parent==null) {System.out.println ("Find not:");        return;        } System.out.println ("1.to add left tree\n2.to add left Tree");            Do {select =s.next (). charAt (0);                if (select== ' 1 ' | | select== ' 2 ') {Binadd (Parent, node, (int) (select-' 0 '));            System.out.println ("OK");        }} while (select!= ' 1 ' && select!= ' 2 ');    System.out.println ("over");        }//get the left right treebin chainbin Bintreeleft (Chainbin bt) {if (bt!=null) {return bt.left;        }else{return null;        }} chainbin Bintreeright (Chainbin bt) {if (bt!=null) {return bt.right;        }else{return null;    }}//cheek the Tree is empty; Boolean bintreeisempty (Chainbin bt) {if (BT ==null) {RETurn true;        }else{return false;        }} int bintreedepth (Chainbin bt) {int dep1, DEP2;        if (bt==null) {return 0;            }else{dep1 = bintreedepth (bt.left);            DEP2 = Bintreedepth (bt.right);            if (Dep1 > Dep2) {return dep1 +1;            }else{return DEP2 +1;        }}}//check from Treebin chainbin bintreefind (chainbin bt,data DATA) {Chainbin p=null;            if (bt==null) {System.out.println ("null");        Return BT; }else{//recursion if (BT. Data.data = = data.data) {System.out.println (BT.                Data.data+ "has" +data.data);            Return BT;                    }else{if ((P=bintreefind (bt.left,data))!=null) {System.out.println ("--->>");                return p; }else if ((P=bintreefind (bt.right,data))!=null) {System.out.println("--->>");                return p;                }else{return null; }}}}//clear the Treebin void Bintreeclear (Chainbin bt) {if (bt!=null) {Bin            Treeclear (Bt.left);        Bintreeclear (Bt.right);    } bt=null;            }//send out Tree void Bintree_baa (Chainbin bt,method oper) {//Pre-order recursive if (bt!=null) {oper.fun (BT); Bintree_baa (Bt.left,oper); Recursion Bintree_baa (Bt.right,oper);    recursion} return;            } void Bintree_aba (Chainbin bt,method oper) {//middle order recursive if (bt!=null) {oper.fun (BT); Bintree_aba (Bt.left,oper); Recursion Bintree_aba (Bt.right,oper);    recursion} return;            } void Bintree_aab (Chainbin bt,method oper) {//post-post recursion if (bt!=null) {oper.fun (BT); Bintree_aab (Bt.left,oper); Recursion Bintree_aab (Bt.right,oper);     Recursion   } return;        } void Bintree_level (Chainbin bt, Method Oper) {Chainbin p=null;        Chainbin q[] = new chainbin[1000];        int head=0,tail=0;            if (bt!=null) {tail = (tail+1)%1000;        Q[tail] = BT;            } while (Head!=tail) {head = (head+1)%1000;            p = Q[head];            Oper.fun (P);                if (p.left!=null) {tail = (tail+1)%1000;            Q[tail] = P.left;                } if (p.right!=null) {tail = (tail+1)%1000;            Q[tail] = p.right;    }} return; }}

Tree structure of Java (binary tree)

Related Article

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.