Recursive, non-recursive algorithm for traversing binary tree (front, middle, and rear) sequence!

Source: Internet
Author: User

Go to: http://blog.csdn.net/alex44667416/article/details/4723991

Package tree;

Import Java.util.Stack;

Binary tree Node
Class Btnode {
Private char key;
Private Btnode left, right;
Public Btnode (char key) {//Create a new node with a value of key
This (key, NULL, NULL);
}
Public Btnode (Char Key, Btnode left, Btnode right) {
This.key = key; Create a new node and specify its left and right nodes.
This.left = left;
This.right = right;
}
Public Char GetKey () {//Returns the value of the node
Return key;
}
public void Setkey (char key) {//Set the value of the node
This.key = key;
}
Public Btnode GetLeft () {//return its left child node
return left;
}
Public Btnode GetRight () {//return to its right child node
return right;
}
public void Setleft (Btnode left) {//Set the child node
This.left = left;
}
public void Setright (Btnode right) {//Set the child nodes
This.right = right;
}
}
Binary Tree Traversal
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; Returns the root node.
}
Access node
public static void Visit (Btnode p) {
System.out.print (P.getkey () + "");
}
Recursive implementation of pre-order traversal
protected static void Preorder (Btnode p) {
if (P! = null) {
Visit (p);
Preorder (P.getleft ());
Preorder (P.getright ());
}
}
Recursive implementation of sequential traversal
protected static void Inorder (Btnode p) {
if (P! = null) {
Inorder (P.getleft ());
Visit (p);
Inorder (P.getright ());
}
}
Recursive implementation of sequential 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<btnode> stack = new stack<btnode> ();
if (p!=null) {
Stack.push (P);
while (!stack.empty ()) {
p = Stack.pop ();
Visit (p);
Right sub-node advanced stack, left dial hand node again into the stack, so first access to the left Dial hand node
if (p.getright () = null)
Stack.push (P.getright ());
if (p.getleft () = null)
Stack.push (P.getleft ());
}
}
}
/* Non-recursive implementation of sequential traversal
* In addition to the leftmost left dial hand node, all left dial hand nodes are successively stacked (a cyclic process).
* At this point P points to the leftmost left Dial hand node,
* Repeat: If P does not have a right sub-node (or P's right child node has been output), then output p, at the same time out of the stack, assign the value to P
* If P has a right sub-node, p is put into the stack, and P points to its right child node.
* Repeat the above steps until P is empty
*/
protected static void Iterativepostorder (Btnode p) {
Btnode q = p;
stack<btnode> stack = new stack<btnode> ();
while (P!=null) {
All of the left dial hand nodes are sequentially in the stack, except for the last one.
while (P.getleft ()!=null) {
Stack.push (P);
p = p.getleft ();
}
The current node has no right sub-nodes or right child nodes that have been output.
while (P.getright () ==null| | P.getright () ==q) {
Visit (p);
Q = p;//record the previous output node
if (Stack.empty ())
Return
p = Stack.pop ();
}
Dealing with right child nodes
Stack.push (P);//If the while loop above is not executed, then the left-most left dial hand node is in the stack.
p = p.getright ();//The Right child node of p here must not be empty.
}
}
Non-recursive implementation of sequential traversal
protected static void Iterativeinorder (Btnode p) {
stack<btnode> stack = new stack<btnode> ();
while (p!= null) {
while (P!=null) {
if (P.getright ()!=null)
Stack.push (P.getright ());//Current right child node into the stack
Stack.push (P);//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-sequence Traversal:");
Preorder (Tree.getroot ());
System.out.print ("/n in-sequence traversal:");
Inorder (Tree.getroot ());
System.out.print ("/N post-traversal:");
Postorder (Tree.getroot ());
The following is a non-recursive traversal of a binary tree
System.out.print ("/n/n Pre-sequence traversal:");
Iterativepreorder (Tree.getroot ());
System.out.print ("/n in-sequence traversal:");
Iterativeinorder (Tree.getroot ());
System.out.print ("/N post-traversal:");
Iterativepostorder (Tree.getroot ());
}
}

(front, middle, and back) sequence traversal binary tree recursive, non-recursive algorithm!

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.