zoukankan      html  css  js  c++  java
  • 二叉树的遍历

    前面几种遍历方法比较简单,说明一下最后一种

    1. 如果路径上前一个节点是父节点,则往左孩子方向走

    2. 如果路径上前一个节点是左孩子,则往右孩子方向走

    3. 如果路径上前一个节点是右孩子,则往父节点方向走

    处理下孩子缺失的情况

    1. 如果只有一个右孩子且从父节点过来,往右孩子方向走  (有右无左)

    2. 如果从左孩子过来且无右孩子,则往父节点方向走        (有左无右)

    3. 如果没有孩子节点,则往父节点走                            (无左无右)

    #include <iostream>
    #include <stack>
    using namespace std;
    struct Tree{
        Tree *parent;
        Tree *left;
        Tree *right;
        int key;
    };
    
    //递归方式 先序 中序 后序
    //先序
    void Ergodic_P(Tree *tree){
        if (!tree)
            return;
        cout<<tree->key;
        Ergodic_P(tree->left);
        Ergodic_P(tree->right);
    }
    //中序
    void Ergodic_M(Tree *tree){
        if (!tree)
            return;
        
        Ergodic_M(tree->left);
        cout << tree->key;
        Ergodic_M(tree->right);
    }
    //后序
    void Ergodic_S(Tree *tree){
        if (!tree)
            return;
        Ergodic_S(tree->left);
        Ergodic_S(tree->right);
        cout << tree->key;
    }
    
    //非递归方式
    //借用栈
    void Ergodic_Stack(Tree *tree){
        stack<Tree *> sta;
        sta.push(tree);
        while (!sta.empty()){
            tree = sta.top();
            sta.pop();
            cout << tree->key;
            if (tree->left)
                sta.push(tree->left);
            if (tree->right)
                sta.push(tree->right);
        }
    }
    
    //非常巧妙的一种方法
    
    void Ergodic_Tricky(Tree *tree){
        Tree *prev = 0;
        while (tree) {
            if (prev == tree->parent) {
                cout << tree->key;
                prev = tree;
                tree = tree->left ? tree->left :
                    tree->right ? tree->right :
                    tree->parent;
            }
            else if (prev == tree->left && tree->right) {
                prev = tree;
                tree = tree->right;
            }
            else {
                prev = tree;
                tree = tree->parent;
            }
        }
    
    }
    
    int main(){
        Tree leaf_1{0,0,0,1};
        Tree leaf_2{ 0, 0, 0, 2 };
        Tree leaf_3{ 0, 0, 0, 3 };
        Tree leaf_4{ 0, 0, 0, 4 };
        Tree tree_1{ 0, &leaf_1, &leaf_2, 5 };
        Tree tree_2{ 0, &leaf_3, &leaf_4, 6 };
        Tree root{ 0, &tree_1, &tree_2, 7 };
        leaf_1.parent = &tree_1;
        leaf_2.parent = &tree_1;
        leaf_3.parent = &tree_2;
        leaf_4.parent = &tree_2;
        tree_1.parent = &root;
        tree_2.parent = &root;
    
        Ergodic_P(&root);
        cout << endl;
        Ergodic_P(&root);
        cout << endl;
        Ergodic_S(&root);
        cout << endl;
        Ergodic_Stack(&root);
        cout << endl;
        Ergodic_Tricky(&root);
    
    }
  • 相关阅读:
    MD5算法--网盘秒传
    无线网络定位算法综述
    android学习---异步任务(AsyncTask)
    python中局部变量的定义
    python3里函数怎么样使用元组或字典作为参数调用(复制他人博客)
    调试exynos4412—ARM嵌入式Linux—LEDS/GPIO驱动之一
    Linux入门之——安装虚拟机软件
    Linux学习方法之以始为终—Linux工作分类
    Linux基础系列—Linux内核源码目录结构
    Linux基础系列—Linux体系结构和Linux内核结构
  • 原文地址:https://www.cnblogs.com/Nastukashii/p/4420497.html
Copyright © 2011-2022 走看看