递归和非递归遍历二叉树


//前序遍历的算法程序  
void PreOrder(BiTNode *root)
{  
    if(root==NULL)  
        return ;  
    printf("%c ", root->data); //输出数据  
    PreOrder(root->lchild); //递归调用,前序遍历左子树  
    PreOrder(root->rchild); //递归调用,前序遍历右子树  
}  

//中序遍历的算法程序  
void InOrder(BiTNode *root)  
{  
    if(root==NULL)
        return ;
    InOrder(root->lchild); //递归调用,前序遍历左子树  
    printf("%c ", root->data); //输出数据  
    InOrder(root->rchild); //递归调用,前序遍历右子树  
}  

//后序遍历的算法程序  
void PostOrder(BiTNode *root)
{
    if(root==NULL)
        return ;
    PostOrder(root->lchild);      //递归调用,前序遍历左子树  
    PostOrder(root->rchild);      //递归调用,前序遍历右子树  
    printf("%c ", root->data);    //输出数据    
}  

/* 
二叉树的非递归前序遍历,前序遍历思想:先让根进栈,只要栈不为空,就可以做弹出操作, 
每次弹出一个结点,记得把它的左右结点都进栈,记得右子树先进栈,这样可以保证右子树在栈中总处于左子树的下面。 
*/  
//recommend
void PreOrder_Nonrecursive(BiTree T)     //先序遍历的非递归    
{  
    if(!T)    
        return ;    

    stack<BiTree> s;  
    s.push(T);  

    while(!s.empty())  
    {  
        BiTree temp = s.top();  
        cout<<temp->data<<" ";  
        s.pop();  
        if(temp->rchild)  
            s.push(temp->rchild);  
        if(temp->lchild)  
            s.push(temp->lchild);  
    }  
}  

//recommend
void InOrderTraverse(BiTree T)   // 中序遍历的非递归    
{    
    if(!T)    
        return ;    
    stack<BiTree> s;    
    BiTree curr = T;    // 指向当前要检查的节点    
    while(curr != NULL || !s.empty())    
    {    
        while(curr != NULL)    // 一直向左走    
        {    
            s.push(curr);    
            curr = curr->lchild;    
        }    
        curr = s.top();    
        s.pop();    
        cout<<curr->data<<"  ";    
        curr = curr->rchild;    
    }    
}    

//recommend
void PostOrder_Nonrecursive1(BiTree T)  // 后序遍历的非递归    
{    
    stack<BiTree> S;    
    BiTree curr = T ;           // 指向当前要检查的节点  
    BiTree previsited = NULL;    // 指向前一个被访问的节点  
    while(curr != NULL || !S.empty())  // 栈空时结束    
    {    
        while(curr != NULL)            // 一直向左走直到为空  
        {    
            S.push(curr);    
            curr = curr->lchild;    
        }    
        curr = S.top();  
        // 当前节点的右孩子如果为空或者已经被访问,则访问当前节点  
        if(curr->rchild == NULL || curr->rchild == previsited)    
        {    
            cout<<curr->data<<"  ";    
            previsited = curr;    
            S.pop();    
            curr = NULL;    
        }    
        else  
            curr = curr->rchild;      // 否则访问右孩子  
    }    
}   

void LeverTraverse(BiTree T)   //方法一、非递归层次遍历二叉树   
{  
    queue <BiTree> Q;  
    BiTree p;  
    p = T;  
    if(p)  
        Q.push(p);  
    while(!Q.empty())  
    {  
        p = Q.front();  
        Q.pop();  
        printf("%c ",T->data);  
        if(p->lchild)   
            Q.push(p->lchild);  
        if(p->rchild)  
            Q.push(p->rchild);  
    }  
}  

int depth(BiTNode *T)   //树的深度  
{  
    if(!T)  
        return 0;  
    int d1,d2;  
    d1=depth(T->lchild);  
    d2=depth(T->rchild);  
    return (d1>d2?d1:d2)+1;  
    //return (depth(T->lchild)>depth(T->rchild)?depth(T->lchild):depth(T->rchild))+1;  
}  

int CountNode(BiTNode *T)  
{  
    if(T == NULL)  
        return 0;  
    return 1+CountNode(T->lchild)+CountNode(T->rchild);  
}  

