Recursive completion of the tree traversal is very well understood, if it is a non-recursive. Don't tell me about the algorithm introduction, I want to make the thinking process of maker
Since recursion can be achieved, it simulates recursion.
The essence of recursion is the compression stack.
First simple tree. Observing the recursive stack process
A, b even if the node's data also represents the address of the node.
Use recursion to complete the pre-order creation of this tree
#include <iostream>using namespace STD;structTreeNodetypedef structtreenode* TREE;structtreenode{CharData TREE left; TREE right;};voidTree_front (tree& T) {CharBuffcout<<"Please input, or NULL (*)"<<endl;Cin>>buff;if(buff!=' * ') {t=New structTreeNodeif(T==null)Exit(-1); t->data=buff; Tree_front (T->left); Tree_front (T->right); }ElseT=null;}intMain () {TREE T; Tree_front (T);return 0;}
Tree set up
So, look at the call procedure for this stack.
Use vs2008 's disassembly form to see
1. Enter main to call the function for the first time
Tree_front (T);
0084162E Lea Eax,[t]
00841631 push EAX
00841632 call Tree_front (8410b4h)//function 1
00841637 Add esp,4
Pass the address of T to the function
2. After entering function 1 for the first time
Enter a, before calling its own function
Tree_front (T->left);
0084156D mov eax,dword ptr [T]
00841570 mov ecx,dword ptr [eax]
00841572 Add ecx,4
00841575 push ECX
00841576 call Tree_front (8410b4h)//function 2
0084157B Add esp,4
Press the A address stack
3. Enter recursive call function 2
Enter B. Before calling its own function
Tree_front (T->left);
0084156D mov eax,dword ptr [T]
00841570 mov ecx,dword ptr [eax]
00841572 Add ecx,4
00841575 push ECX
00841576 call Tree_front (8410b4h)//function 3
0084157B Add esp,4
Put B pressure Stack
4. Enter recursive call function 3
Enter x, which indicates input null
T=null;
00841591 mov eax,dword ptr [T]
00841594 mov dword ptr [eax],0
Return to the place where function 3 is called
Run the next instruction
0084157B Add esp,4
is out of the stack, will b out of the stack
Ready to enter right subtree call function
5. Enter recursive right subtree call function
Tree_front (T->right);
0084157E mov eax,dword ptr [T]
00841581 mov ecx,dword ptr [eax]
00841583 Add ecx,8
00841586 push ECX
00841587 call Tree_front (8410b4h)//function 4
0084158C Add esp,4
At this point, the B pressure stack
6. Enter Recursive call function 4
Enter x, which indicates input null
T=null;
00841591 mov eax,dword ptr [T]
00841594 mov dword ptr [eax],0
Return to function 4, run the next instruction
0084158C Add esp,4
b out of the stack, at this point, will come out of function 2, into the function 2 of the next right subtree call
7. Call into the next right subtree of function 2
Enter X. Run
T=null;
00841591 mov eax,dword ptr [T]
00841594 mov dword ptr [eax],0
Then exit the function, run the next instruction,
。
。。。
Know that the end is complete
Use a call graph to represent
Analyze the situation of this stack
Either node A or Node B. When you enter the left subtree, the address of the node is saved so that you can return
So you can use a vector container to simulate this press-in process.
Initial some variables
struct TreeNode;
typedef struct TREENODE* TREE;
struct TreeNode
{
char data;
TREE left;
TREE right;
};
The pseudo-code of the original non-recursive pre-order traversal is
void preorder (TREE T)
{
while (1)
{
Interview Node T.
Press Stack T.
T=t->left (T becomes Zuozi)
}
}
Complete A to B traversal.
B's Do node is null at this point, should pop out b node
Then use the right subtree of B as the new node
Then change the pseudo-code
void preorder (TREE T)
{
while (1)
{
if (t!=null)
{
Interview node T;
Pressure stack t;
T=t->left (T becomes Zuozi)
}
Else
{
T= out the stack;
t=t->right;
}
}
}
Out of the stack is now traversing to the left sub-tree is null, according to the latest stack node, switch to the right subtree
Returns the parent tree of the parent tree when the right subtree is null. Then it becomes the right sub-tree of the parent tree.
Again, while the end condition of the while, in the observation diagram, the right subtree of a is returned. Traversal ends. At this point the stack no longer includes a, B nodes,
Change Pseudo-code
void preorder (TREE T)
{
Initialize the stack;
while (stack is not empty)
{
if (t!=null)
{
Interview node T;
Press Stack T.
T=t->left (T becomes Zuozi)
}
Else
{
T= out the stack;
t=t->right;
}
}
}
The condition still has a bug, when a Zuozi returns, the stack is empty. But at this point, we're going to go through a right subtree.
Look at the stack when the whole tree is traversing.
The stack has three places, so add a condition
Here are the variables are nodes, so the last time the stack empty the corresponding node situation
Effective-----Null (last right sub-tree)
And to exit is when the last stack is empty, based on logical inference. Both phases
Change Pseudo-code
void preorder (TREE T)
{
Initializes the stack.
while (Stack not empty | | node is NOT NULL)
{
if (t!=null)
{
Interview node T;
Pressure stack t;
T=t->left (T becomes Zuozi)
}
Else
{
T= out the stack;
t=t->right;
}
}
}
So the code is implemented as
voidvector <TREE>stack//vector <char> view;whilestack.size()!=0) { if(T!=NULL) { cout<<T->data<<endl; stack.push_back(T); //view.push_back(T->data); T=T->left; } else { T=stack.back(); stack.pop_back(); // view.pop_back(); T = T->right; } }}
The entire test code is (including recursive traversal.) As a comparison)
#include <iostream>#include <vector>using namespace STD;structTreeNodetypedef structtreenode* TREE;structtreenode{CharData TREE left; TREE right;};voidTree_front (tree& T) {CharBuffcout<<"Please input, or NULL (*)"<<endl;Cin>>buff;if(buff!=' * ') {t=New structTreeNodeif(T==null)Exit(-1); t->data=buff; Tree_front (T->left); Tree_front (T->right); }ElseT=null;}voidPreorder (TREE T) { vector <TREE> Stack; vector <char>View while(T!=null | |Stack. Size ()! =0) {if(T!=null) {cout<<T->data<<endl;Stack. push_back (T); View.push_back (T->data); t=t->left; }Else{t=Stack. back ();Stack. Pop_back (); View.pop_back (); T = t->right; } }}voidPre_compare (TREE T) {if(T!=null) {cout<<T->data<<endl; Pre_compare (T->left); Pre_compare (T->right); }}intMain () {TREE T; Tree_front (t); preorder (t);cout<<"Follow is to compare"<<endl; Pre_compare (T);return 0;}
Constructing process of reproducing binary tree non-recursion algorithm