Data structure Java version traversal binary tree (vi)

Source: Internet
Author: User

A binary tree is one of the most common trees we have in our program (personal view). The simplest binary tree is made up of a root node, two child nodes (one left and one right into the children's nodes). Binary tree is a combination of arrays and linked lists, which includes the advantages of quick lookup of arrays and the advantages of quick addition and deletion of linked lists. It is necessary to master the traversal method of binary tree. Here are four ways to traverse a binary tree.

Let's take a simple two-fork tree as an example to illustrate four kinds of traversal:

  

To create a tree node class:

    // tree node    class TreeNode {        publicChar  val;          Public TreeNode left;          Public TreeNode right;          Public TreeNode (char  x) {            = x;        }    }

Middle Sequence Traversal:

Go through the left child node---root node---right child node

// Middle Sequence traversal        left-root-right    (pre-post: Position of root)publicvoid  inorder (TreeNode current) {      Ifnull) {        inorder (current.left);        Visit (current);        Inorder (current.right);    }}

Pre-order Traversal:

Traverse the root node---left child node---right child node

// Pre-sequence traversal        root-left-right public void  preorder (TreeNode current)    {ifnull  ) {        Visit (current);        Inorder (current.left);        Inorder (current.right);    }}

Post-post traversal:

Traverse left child node---Right child node---root node

    // subsequent traversal        left-right-root     Public void Postorder (TreeNode current) {        ifnull) {            inorder (current.left);            Inorder (current.right);            Visit (current);        }    }

Sequence Traversal:

The same layer of nodes traverses from left to right.

    //sequence traversal of the same layer node from left to right, sequentially traversing FIFO     Public voidLevelorder (TreeNode current) {List<TreeNode> q =NewArraylist<treenode>();  while(Current! =NULL) {Visit (current); if(Current.left! =NULL) {q.add (current.left); }            if(Current.right! =NULL) {q.add (current.right); }            if(Q.isempty ())return ; Current= Q.remove (0); }    }

These are the four basic traversal methods. Let's test this by first writing a tool function to display the value of the current node.

    // ********* Auxiliary Tools ****************     Public void Visit (TreeNode node) {        System.out.print (node.val);    }

Write Test function:

  

@Test Public voidFun () {TreeNode root=NewTreeNode (' + ')); Root.left=NewTreeNode ('-'); Root.right=NewTreeNode (' E '); Root.left.left=NewTreeNode (' * ')); Root.left.right=NewTreeNode (' D '); Root.left.left.left=NewTreeNode ('/'); Root.left.left.right=NewTreeNode (' C '); Root.left.left.left.left=NewTreeNode (' A '); Root.left.left.left.right=NewTreeNode (' B '); System.out.println ("Middle sequence Traversal:");        Inorder (root);        System.out.println (); System.out.println ("Pre-order Traversal:");        Preorder (root);        System.out.println (); System.out.println ("Post-post traversal:");        Postorder (root);        System.out.println (); System.out.println ("Sequence Traversal:");        Levelorder (root);    System.out.println (); }

The result of the traversal is obtained from the above test function:

All code:

 PackageTree;Importjava.util.ArrayList;Importjava.util.List;Importorg.junit.Test;/*** Traversal of the binary number * + *-E * * D */C * A B *@authorRanter **/ Public classBinaryTree {@Test Public voidFun () {TreeNode root=NewTreeNode (' + ')); Root.left=NewTreeNode ('-'); Root.right=NewTreeNode (' E '); Root.left.left=NewTreeNode (' * ')); Root.left.right=NewTreeNode (' D '); Root.left.left.left=NewTreeNode ('/'); Root.left.left.right=NewTreeNode (' C '); Root.left.left.left.left=NewTreeNode (' A '); Root.left.left.left.right=NewTreeNode (' B '); System.out.println ("Middle sequence Traversal:");        Inorder (root);        System.out.println (); System.out.println ("Pre-order Traversal:");        Preorder (root);        System.out.println (); System.out.println ("Post-post traversal:");        Postorder (root);        System.out.println (); System.out.println ("Sequence Traversal:");        Levelorder (root);    System.out.println (); }        //****************************************************************        //Middle Sequence traversal left-root-right (pre-middle post: position of root)     Public voidinorder (TreeNode current) {if(Current! =NULL) {inorder (current.left);            Visit (current);        Inorder (Current.right); }    }    //Pre-sequence traversal root-left-right     Public voidPreorder (TreeNode current) {if(Current! =NULL) {Visit (current);            Inorder (Current.left);        Inorder (Current.right); }    }    //subsequent traversal left-right-root     Public voidPostorder (TreeNode current) {if(Current! =NULL) {inorder (current.left);            Inorder (Current.right);        Visit (current); }    }    //sequence traversal of the same layer node from left to right (using queue sequence traversal)     Public voidLevelorder (TreeNode current) {List<TreeNode> q =NewArraylist<treenode>();  while(Current! =NULL) {Visit (current); if(Current.left! =NULL) {q.add (current.left); }            if(Current.right! =NULL) {q.add (current.right); }            if(Q.isempty ())return ; Current= Q.remove (0); }    }        //********* Auxiliary Tools ****************     Public voidVisit (TreeNode node) {System.out.print (node.val); }                //tree Node    classTreeNode { Public CharVal;  PublicTreeNode left;  PublicTreeNode right;  PublicTreeNode (Charx) {val=x; }    }}

Data structure Java version traversal binary tree (vi)

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.