void PreOrder_Nonrecursive1(BiTree T)     //先序遍历的非递归 
{
    if(!T)  
        return ;
    stack<BiTree> s;
    BiTree curr = T;
    while(curr != NULL || !s.empty())
    {
        while(curr != NULL)
        {
            cout<<curr->data<<"  ";
            s.push(curr);
            curr = curr->lchild;
        }
        if(!s.empty())
        {
            curr = s.top();
            s.pop();
            curr = curr->rchild;
        }
    }
}

void InOrderTraverse1(BiTree T)   // 中序遍历的非递归  
{  
    if(!T)  
        return ;  
    BiTree curr = T;    // 指向当前要检查的节点  
    stack<BiTree> s;
    while(curr != NULL || !s.empty())
    {
        while(curr != NULL)
        {
            s.push(curr);
            curr = curr->lchild;
        }//while
        if(!s.empty())
        {
            curr = s.top();
            s.pop();
            cout<<curr->data<<"  ";
            curr = curr->rchild;
        }
    }
}

//转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/6583988

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
遍历二叉树有三种方式,分别是前序遍历、中序遍历和后序遍历。其中,前序遍历指先遍历根节点,再遍历左子树和右子树;中序遍历指先遍历左子树,再遍历根节点和右子树;后序遍历指先遍历左子树和右子树,再遍历根节点。下面是使用递归非递归方式遍历二叉树的示例代码: ```c++ #include <iostream> #include <stack> using namespace std; // 二叉树结点的定义 struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; // 递归前序遍历 void preorderTraversalRecursive(TreeNode* root) { if (root == NULL) return; cout << root->val << " "; preorderTraversalRecursive(root->left); preorderTraversalRecursive(root->right); } // 非递归前序遍历 void preorderTraversalIterative(TreeNode* root) { if (root == NULL) return; stack<TreeNode*> s; s.push(root); while (!s.empty()) { TreeNode* node = s.top(); s.pop(); cout << node->val << " "; if (node->right != NULL) s.push(node->right); if (node->left != NULL) s.push(node->left); } } // 递归中序遍历 void inorderTraversalRecursive(TreeNode* root) { if (root == NULL) return; inorderTraversalRecursive(root->left); cout << root->val << " "; inorderTraversalRecursive(root->right); } // 非递归中序遍历 void inorderTraversalIterative(TreeNode* root) { if (root == NULL) return; stack<TreeNode*> s; TreeNode* node = root; while (node != NULL || !s.empty()) { while (node != NULL) { s.push(node); node = node->left; } node = s.top(); s.pop(); cout << node->val << " "; node = node->right; } } // 递归后序遍历 void postorderTraversalRecursive(TreeNode* root) { if (root == NULL) return; postorderTraversalRecursive(root->left); postorderTraversalRecursive(root->right); cout << root->val << " "; } // 非递归后序遍历 void postorderTraversalIterative(TreeNode* root) { if (root == NULL) return; stack<TreeNode*> s1, s2; s1.push(root); while (!s1.empty()) { TreeNode* node = s1.top(); s1.pop(); s2.push(node); if (node->left != NULL) s1.push(node->left); if (node->right != NULL) s1.push(node->right); } while (!s2.empty()) { cout << s2.top()->val << " "; s2.pop(); } } int main() { // 构造二叉树 TreeNode* root = new TreeNode(1); root->left = new TreeNode(2); root->right = new TreeNode(3); root->left->left = new TreeNode(4); root->left->right = new TreeNode(5); root->right->left = new TreeNode(6); root->right->right = new TreeNode(7); // 递归遍历 cout << "递归前序遍历:"; preorderTraversalRecursive(root); cout << endl; cout << "递归中序遍历:"; inorderTraversalRecursive(root); cout << endl; cout << "递归后序遍历:"; postorderTraversalRecursive(root); cout << endl; // 非递归遍历 cout << "非递归前序遍历:"; preorderTraversalIterative(root); cout << endl; cout << "非递归中序遍历:"; inorderTraversalIterative(root); cout << endl; cout << "非递归后序遍历:"; postorderTraversalIterative(root); cout << endl; return 0; } ``` 注意,非递归方式需要使用栈来存储待遍历的结点,同时也需要注意遍历顺序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值