wynx - Tree-traverse-review- all kinds of enjoy it !

#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;
}  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值