一个简单二叉树的C++实现(一)

时间:2021-10-18 05:26:13

很久没有接触二叉树了,写这个当作练手,接下来会比较详细地实现二叉树的各个功能及应用。

/*
* BinaryTree.cpp
* Author: Qiang Xiao
* Time: 2015-07-17
*/ #include<iostream>
#include<string>
using namespace std; template<class ElemType>
class BinaryNode{
public:
ElemType data;
BinaryNode<ElemType>* leftChild;
BinaryNode<ElemType>* rightChild; BinaryNode();
BinaryNode(const ElemType elem, BinaryNode<ElemType>* left = NULL, BinaryNode<ElemType>* right = NULL);
void print() const;
}; template<class ElemType>
void BinaryNode<ElemType>::print() const{
cout << this->data << endl;
} template<class ElemType>
BinaryNode<ElemType>::BinaryNode(){
this->leftChild = NULL;
this->rightChild = NULL;
} template<class ElemType>
BinaryNode<ElemType>::BinaryNode(const ElemType elem, BinaryNode<ElemType>* left, BinaryNode<ElemType>* right){
this->data = elem;
this->leftChild = left;
this->rightChild = right;
} /**************************************************************************************************/
template<class ElemType>
class BinaryTree{
private:
BinaryNode<ElemType>* root;
public:
BinaryTree();
BinaryTree(BinaryNode<ElemType>* r);
// ~BinaryTree();
BinaryNode<ElemType>* getRoot() const;
bool isEmpty() const;
void preOrder(BinaryNode<ElemType>* r) const;
void inOrder(BinaryNode<ElemType>* r) const;
void postOrder(BinaryNode<ElemType>* r) const;
}; template<class ElemType>
BinaryTree<ElemType>::BinaryTree(BinaryNode<ElemType>* r){
root= r;
} template<class ElemType>
BinaryTree<ElemType>::BinaryTree(){
root= new BinaryNode<ElemType>();
} template<class ElemType>
BinaryNode<ElemType>* BinaryTree<ElemType>::getRoot() const{
return root;
} template<class ElemType>
bool BinaryTree<ElemType>::isEmpty() const{
return root== false;
} template<class ElemType>
void BinaryTree<ElemType>::preOrder(BinaryNode<ElemType>* r) const{
if(r== NULL)
return;
cout<<r->data<<"\t";
preOrder(r->leftChild);
preOrder(r->rightChild);
} template<class ElemType>
void BinaryTree<ElemType>::inOrder(BinaryNode<ElemType>* r) const{
if(r== NULL)
return;
inOrder(r->leftChild);
cout<<r->data<<"\t";
inOrder(r->rightChild);
} template<class ElemType>
void BinaryTree<ElemType>::postOrder(BinaryNode<ElemType>* r) const{
if(r== NULL)
return;
postOrder(r->leftChild);
postOrder(r->rightChild);
cout<<r->data<<"\t";
} int main(){
BinaryNode<string>* right1= new BinaryNode<string>("RIGHT1");
BinaryNode<string>* left= new BinaryNode<string>("LEFT", NULL, right1);
BinaryNode<string>* right= new BinaryNode<string>("RIGHT");
BinaryNode<string>* root = new BinaryNode<string>("ROOT", left, right); BinaryTree<string>* tree= new BinaryTree<string>(root);
cout<<"preOrder: \t";
tree->preOrder(root);
cout<<"\ninOrder: \t";
tree->inOrder(root);
cout<<"\npostOrder: \t";
tree->postOrder(root);
cout<<endl; return ;
}

下面是运行结果:

xiaoq@xq-ubun:~/C/DataStructure$ ./BinaryTree.o
preOrder: ROOT LEFT RIGHT1 RIGHT
inOrder: LEFT RIGHT1 ROOT RIGHT
postOrder: RIGHT1 LEFT RIGHT ROOT
xiaoq@xq-ubun:~/C/DataStructure$

这个版本是初级版本,遍历采用递归方式。接下来将不断完善!

欢迎交流!