2021-02-14

二叉树构造以及遍历【C++】

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


using namespace std;

template<class T>
class BTree {
public:
    BTree(/* args */) {
        root_node = nullptr;
    }
    ~BTree() {
        Destory(root_node);
    }

    struct BTNode
    {
        T value;
        BTNode *left, *right;
    };
    typedef BTNode* pBTNode;
    
    
    void Increase(T data);
    void PreOrder(pBTNode bt_root);
    void InOrder(pBTNode bt_root);
    void PostOrder(pBTNode bt_root);
    void LevelOrder(pBTNode bt_root);
    void Destory(pBTNode& bt_root);
    bool FindData(T data, pBTNode current_node);

    pBTNode root_node;  

};

template<class T>
void BTree<T>::Increase(T data) {
    pBTNode new_node = new BTNode;
    new_node->value = data;
    new_node->left = nullptr;
    new_node->right = nullptr;

    if (root_node == nullptr) {
        root_node = new_node;
    } else {
        pBTNode base = nullptr;
        pBTNode current = root_node;

        while (current != nullptr) {
            base = current;

            if(data <= current->value) {
                current = current->left;
            } else {
                current = current->right;
            }
        }

        if (data <= base->value) {
            base->left = new_node;
        } else {
            base->right = new_node;
        }
    }
}

template<class T>
void BTree<T>::PreOrder(pBTNode bt_root) {
    if (bt_root == nullptr) {
        return;
    }

    cout<<bt_root->value<<" ";

    PreOrder(bt_root->left);
    PreOrder(bt_root->right);
}

template<class T>
void BTree<T>::InOrder(pBTNode bt_root) {
    if (bt_root == nullptr) {
        return;
    }

    InOrder(bt_root->left);

    cout<<bt_root->value<<" ";
    
    InOrder(bt_root->right);
}

template<class T>
void BTree<T>::PostOrder(pBTNode bt_root) {
    if (bt_root == nullptr) {
        return;
    }

    PostOrder(bt_root->left);
    PostOrder(bt_root->right);

    cout<<bt_root->value<<" ";
}

template<class T>
void BTree<T>::LevelOrder(pBTNode bt_root) {
    if (bt_root == nullptr) {
        return;
    }

    queue<pBTNode> tmp;
    tmp.push(bt_root);

    while (!tmp.empty())
    {
        cout<<tmp.front()->value<<" ";

        if (tmp.front()->left) {
            tmp.push(tmp.front()->left);
        }
        if (tmp.front()->right) {
            tmp.push(tmp.front()->right);
        }

        tmp.pop();
    }
    

}

template<class T>
void BTree<T>::Destory(pBTNode& bt_root) {
    if (bt_root == nullptr) {
        return;
    }

    Destory(bt_root->left);
    Destory(bt_root->right);

    
    delete bt_root;
    bt_root = nullptr;
}

template<class T>
bool BTree<T>::FindData(T data, pBTNode current_node) {

    if (current_node == nullptr) {
        cout<<"dont find: "<<data<<endl;
        return false;
    } else if (data == current_node->value) {
        cout<<"find: "<<data<<endl;
        return true;
    } else if (data <= current_node->value) {
        FindData(data, current_node->left);
    } else if (data > current_node->value) {
        FindData(data, current_node->right);
    }
}



int main(int argc, char** argv) {

    BTree<int> btree;
    vector<int> aa{3, 6, 2, 11, 21, 8, 0, 7};

    cout<<"init data: ";
    for (auto& e : aa) {
        cout<<e<<" ";
        btree.Increase(e);
    }

    cout<<"\nPreOrder: ";
    btree.PreOrder(btree.root_node);

    cout<<"\nInOrder: ";
    btree.InOrder(btree.root_node);

    cout<<"\nPostOrder: ";
    btree.PostOrder(btree.root_node);

    cout<<"\nLevelOrder: ";
    btree.LevelOrder(btree.root_node);

    cout<<"\n";
    btree.FindData(21, btree.root_node);
    btree.FindData(11, btree.root_node);
    btree.FindData(10, btree.root_node);

    cout<<"\nBtree Destory"<<endl;
    btree.Destory(btree.root_node);

    cout<<"PreOrder: ";
    btree.PreOrder(btree.root_node);
    cout<<endl;

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值