#include <iostream>
#include <stack>
using namespace std;
struct node
{
int data;
struct node* left;
struct node* right;
};
node* newNode(int data)
{
node* node1 = new node;
node1->data = data;
node1->left = NULL;
node1->right = NULL;
return(node1);
}
/* Driver program to test above functions*/
void InorderTraverse(node *root)
{
if(root)
{
InorderTraverse(root->left);
cout << root->data<<" -> ";
InorderTraverse(root->right);
}
}
void InorderTraverse2(node *root)
{
if(root == NULL)
return ;
stack<node *> tmpstack;
while(!tmpstack.empty() || root)
{
while(root)
{
tmpstack.push(root);
root = root->left;
}
if(!tmpstack.empty())
{
node * temp = tmpstack.top();
cout << temp->data;
tmpstack.pop();
root = temp->right;// chage the tree;
}
}
}
void PostOrder1(node * root)
{
if(root == NULL) return;
node * pre = NULL ;
stack<node*> stk;
stk.push(root);
while(!stk.empty())
{
node * cur = stk.top();
if(pre == NULL || pre->left == cur || pre->right == cur)
{
if(cur->left)
{
stk.push(cur->left);
}
else
{
if(cur->right)
stk.push(cur->right);
}
}
else
if(pre == cur->left)
{
if(cur->right)
{
stk.push(cur->right);
}
}
else
{
cout << cur->data<<" -> ";
stk.pop();
}
pre = cur;
}
}
void PostOrder_complex(node * root)
{
if(root == NULL) return;
node * pre = NULL ;
stack<node*> stk;
stk.push(root);
while(!stk.empty())
{
node * cur = stk.top();
if(pre == NULL || pre->left == cur || pre->right == cur)
{// this means the curr's father is visited; so the cur node becomes the
// new father node; we will try to visit its left child if not we will
// visit its right child ; if do not have right child and left child;
// means this is a leaf node; visit it and pop it out ;
if(cur->left)
{
stk.push(cur->left);
}
else
{
if(cur->right)
{
stk.push(cur->right);
}
else
{
cout << cur->data<<" -> ";
stk.pop();
}
}
}
else
if(pre == cur->left)
{// this means cur's left child has just been visited;so we try to visit
// cur's right child;
// if cur do not have right child ;
// then visit cur; pop cur;
if(cur->right)
{
stk.push(cur->right);
}
else
{
cout << cur->data<<" -> ";
stk.pop();
}
}
else
if(pre == cur->right)
// this means the cur's right child has been visited; so the father node //should be visited now; so visit and pop;
{
cout << cur->data<<" -> ";
stk.pop();
}
pre = cur;
}
}
void recursive_postOrder(node * phead)
{
if(phead)
{
recursive_postOrder(phead->left);// new start is phead->left
recursive_postOrder(phead->right);// new start is phead->right
cout << phead->data<<" -> ";
}
}
void pre_oder_non_recursive(node * phead)
{
if(phead == NULL)
return;
stack<node *> stk;
stk.push(phead);
while(!stk.empty())
{
node *ptemp = stk.top();
cout << ptemp->data <<" -> ";
stk.pop();
if(ptemp->right)
{
stk.push(ptemp->right);
}
if(ptemp->left)
{
stk.push(ptemp->left);
}
}
}
void pre_order_recursive(node * phead)
{
if(phead)
{
cout <<phead->data<<" -> ";
pre_order_recursive(phead->left);
pre_order_recursive(phead->right);
}
}
int main()
{
struct node *root = newNode(4);
root->left = newNode(2);
root->right = newNode(5);
root->left->left = newNode(1);
root->left->right = newNode(3);
/*if(isBST(root))
printf("Is BST");
else
printf("Not a BST");
getchar();*/
cout <<endl<< "inorder1"<<endl;
InorderTraverse2(root);
cout <<endl<< "inoder2"<<endl;
InorderTraverse(root);
cout <<endl<<"PostOrder1"<<endl;
PostOrder1(root);
cout <<endl<<"PostOrder_complex"<<endl;
PostOrder_complex(root);
cout <<endl<<"recursive_postOrder"<<endl;
recursive_postOrder(root);
cout << endl<< "pre_order_recursive"<<endl;
pre_order_recursive(root);
cout << endl<<"pre_oder_non_recursive"<<endl;
pre_oder_non_recursive(root);
return 0;
}
wynx - Tree-traverse-review- all kinds of enjoy it !
最新推荐文章于 2022-02-25 17:13:51 发布