Non-recursive implementation of binary tree traversal of Java data structure

Source: Internet
Author: User

Algorithm Overview
Recursive algorithm is concise and readable, but it consumes more time and storage space than non-recursive algorithm. To improve the efficiency, we can adopt a non-recursive two-fork tree traversal algorithm. Non-recursive implementations are implemented with stacks, because the stack's advanced post-out structure is similar to recursion.
For the middle sequence traversal, the non-recursive algorithm is much more efficient than the recursive algorithm. The process of implementing the sequential traversal algorithm is as follows:
(1). Initialize the stack, the root node into the stack;
(2). If the stack is non-empty, then the left child node of the top node of the stack is successively pushed into the stack until null (to the leaf node), and the top node of the stack is accessed (performing the visit operation) and the right child node of the top node of the stack becomes the stack top node.
(3). Repeat Execution (2) until the stack is empty.
Algorithm Implementation
Package datastructure.tree;

Import Datastructure.stack.ArrayStack;
Import Datastructure.stack.Stack;

public class Unrecorderbtree implements visit{
Private stack stack = new Arraystack ();
Private BTree BT;
@Override
public void Visit (BTree BTree) {
System.out.print ("\ T" + btree.getrootdata ());
}

public void Inorder (BTree boot) {
Stack.clear ();
Stack.push (boot);
while (!stack.isempty ()) {
Left child node into the stack.
while (BT = ((BTree) (Stack.peek ())). Getleftchild ()) = null) {
Stack.push (BT);
}
If the node doesn't have a right child, step up the stack.
while (!stack.isempty () &&! ( (BTree) Stack.peek ()). Hasrighttree ()) {
BT = (BTree) stack.pop ();
Visit (BT);
}
If the node has a right child, then the right child enters the stack.
if (!stack.isempty () && (BTree) Stack.peek ()). Hasrighttree ()) {
BT = (BTree) stack.pop ();
Visit (BT);
Stack.push (Bt.getrightchild ());
}
}
}

}
Test:


Package datastructure.tree;
/**
* Test binary Tree
* @author Administrator
*
*/
public class Btreetest {
public static void Main (String args[]) {
BTree BTree = new Linkbtree (' A ');
BTree bt1, BT2, BT3, BT4;
BT1 = new Linkbtree (' B ');
Btree.addlefttree (BT1);
BT2 = new Linkbtree (' D ');
Bt1.addlefttree (BT2);

BT3 = new Linkbtree (' C ');
Btree.addrighttree (BT3);
BT4 = new Linkbtree (' E ');
Bt3.addlefttree (BT4);
BT4 = new Linkbtree (' F ');
Bt3.addrighttree (BT4);

Recursionorderbtree order = new Recursionorderbtree ();
System.out.println ("\ n sequence traversal:");
Order.inorder (Btree);

}
}

The results are as follows:
Middle Sequence Traversal:
D B A E
C F

Reprint to: http://blog.csdn.net/luoweifu/article/details/9079799

Non-recursive implementation of binary tree traversal of Java data structure

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.