美文网首页
二叉树递归与非递归 - 代码实现

二叉树递归与非递归 - 代码实现

作者: cb_guo | 来源:发表于2019-05-26 10:46 被阅读0次
  • 前序遍历,中序遍历,后序遍历,层次遍历,深度遍历 参考
    深度遍历 本质上就是 前序遍历

C++ 实现

// 2019.5.24 
// 二叉树的递归与非递归遍历

# include <stdio.h>
# include <stdlib.h>
# include <stack>
# include <iostream>
using namespace std;

// 节点信息
struct TreeNode{
    int val;
    struct TreeNode *left;   // 左孩子
    struct TreeNode *right;  // 右孩子
};

// 节点的数值
int node_of_tree[20] = {1, 2, 4, 0, 0, 5, 7, 0, 0, 8, 0, 0, 3, 0, 6, 0, 0}; 

// 创建二叉树树
struct TreeNode *create(struct TreeNode *root, int& x){
    if(node_of_tree[x] == 0){
        root = NULL;
        x += 1;
    }
    else{
        root = (struct TreeNode *)calloc(1, sizeof(struct TreeNode));
        root -> val = node_of_tree[x];
        x += 1;
        root -> left  = create(root -> left,  x);
        root -> right = create(root -> right, x);
    }
    return root;
}

// 前序遍历 -> 递归实现
void qianxu_digui_yes(struct TreeNode *root){
    if(root){
        printf("%d ", root -> val);
        qianxu_digui_yes(root -> left);
        qianxu_digui_yes(root -> right);
    }
}

// 前序遍历 -> 非递归实现
void qianxu_digui_no(struct TreeNode *root){
    if(root == NULL){
        cout << " 二叉树为空 " << endl;
        return;
    }

    stack<TreeNode *> sk;
    TreeNode *p = root;
    while(!sk.empty() || p){
        if(p){
            cout << p->val << " ";
            sk.push(p);
            p = p-> left;
        }
        else{
            p = sk.top();
            sk.pop();
            p = p -> right;
        }
    }
    printf("\n\n");
}

// 中序遍历 -> 递归实现
void zhongxu_digui_yes(struct TreeNode *root){
    if(root){
        zhongxu_digui_yes(root -> left);
        printf("%d ", root -> val);
        zhongxu_digui_yes(root -> right);
    }
}
// 中序遍历 -> 非递归实现
void zhongxu_digui_no(struct TreeNode *root){
    if(root == NULL){
        cout<<" 二叉树为空 " << endl;
        return;
    }

    stack<TreeNode *> sk;
    TreeNode *p = root;
    while(!sk.empty() || p){
        if(p){
            sk.push(p);
            p = p -> left;
        }
        else{
            p = sk.top();
            sk.pop();
            cout << p->val << " ";
            p = p -> right;
        }
    }
    printf("\n\n");
}

// 后序遍历 -> 递归实现
void houxu_digui_yes(struct TreeNode *root){
    if(root){
        houxu_digui_yes(root -> left);
        houxu_digui_yes(root -> right);
        printf("%d ", root -> val);
    }
}
// 后序遍历 -> 非递归实现
void houxu_digui_no(struct TreeNode *root){
    if(root == NULL){
        cout<<" 二叉树为空 " << endl;
        return;
    }

    stack<TreeNode *> sk;
    TreeNode *p, *lastvisit;
    p = root;
    lastvisit = NULL;

    while(p){
        sk.push(p);
        p = p->left;
    }

    while(!sk.empty()){
        p = sk.top();
        sk.pop();

        if(p->right == NULL || p->right == lastvisit){
            cout << p->val << " ";
            lastvisit = p;
        }
        // else if(p->left == lastvisit)
        else{
            sk.push(p);
            p = p -> right;

            while(p){
                sk.push(p);
                p = p -> left;
            }
        }
    }
    printf("\n\n");
}

// 主函数
int main(){
    struct TreeNode *root = NULL;
    int x = 0;
    root = create(root, x);

    printf(" 前序遍历递归实现   ->> ");
    qianxu_digui_yes(root); cout<<endl;
    printf(" 前序遍历非递归实现 ->> ");
    qianxu_digui_no(root);

    printf(" 中序遍历递归实现   ->> ");
    zhongxu_digui_yes(root); cout<<endl;
    printf(" 中序遍历非递归实现 ->> ");
    zhongxu_digui_no(root);

    printf(" 后序遍历递归实现   ->> ");
    houxu_digui_yes(root); cout<<endl;
    printf(" 后序遍历非递归实现 ->> ");
    houxu_digui_no(root);
    return 0;
}

相关文章

  • 算法之二叉树

    二叉树之C++实现 创建二叉树 复制二叉树 先序遍历 递归实现 非递归实现 中序遍历 递归实现 非递归实现 后序遍...

  • 总结

    1、二叉树广度遍历(非递归) 广度遍历非递归实现需要依靠一个队列。 2、二叉树深度遍历(递归与非递归,前序,中序和...

  • 二叉树的遍历实现递归与非递归

    本文实现了二叉树的深度遍历算法,分为递归与非递归 递归的实现非常简单,基本上没啥难度 非递归的实现需要根据遍历的顺...

  • 常用算法

    1.反转二叉树 object-c实现: 非递归方式: ---代码实现

  • 专题:递归与累加阶乘

    递归实现累加和阶乘 累加核心代码: 阶乘的核心代码: 阶乘的非递归实现思路: 阶乘的非递归实现核心代码:

  • 二叉树的前序、中序、后序遍历(递归、非递归)

    二叉树 前序 递归: 非递归: 中序 递归: 非递归: 层序 递归: 非递归:

  • 二叉树,非递归法

    递归法 二叉树的递归,有前序遍历、中序遍历、后序遍历,一般采用递归法,比较简单 非递归法 二叉树非递归法,采用栈来实现

  • 左神算法笔记——Morris遍历

    基本问题——实现二叉树的前序、中序、后序遍历 (递归、非递归,mirros方法) 递归 递归方式下的前中后遍历 非...

  • 二叉树

    结构体 创建二叉树 递归遍历 栈操作 非递归遍历 层次遍历 完整代码

  • 数据结构-树的遍历

    1. 先序遍历 递归实现 非递归实现 2. 中序遍历 递归实现 非递归实现 3. 后序遍历 递归实现 非递归实现 ...

网友评论

      本文标题:二叉树递归与非递归 - 代码实现

      本文链接:https://www.haomeiwen.com/subject/fkdxtctx.html