一个简单二叉树的C++实现(一)
2015-07-18 00:06
721 查看
很久没有接触二叉树了,写这个当作练手,接下来会比较详细地实现二叉树的各个功能及应用。
下面是运行结果:
这个版本是初级版本,遍历采用递归方式。接下来将不断完善!
欢迎交流!
/* * 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 0; }
下面是运行结果:
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$
这个版本是初级版本,遍历采用递归方式。接下来将不断完善!
欢迎交流!
相关文章推荐
- 【多文件操作】使用C++筛选合并多文件的尝试
- C语言编程基础9
- 搬寝室
- 【leetcode c++】82 Remove Duplicates from Sorted List II
- 用户输入M,N值,从1至N开始顺序循环数数,每数到M输出该数值,直至全部输出。写出C程序。
- 【leetcode c++】83 Remove Duplicates from Sorted List
- UVa1593 字符串处理
- 【leetcode c++】80 Remove Duplicates from Sorted Array II
- C++访问控制
- int A[nSize],其中隐藏着若干0,其余非0整数,写一个函数int Func(int* A, int nSize),使A把0移至后面,非0整数移至 数组前面并保持有序,返回值为原数据中第一个
- C语言小例子
- C++继承经典例子
- c/c++存储区分配 堆,栈。。。。
- 算法导论 第十五章:动态规划之棒的切割(Rod Cutting)
- C++模板函数初次使用
- 希望获得一个前辈的腾讯2016校园招聘TST内推---- C++研发
- c++ 之自己写库函数 (静态库)
- C/C++数组名与指针区别深入探索
- C语言指针与二维数组
- c++primer之多维数组