码迷,mamicode.com
首页 > 其他好文 > 详细

二叉树相关

时间:2020-11-06 01:25:12      阅读:16      评论:0      收藏:0      [点我收藏+]

标签:stack   相关   arc   tree   return   pop   normal   amp   ted   

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

  

二叉树相关

标签:stack   相关   arc   tree   return   pop   normal   amp   ted   

原文地址:https://www.cnblogs.com/LiuBingBlogs/p/13929522.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!