二叉树相关

Posted liubingblogs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二叉树相关相关的知识,希望对你有一定的参考价值。

#include <iostream>
#include <vector>
#include <stack>
#include <queue>

template <class T>
typedef struct node {
    node* left;
    node* right;
    T val;
    std::string ext;
    node(T val) : left(nullptr), right(nullptr), val(val), ext(""){}
};
typedef node<int> Node;
Node* creat_normal_tree() {
    Node* pRoot = new Node(5);
    pRoot->left = new Node(3);
    pRoot->left->left = new Node(1);
    pRoot->left->right = new Node(4);
    pRoot->right = new Node(8);
    pRoot->right->left = new Node(7);
    pRoot->right->right = new Node(9);
    return pRoot;
}
Node* creat_normal_tree2() {
    Node* pRoot = new Node(20);
    pRoot->left = new Node(15);
    pRoot->left->left = new Node(14);
    pRoot->left->right = new Node(16);
    pRoot->right = new Node(25);
    pRoot->right->left = new Node(22);
    pRoot->right->right = new Node(299);
    return pRoot;
}

/*************************Search*****************************/
// pre
void pre_order_r(Node* pRoot) {
    if (nullptr == pRoot) {
        return;
    }
    std::cout << pRoot->val << ",";
    pre_order_r(pRoot->left);
    pre_order_r(pRoot->right);
}
void pre_order_stack(Node* pRoot) {
    if (nullptr == pRoot) {
        return;
    }
    Node* pCur = pRoot;
    std::stack<Node*> helper;
    helper.push(pCur);
    while (!helper.empty()) {
        Node* pTmp = helper.top();
        helper.pop();
        std::cout << pTmp->val << ",";
        if (pTmp->right) {
            helper.push(pTmp->right);
        }
        if (pTmp->left) {
            helper.push(pTmp->left);
        }
    }

}
// in
void in_order_r(Node* pRoot) {
    if (nullptr == pRoot) {
        return;
    }
    in_order_r(pRoot->left);
    std::cout << pRoot->val << ",";
    in_order_r(pRoot->right);
}
void in_order_stack(Node* pRoot) {
    if (nullptr == pRoot) {
        return ;
    }
    std::stack<Node*> helper;
    Node* pCur = pRoot;
    while (pCur || !helper.empty()) {
        if (pCur) {
            helper.push(pCur);
            pCur = pCur->left;
        } else {
            Node* pTop = helper.top();
            helper.pop();
            std::cout << pTop->val << ",";
            pCur = pTop->right;
        }
    }
}
// post
void post_order_r(Node* pRoot) {
    if (nullptr == pRoot) {
        return;
    }
    post_order_r(pRoot->left);
    post_order_r(pRoot->right);
    std::cout << pRoot->val << ",";
}
void post_order_stack(Node* pRoot) {
    if (nullptr == pRoot) {
        return;
    }
    std::stack<Node*> helper;
    Node* pCur = pRoot;
    while (pCur || !helper.empty()) {
        if (pCur) {
            helper.push(pCur);
            pCur = pCur->left;
        } else {
            Node* pTop = helper.top();
            if (std::strcmp(pTop->ext.c_str(), "visited") == 0) {
                std::cout << pTop->val << ",";
                helper.pop();
            } else {
                pTop->ext = "visited";
                pCur = pTop->right;
            }

        }
    }
}
/*************************Search end*****************************/


/*************************Merge two bsts start*****************************/
// step 1 get in order arrs
void get_order_arr(Node* pRoot, std::vector<int> &out) {
    if (nullptr == pRoot) {
        return;
    }
    get_order_arr(pRoot->left, out);
    out.push_back(pRoot->val);
    get_order_arr(pRoot->right, out);
}
// step 2 insert node into bst
Node* insert_node_into_bst(Node* pRoot, int val) {
    if (nullptr == pRoot) {
        pRoot = new Node(val);
        return pRoot;
    }
    // val != pRoot->val in bst
    if (pRoot->val < val) {
        pRoot->right = insert_node_into_bst(pRoot->right, val);
    }
    if (pRoot->val > val) {
        pRoot->left = insert_node_into_bst(pRoot->left, val);
    }
    return pRoot;
}
// step 3 Merge
Node* merge_bsts(Node* pRoot1, Node* pRoot2) {
    // special cases
    if (nullptr == pRoot1 && nullptr == pRoot2) {
        return nullptr;
    }
    if (nullptr == pRoot1) {
        return pRoot2;
    }
    if (nullptr == pRoot2) {
        return pRoot1;
    }

    // in order arr
    std::vector<int> helper;
    get_order_arr(pRoot1, helper);

    // insert into tree2
    for (auto itr:helper) {
        insert_node_into_bst(pRoot2, itr);
    }
    return pRoot2;
}
/*************************Merge two bsts End*****************************/

int main() {
    std::cout << "Hello, World!" << std::endl;
    Node* pRoot = creat_normal_tree();
    in_order_r(pRoot);
    std::cout <<  std::endl;

    Node* pRoot1 =  creat_normal_tree2();
    in_order_r(pRoot1);
    std::cout <<  std::endl;

    Node * pRoot2 = merge_bsts(pRoot, pRoot1);
    in_order_r(pRoot2);
    std::cout <<  std::endl;

    return 0;
}

  

以上是关于二叉树相关的主要内容,如果未能解决你的问题,请参考以下文章

二叉树相关代码记录

数据结构(C语言版)严蔚敏---二叉树遍历操作二叉树的相关代码

数据结构(C语言版)严蔚敏---二叉树遍历操作二叉树的相关代码

简单二叉树相关代码

不再惧怕!二叉树结构相关算法总结 | 原力计划

Java 链表二叉树集合的相关总结(附代码)