Recursive and non-recursive traversal of Binary Trees (for example)

Source: Internet
Author: User

Recursive and non-recursive traversal of Binary Trees (for example)

Binary tree traversal mainly involves Recursive Implementation and non-Recursive Implementation. Recursive Implementation is easy to understand. Non-Recursive Implementation mainly utilizes the stack idea, and then comes first and foremost, in this article, the non-recursive traversal of Binary Trees is implemented mainly by using the functions that the consumer list can be used as a stack. An example is as follows:

Package com. sheepmu; import java. util. optional list; public class BinaryTree {private TreeNode root; // root node public BinaryTree () {} public BinaryTree (TreeNode root) {this. root = root;} public TreeNode getRoot () {return root;} public void setRoot (TreeNode root) {this. root = root;}/*** node class */private static class TreeNode {private String data = null; // private TreeNode left of data; // reference of left node privat E TreeNode right; // reference of the right node public TreeNode (String data, TreeNode left, TreeNode right) // constructor of the node, the node created in the test function uses it {this. data = data; this. left = left; this. right = right;} public String getData () // get and set methods of the node class {return data;} public void setData (String data) {this. data = data;} public TreeNode getLeft () {return left;} public void setLeft (TreeNode left) {this. left = left;} public TreeNode ge TRight () {return right;} public void setRight (TreeNode right) {this. right = right ;}/ ***** recursive pretraversal */public void preOrder (TreeNode node) {if (node! = Null) {System. out. print (node. getData () + ""); preOrder (node. getLeft (); preOrder (node. getRight () ;}}/*** recursive traversal */public void inOrder (TreeNode node) {if (node! = Null) {inOrder (node. getLeft (); System. out. print (node. getData () + ""); inOrder (node. getRight () ;}}/*** recursive subsequent traversal */public void postOrder (TreeNode node) {if (node! = Null) {postOrder (node. getLeft (); postOrder (node. getRight (); System. out. print (node. getData () + "") ;}}/*** non-recursive pre-continuation traversal */public void preOrderNoRecursion () {revoke list
 
  
Stack = new consumer list
  
   
(); Stack. push (root); TreeNode current = null; while (! Stack. isEmpty () {current = stack. pop (); System. out. print (current. data + ""); if (current. getRight ()! = Null) stack. push (current. getRight (); if (current. getLeft ()! = Null) stack. push (current. getLeft ();} System. out. println ();}/*** continuous traversal in non-recursion */public void inorderNoRecursion () {revoke list
   
    
Stack = new consumer list
    
     
(); TreeNode current = root; while (current! = Null |! Stack. isEmpty () {while (current! = Null) {stack. push (current); current = current. getLeft ();} if (! Stack. isEmpty () {current = stack. pop (); System. out. print (current. data + ""); current = current. getRight () ;}} System. out. println ();}/*** non-recursive subsequent traversal */public void postorderNoRecursion () {TreeNode rNode = null; TreeNode current = root; history list
     
      
Stack = new consumer list
      
        (); While (current! = Null |! Stack. isEmpty () {while (current! = Null) {stack. push (current); current = current. getLeft () ;}current = stack. pop (); while (current! = Null & (current. getRight () = null | current. getRight () = rNode) {System. out. print (current. data + ""); rNode = current; if (stack. isEmpty () {System. out. println (); return;} current = stack. pop ();} stack. push (current); current = current. getRight () ;}} public static void main (String [] args) {TreeNode l2 = new TreeNode ("E", null, null ); // The Five Elements construct a binary tree TreeNode r2 = new TreeNode ("D", null, null); TreeNode l1 = new TreeNode ("B", null, r2 ); // TreeNode r1 = new TreeNode ("C", l2, null) on the left subtree of the root node; // TreeNode root = new TreeNode ("A", l1, r1); // create the root node BinaryTree bt = new BinaryTree (root); System. out. print ("recursive pre-order traversal ------->"); bt. preOrder (bt. getRoot (); System. out. print ("non-recursive pre-order traversal ------->"); bt. postorderNoRecursion (); System. out. print ("recursive ordinal traversal ------->"); bt. inOrder (bt. getRoot (); System. out. print ("non-recursive middle-order traversal ------->"); bt. inorderNoRecursion (); System. out. print ("recursive post-order traversal ------->"); bt. postOrder (bt. getRoot (); System. out. print ("non-recursive post-order traversal ------->"); bt. postorderNoRecursion ();}}
      
     
    
   
  
 
Recursive pre-order traversal -------> a B d c e non-recursive pre-order traversal -------> D B E C
Recursive middle-order traversal -------> B d a e c Non-recursive middle-order traversal -------> B D A E C
Recursive post-order traversal -------> d B e c a non-recursive post-order traversal -------> D B E C



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.