Java traversal of binary tree by hierarchy

Source: Internet
Author: User

/** * @authorCJ July 14, 2017 pm 1:14:31*/ PackageCom.yuanye.algorithm;Importjava.util.LinkedList;Importjava.util.List; Public classBinaryTree {//private static list<node> currentlevelnodes=new arraylist<> ();//private static list<node> nextlevelnodes=new arraylist<> ();    Private StaticList<node> nodelist=NewLinkedlist<>();  Public Static voidMain (string[] args) {Node RootNode=NewNode (); Rootnode.setvalue (88); Generatetree (RootNode,5,0);    Printtree (RootNode); }     Public Static voidGeneratetree (Node parentnode,intDepthintcurrentdepth) {        if(currentdepth>depth-1)            return; Node Leftnode=NewNode (); Leftnode.setvalue (6000+currentdepth); Node Rightnode=NewNode (); Rightnode.setvalue (9000+currentdepth);        Parentnode.setleftnode (Leftnode);        Parentnode.setrightnode (Rightnode); Generatetree (leftnode,depth,currentdepth+1); Generatetree (rightnode,depth,currentdepth+1); }     Public Static voidPrinttree (Node rootNode) {System.out.println (Rootnode.getvalue ()); Node Leftnode=Rootnode.getleftnode (); Node Rightnode=Rootnode.getrightnode (); if(leftnode!=NULL) {nodelist.add (Leftnode); }        if(rightnode!=NULL) {nodelist.add (Rightnode); }//if (currentlevelnodes.size () ==0) {//Currentlevelnodes.addall (nextlevelnodes);//Nextlevelnodes.removeall (nextlevelnodes);//        }//if (currentlevelnodes.size () >0) {//Node nextnode=currentlevelnodes.get (0);//currentlevelnodes.remove (0);//Printtree (nextnode);//        }        if(Nodelist.size () >0) {Node NextNode=nodelist.get (0); Nodelist.remove (0);        Printtree (NextNode); }    }}classnode{Private intvalue; PrivateNode Leftnode; PrivateNode Rightnode;  Public intGetValue () {returnvalue; }     Public voidSetValue (intvalue) {         This. Value =value; }     PublicNode Getleftnode () {returnLeftnode; }     Public voidSetleftnode (Node leftnode) { This. Leftnode =Leftnode; }     PublicNode Getrightnode () {returnRightnode; }     Public voidSetrightnode (Node rightnode) { This. Rightnode =Rightnode; } @Override PublicString toString () {return"Node [value=" + Value + ", leftnode=" + Leftnode + ", rightnode=" + Rightnode + "]"; }}

Java traversal of binary tree by hierarchy

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.