主函数文件
- #pragma once
- #include "BinaryTree.h"
- int main(){
- BinaryTree<int> a;
- a.build( a.getDRoot());
- cout<<"a.levelOrder(a.getRoot() );"<<endl;
- a.levelOrder(a.getRoot() ); //right!
- cout<<"a.postOrder(a.getRoot());"<<endl;
- a.postOrder(a.getRoot());
- cout<<endl;
- cout<<"a.inOrder(a.getRoot());"<<endl;
- a.inOrder(a.getRoot());
- cout<<"a.preOrder(a.getRoot());"<<endl;
- a.preOrder(a.getRoot());
- cout<<"a.deleteBinaryTree(a.getRoot()->getLeftChild());"<<endl;
- a.deleteBinaryTree(a.getRoot()->getLeftChild());
- a.levelOrder(a.getRoot() );
- exit(0);
- }
BinaryTreeNote.h
- BinaryTreeNote<T>* BinaryTreeNote<T>::getLeftChild(){
- return this->left;
- }
- template<class T>
- BinaryTreeNote<T>* BinaryTreeNote<T>::getRightChile(){
- return this->right;
- }
- template<class T>
- T BinaryTreeNote<T>::getValue()const{
- return this->data;
- }
- template<class T>
- bool BinaryTreeNote<T>::isLeaf()const {
- return ! (this->left||this->right);
- }
- template<class T>
- void BinaryTreeNote<T>::setLeftChild(BinaryTreeNote* l ){
- this->left=l;
- }
- template<class T>
- void BinaryTreeNote<T>::setRightChild(BinaryTreeNote* r ){
- this->right=r;
- }
- template<class T>
- BinaryTreeNote<T>::~BinaryTreeNote(){
- }
BinaryTree.h
- #pragma once
- #include<iostream>
- #include<queue>
- #include<stack>
- #include"BinaryTreeNote.h"
- #include<string>
- using namespace std;
- template<class T>
- class BinaryTree{
- BinaryTreeNote<T> *rt;
- int num;
- public:
- BinaryTree();
- ~BinaryTree();
- bool isEmpty( ) const;
- BinaryTreeNote<T>* getRoot()const; //return root note
- BinaryTreeNote<T>* getParent(BinaryTreeNote<T>* current )const; //return parent note
- BinaryTreeNote<T>* getLeftSibing(BinaryTreeNote<T>* current ) const; //return left brother
- BinaryTreeNote<T>* getRightSibing(BinaryTreeNote<T>* current ) const; //return right brother
- void breadFristOrder(BinaryTreeNote<T>* ); //广度优先遍历
- void preOrder(BinaryTreeNote<T>* ); // 前序
- void inOrder(BinaryTreeNote<T>* ); //中序
- void postOrder(BinaryTreeNote<T>* ); //后序
- void levelOrder(BinaryTreeNote<T>* ); //
- void deleteBinaryTree(BinaryTreeNote<T>* root); //delete the tree which is the charge of root
- void build(BinaryTreeNote<T>** );
- BinaryTreeNote<T>** getDRoot(){
- return &rt;
- }
- };
- template<class T>
- BinaryTree<T>::BinaryTree( ):rt(new BinaryTreeNote<T>),num(0){ }
- template<class T>
- bool BinaryTree<T>::isEmpty( )const{
- return !rt;
- }
- template<class T>
- BinaryTreeNote<T>* BinaryTree<T>::getRoot() const{
- return rt;
- }
- template<class T>
- BinaryTreeNote<T>* BinaryTree<T>::getParent(BinaryTreeNote<T>* current ) const {
- queue<BinaryTreeNote<T>*> n;
- BinaryTreeNote<T>* cur;
- n.push(this->getRoot());
- while(!n.empty() ){
- cur=n.front();
- n.pop();
- if(cur->getLeftChild()==current ||cur->getRightChile()==current )
- return cur;
- if( cur->getLeftChild()!=0)
- n.push(cur->getLeftChild);
- else if(cur->getRightChile())
- n.push(cur->getRightChile());
- }
- }
- template<class T>
- BinaryTreeNote<T>* BinaryTree<T>::getLeftSibing(BinaryTreeNote<T>* current ) const{
- if( this->getParent()==NULL)
- return NULL;
- return this->getParent(current)->getLeftChild();
- }
- template<class T>
- BinaryTreeNote<T>* BinaryTree<T>::getRightSibing(BinaryTreeNote<T>* current ) const{
- if( this->getParent()==NULL)
- return NULL;
- return this->getParent(current)->getRightChile();
- }
- template<class T>
- void BinaryTree<T>::breadFristOrder( BinaryTreeNote<T>* root ){
- queue<BinaryTreeNote<T>*> local;
- local.push(root );
- BinaryTreeNote<T>* index=local.front();
- while( !local.empty() ){
- if( index ){
- local.push(index->getLeftChild());
- local.push(index->getRightChile());
- cout<<index->getValue()<<" ";
- }
- local.pop();
- }
- cout<<endl;
- }
- template<class T>
- void BinaryTree<T>::preOrder(BinaryTreeNote<T>* root) {
- BinaryTreeNote<T>* cur=root;
- stack<BinaryTreeNote<T>*> n;
- while( cur || !n.empty() ){
- if( cur ){
- cout<<cur->data<<" ";
- if(cur->getRightChile() )
- n.push(cur->getRightChile());
- cur=cur->getLeftChild();
- }
- else{
- cur=n.top();
- n.pop();
- }
- }
- cout<<endl;
- }
- template<class T>
- void BinaryTree<T>::inOrder(BinaryTreeNote<T>* root){
- //关键部分就是把一个过程抽象成一步一步执行相同的过程!
- BinaryTreeNote<T>* cur=root;
- stack<BinaryTreeNote<T>* > n;
- while( !n.empty() || cur!=0 ){
- if(cur ){
- n.push(cur);
- cur=cur->getLeftChild();
- }
- else{
- cur=n.top();
- cout<<cur->getValue()<<" ";
- cur=cur->getRightChile();
- n.pop();
- }
- }
- cout<<endl;
- }
- template<class T>
- void BinaryTree<T>::postOrder(BinaryTreeNote<T>* root){
- stack<BinaryTreeNote<T>* > n;
- BinaryTreeNote<T>* cur=root;
- BinaryTreeNote<T>* pre=root;
- while( cur ){
- while( cur->getLeftChild() ){
- n.push(cur);
- cur=cur->getLeftChild();
- }
- while( cur &&( cur->getRightChile()==0 || cur->getRightChile()==pre ) ){
- cout<<cur->getValue()<<" ";
- pre=cur;
- if(n.empty())
- return;
- cur=n.top();
- n.pop();
- }
- n.push(cur);
- cur=cur->getRightChile();
- }
- cout<<endl;
- }
- template<class T>
- void BinaryTree<T>::levelOrder(BinaryTreeNote<T>* root){
- if(!root)
- return ;
- queue<BinaryTreeNote<T>* > n;
- BinaryTreeNote<T>* cur;
- n.push(root);
- while(!n.empty() ){
- cur=n.front();
- n.pop();
- cout<<cur->getValue()<<" ";
- if( cur->getLeftChild()!=0)
- n.push(cur->getLeftChild());
- if(cur->getRightChile()!=0)
- n.push(cur->getRightChile());
- }
- cout<<endl;
- }
- template<class T>
- void BinaryTree<T>::deleteBinaryTree(BinaryTreeNote<T>* root){
- if(!root)
- return ;
- queue<BinaryTreeNote<T>* > n;
- BinaryTreeNote<T>* cur;
- n.push(rt);
- if(root==rt)
- rt=0;
- while(!n.empty() ){
- cur=n.front();
- n.pop();
- if(cur->getLeftChild()==root ){
- cur->left=0;
- break;
- }
- else if(cur->getRightChile()==root ){
- cur->right=0;
- break;
- }
- if( cur->getLeftChild()!=0)
- n.push(cur->getLeftChild());
- if(cur->getRightChile()!=0)
- n.push(cur->getRightChile());
- }
- }
- template<class T>
- BinaryTree<T>::~BinaryTree(){
- this->deleteBinaryTree (this->rt);
- }
- //下面是一段2重的指针代码.......
- template<class T>
- void BinaryTree<T>::build(BinaryTreeNote<T>** root){
- T h;
- static int ll=0;
- if(ll++==0)
- cout<<"root:";
- if(cin>>h){
- *root=new BinaryTreeNote<T>;
- num++;
- (*root)->data=h;
- cout<<h<<" left:";
- build(&((*root)->left));
- cout<<h<<" right:";
- build(&((*root)->right));
- }
- else{
- cin.clear();
- return ;
- }
- }
转载于:https://blog.51cto.com/treap/1056679