Construction of complete binary tree of Java and four kinds of traversal methods

Source: Internet
Author: User

Originally is the basic knowledge, can not throw too clean, today unexpectedly took so long time to write, remember.

Like a complete binary tree:

The result of the first order traversal should be: 1 2 4 5 3 6 7

The result of the middle sequence traversal should be: 4 2 5 1 6 3 7

Post-order traversal results should be: 4 5 2 6 7 3 1

Sequence traversal results should be: 1 2 3 4 5 6 7

The binary tree's first order traversal, the middle sequence traversal, the post-order traversal are all the same, the recursive operation is performed.

I'm going to record the hierarchy traversal: The hierarchy traversal needs to use the queue, first team in the squad, each time out of the team's element check is whether it has left and right children, there will be added to the queue, due to the use of the queue of the first-out principle, the hierarchy traversal.

The complete code (Java implementation) is documented below, including several traversal methods:

ImportJava.util.ArrayDeque;Importjava.util.ArrayList;Importjava.util.List;ImportJava.util.Queue;/*** Define binary tree node elements *@authorBubble **/classNode { PublicNode Leftchild;  PublicNode Rightchild;  Public intdata;  PublicNode (intdata) {         This. data =data; }} Public classTestbintree {/*** Construct a arry array into a fully binary tree *@paramthe array that arr needs to build *@returnTwo root node of the fork tree*/     PublicNode Initbintree (int[] arr) {        if(Arr.length = = 1) {            return NewNode (arr[0]); } List<Node> nodeList =NewArraylist<>();  for(inti = 0; i < arr.length; i++) {Nodelist.add (NewNode (Arr[i])); }        inttemp = 0;  while(Temp <= (arr.length-2)/2) {//notice here that the subscript of the array is zero- based            if(2 * temp + 1 <arr.length) {nodelist.get (temp). Leftchild= Nodelist.get (2 * temp + 1); }            if(2 * temp + 2 <arr.length) {nodelist.get (temp). Rightchild= Nodelist.get (2 * temp + 2); } temp++; }        returnNodelist.get (0); }     /*** Sequence traversal binary tree, and layered print *@paramroot two fork root node **/      Public voidtrivalbintree (Node root) {Queue<Node> Nodequeue =NewArraydeque<>();        Nodequeue.add (root); Node Temp=NULL; intCurrentLevel = 1;//record the number of nodes that the current layer needs to print        intNextlevel = 0;//record the number of nodes that need to be printed on the next layer         while(temp = Nodequeue.poll ())! =NULL) {            if(Temp.leftchild! =NULL) {nodequeue.add (temp.leftchild); Nextlevel++; }            if(Temp.rightchild! =NULL) {nodequeue.add (temp.rightchild); Nextlevel++; } System.out.print (Temp.data+ " "); CurrentLevel--; if(CurrentLevel = = 0) {System.out.println (); CurrentLevel=Nextlevel; Nextlevel= 0; }        }    }          /*** First Order traversal *@paramroot two fork root node*/         Public voidpretrival (Node root) {if(Root = =NULL) {                return; } System.out.print (Root.data+ " ");            Pretrival (Root.leftchild);        Pretrival (Root.rightchild); }        /*** Middle Sequence traversal *@paramroot two fork root node*/         Public voidmidtrival (Node root) {if(Root = =NULL) {                return;            } midtrival (Root.leftchild); System.out.print (Root.data+ " ");        Midtrival (Root.rightchild); }        /*** Post-post traversal *@paramroot two fork root node*/         Public voidaftertrival (Node root) {if(Root = =NULL) {                return;            } aftertrival (Root.leftchild);            Aftertrival (Root.rightchild); System.out.print (Root.data+ " "); }                         Public Static voidMain (string[] args) {testbintree btree=NewTestbintree (); int[] arr =New int[] {1,2,3,4,5,6,7}; Node Root=Btree.initbintree (arr); System.out.println ("Sequence traversal (layered printing):");            Btree.trivalbintree (root); System.out.println ("\ n First Order traversal:");            Btree.pretrival (root); System.out.println ("\ n Sequence Traversal:");            Btree.midtrival (root); System.out.println ("\ nthe post-traversal:");                    Btree.aftertrival (root); }            } 

Traversal results:

Construction of complete binary tree of Java and four kinds of traversal methods

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.