Java language implementation of non-recursive algorithm and recursive algorithm for hierarchical traversal of two-fork tree

Source: Internet
Author: User

/**  two fork tree node  */public class btnode {private char key;  private btnode left, right;  Public btnode (Char key)  {this (key, null, null); } public btnode (char key, btnode left, btnode right)  {this.key =     Key    this.left = left;  this.right = right;  } public char getkey ()  {return key;  } public void setkey (Char key)  {this.key = key;  } public btnode getleft ()  {return left;  } public void setleft (Btnode left)  {this.left = left;  } public btnode getright ()  {return right;  } public void setright (btnode right)  {this.right = right;  }}/**  two fork tree traverse  */public class bintree {protected btnode root; Public bintree (btnode root)  {this.root = root;  } public btnode getroot ()  {return root; }/**  Construction Tree  */public static btnode init ()  {btnode a = new     Btnode (' A ');    Btnode b = new btnode (' B ',  null, a);    Btnode c = new btnode (' C ');    Btnode d = new btnode (' d ',  b, c);    Btnode e = new btnode (' e ');    Btnode f = new btnode (' F ',  e, null);    Btnode g = new btnode (' G ',  null, f);    Btnode h = new btnode (' h ',  d, g);    Return h;// root}/**  Access node  */public static void visit (BTNode p)  {  System.out.print (P.getkey ()  +  " "); }/**  recursive implementation of the pre-sequence traversal  */protected static void preorder (btnode p)  {if  (p !  = null)  {visit (p);    Preorder (P.getleft ());    Preorder (P.getright ()); }}/**  recursive implementation of the sequence traversal  */protected static void inorder (btnode p)  {if  (p !      = null)  {inorder (P.getleft ());      Visit (p);    Inorder (P.getright ()); }}/**  recursive implementation of the post-traversal  */Protected static void postorder (btnode p)  {if  (p       != null)  {Postorder (P.getleft ());      Postorder (P.getright ());    Visit (p); }}/**  non-recursive implementation of pre-sequence traversal  */protected static void iterativepreorder (btnode p)  {Stack& Lt    Btnode> stack = new stack<btnode> ();      if  (p != null)  {Stack.push (p);        while  (!stack.empty ())  {p = stack.pop ();        Visit (p);        if  (P.getright ()  != null) Stack.push (P.getright ());      if  (P.getleft ()  != null) Stack.push (P.getleft ());}}}/**  non-recursive implementation of post-traversal  */Protected static void iterativepostorder (btnode p)  {BT    node q = p;    Stack<btnode> stack = new stack<btnode> (); while  (p != null)  {//  left dial hand tree into the stack for  (;  p.getleft ()  != null; p&nbs      P;= p.getleft ()) Stack.push (p);   Current node No right child or right child has output while  (p != null &&  (P.getright ()  == null  | |         p.getright ()  == q))  {visit (p);        q = p;//  records the last output node if  (Stack.empty ()) return;      P = stack.pop ();      }//  Processing Right sub-stack.push (p);    P = p.getright (); }}/**  non-recursive implementation of the sequence traversal  */protected static void iterativeinorder (btnode p)  {Stack& Lt    Btnode> stack = new stack<btnode> (); while  (P&NBSP;!=&NBsp;null)  {while  (p != null)  {if  (P.getright ()  != null) STA      Ck.push (P.getright ());//  The current node right child into the stack Stack.push (p);//  the current node into the stack p = p.getleft ();      } p = stack.pop ();        while  (!stack.empty ()  && p.getright ()  == null)  {visit (p);      P = stack.pop ();      } visit (P);      if  (!stack.empty ()) P = stack.pop ();    else p = null; }} public static void main (String[] args)  {bintree tree = new     Bintree (init ());    System.out.print (" pre-order:");    Preorder (Tree.getroot ());    System.out.println ();    System.out.print ("In-order:");    Inorder (Tree.getroot ());    System.out.println ();    System.out.print ("Post-order:");    Postorder (Tree.getroot ());    System.out.println ();    System.out.print (" pre-order:"); IterativepreOrder (Tree.getroot ());    System.out.println ();    System.out.print ("In-order:");    Iterativeinorder (Tree.getroot ());    System.out.println ();    System.out.print ("Post-order:");    Iterativepostorder (Tree.getroot ());  System.out.println (); }}
/* Traversal is one of the most basic operations on trees, the so-called traversal of the binary tree, that is, according to certain rules and order all the nodes of the binary tree, so that each node is visited once, and only be visited once.  Because the binary tree is a nonlinear structure, the traversal of the tree is essentially the transformation of each node of the two-fork tree into a linear sequence to be represented.  Set L, D, R to Traverse Zuozi, access the root node and traverse right subtree, there are three cases of traversal of a binary tree: The DLR (called the root sequence traversal), LDR (known as the root sequence traversal), LRD (called the post-root sequence traversal). (1) The first order traversal accesses the root; the left subtree is traversed in the first order, and the right subtree (2) is traversed by the sequence traversal to traverse the left subtree by the middle sequence; to access the root; to traverse the right sub-tree by the middle order (3) The sequential traversal traverses the left sub-tree sequentially, and the right subtree is traversed by order; access to the root (4) hierarchy Access the root, access the child, and then access the child's children (the lower the hierarchy) (two children of the same level) */

Java language implementation of non-recursive algorithm and recursive algorithm for hierarchical traversal of two-fork tree

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.