二叉树构造以及遍历【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;
}