二叉树实现

来源:互联网 发布:黄海波惹了谁 知乎 编辑:程序博客网 时间:2024/05/17 03:59

#include <stack>
#include <iostream>
#include <math.h>
using namespace std;


/////////////////////////////////////////////////////////////
//普通二叉树结点结构
/////////////////////////////////////////////////////////////
template <class T>
struct BSTNode
{
BSTNode *left,*right;
T element;
BSTNode(BSTNode* x=0,BSTNode* y=0,T e=T())
{
left=x;
right=y;
element=e;
}
};

/////////////////////////////////////////////////////////////
//普通二叉树类
/////////////////////////////////////////////////////////////
template <class T>
class BST
{
public:
 BST(){root=NULL;}
 BST(BST<T>& tree)
 {
  cout<<"in copy structure"<<endl;
  BSTNode<T>* head=tree.getroot();
        root=new BSTNode<T>(head->element);
  root->left=copyBST(head->left);
  root->right=copyBST(right->right);
 }
 void visit(BSTNode<T>* p)
 {
  cout<<p->element<<",";
 }
 BSTNode<T>* copyBST(BSTNode<T>* node)
 {
 if(node!=NULL)
  {
         BSTNode<T>* temp=new BSTNode<T>(node->element);
   temp->left=copyBST(node->left);
   temp->right=copyBST(node->right);
   return temp;
  }
  return NULL;
 }
 void preorder();
 void midorder();
 void postorder();
 BSTNode<T>* getroot();
    void insert(const T& e);
    BSTNode<T>* findnode(const T& e);
    void remove(BSTNode<T>*& node);
    void remove(const T& e);
 void printData( T data, int level );
    void printTree( BSTNode<T>* node, int level );
    void CreateNode(BSTNode<T>* node,istream& in);
 ~BST(){delete root;}
 void Input(istream& in);
 void Output(ostream& out);

private:
 BSTNode<T>* root;
 friend ostream& operator <<(ostream& out,BSTNode<T>& node);
 friend istream& operator >>(istream& in,BSTNode<T>& node);

};

 

/////////////////////////////////////////////////////////////
//二叉树先序遍历
/////////////////////////////////////////////////////////////
template <class T>
void BST<T>::preorder()
{
stack<BSTNode<T>*> pool;
BSTNode<T>* head=root;
while(!pool.empty()||head!=NULL)
{
   while(head!=NULL)
   {
    visit(head);
    pool.push(head);
    head=head->left;
   }
   if(!pool.empty())
   {  
    head=pool.top();
    pool.pop();
    head=head->right;
   }

}
cout<<endl;
}

 

 

/////////////////////////////////////////////////////////////
//二叉树中序遍历
/////////////////////////////////////////////////////////////
template <class T>
void BST<T>::midorder()
{
stack<BSTNode<T>*> pool;
BSTNode<T>* head=root;
while(!pool.empty()||head!=NULL)
{
while(head!=NULL)
{
pool.push(head);
head=head->left;
}
if(!pool.empty())
{
head=pool.top();
pool.pop();
visit(head);
head=head->right;
 }
}
cout<<endl;
}

 

/////////////////////////////////////////////////////////////
//二叉树后序遍历
/////////////////////////////////////////////////////////////
template <class T>
void BST<T>::postorder()
{
stack<BSTNode<T>*> pool;
BSTNode<T>* pre=root;
BSTNode<T>* head=root;
while(head!=NULL)
{
for(;head->left!=NULL;head=head->left) //左子树入栈
{
pool.push(head);                         
}
while(head!=NULL&&(head->right==0||head->right==pre))  //访问左子树结点,或根结点(右子树访问完毕)
{
visit(head);                                         
pre=head;
if(pool.empty())
return;
head=pool.top();
pool.pop();
}
pool.push(head);                          //压入根结点
head=head->right;                        //访问右子树
}
cout<<endl;
}

 

/////////////////////////////////////////////////////////////
//二叉树结点加入
/////////////////////////////////////////////////////////////
template <class T>
void BST<T>::insert(const T& e)
{
 BSTNode<T>* temp=findnode(e);
 if(temp==NULL)
 {
      BSTNode<T>* p=root;
   BSTNode<T>* pre=0;
   while(p!=0)
   {
          pre=p;
    if(p->element<e)
     p=p->right;
    else
     p=p->left;
   }
   if(root==0)
    root=new BSTNode<T>(0,0,e);
   else
   {
    if((pre->element)>e)
     pre->left=new BSTNode<T>(0,0,e);
    else
           pre->right=new BSTNode<T>(0,0,e);

   }
 }
 else
  cout<<"node already exist"<<endl;
}

/////////////////////////////////////////////////////////////
//二叉树结点查找
/////////////////////////////////////////////////////////////
template <class T>
BSTNode<T>* BST<T>::findnode(const T& e)
 {
      BSTNode<T>* p=root;
   stack<BSTNode<T>*> s;
   while(p!=NULL||!s.empty())
   {
    while(p!=NULL)
   {
    if(p->element==e)
     return p;
    s.push(p);
    p=p->left;
      }
   if(!s.empty())
   {
    p=s.top();
    s.pop();
    p=p->right;
   }
   }
   return NULL;
 }


/////////////////////////////////////////////////////////////
//二叉树结点删除
/////////////////////////////////////////////////////////////
template <class T>
void BST<T>::remove(BSTNode<T>*& node)
{
BSTNode<T>* temp=node;
if(node!=0)
{
if(!node->right)
{
node=node->left;
}
else if(!node->left)
{
node=node->right;
}
else
{
temp=node->right;
while(temp->left!=0)
temp=temp->left;
temp->left=node->left;
temp=node;
node=node->right;
}
delete temp;
}
}


template <class T>
void BST<T>::remove(const T& e)
{
 BSTNode<T>* pre=findpre(e);
 BSTNode<T>* node=findnode(e);
 if(node!=NULL)
 {
 if(pre->left==node)
  remove(pre->left);
 else if(pre->right==node)
  remove(pre->right);
 else if(root!=0)
  cout<<"key "<<e<<"is not in the tree"<<endl;
 else cout<<"the tree is empty"<<endl;
 }

}

 

/////////////////////////////////////////////////////////////
//打印二叉树结点
/////////////////////////////////////////////////////////////
template <class T>
void BST<T>::printData(T data,int level ){
for( int i = 0; i < level; i++ ){
printf( "   ");
}
cout<<data<<endl;
}

 

/////////////////////////////////////////////////////////////
//打印二叉树结构
/////////////////////////////////////////////////////////////
template <class T>
void BST<T>::printTree(BSTNode<T>* node,int level){
if( node == NULL ) return;
if( node->right ) {
printTree( node->right, level + 1 );
}
printData( node->element, level );
if( node->left ){
printTree( node->left, level + 1 );
}
}


/////////////////////////////////////////////////////////////
//构造二叉树
/////////////////////////////////////////////////////////////
template <class T>
void BST<T>::CreateNode(BSTNode<T>* node,istream& in)
{
if(node!=NULL)
{
 cout<<"please input the left node of "<<node->element<<endl;
 T e;
 in>>e;
 if(e!=T())
 {
    BSTNode<T>* left=new BSTNode<T>(0,0,e);
 node->left=left;
 CreateNode(left,in);
 }
 cout<<"please input the right node of "<<node->element<<endl;
 in>>e;
 if(e!=T())
 {
    BSTNode<T>* right=new BSTNode<T>(0,0,e);
 node->right=right;
 CreateNode(right,in);
 }
}
else
{
 cout<<"please input root"<<endl;
 T e;
    in>>e;
 root=new BSTNode<T>(0,0,e);
 CreateNode(root,in);
}
}

/////////////////////////////////////////////////////////////
//返回二叉树根结点
/////////////////////////////////////////////////////////////
template <class T>
BSTNode<T>* BST<T>::getroot()
{
 return root;
}


/////////////////////////////////////////////////////////////
//输入二叉树
/////////////////////////////////////////////////////////////
template <class T>
void BST<T>::Input(istream& in)
{
cout<<"please input the tree node"<<endl;
CreateNode(root,in);
}


/////////////////////////////////////////////////////////////
//输出二叉树
/////////////////////////////////////////////////////////////
template <class T>
void BST<T>::Output(ostream& out)
{
out<<"the BST tree is as follows:"<<endl;
printTree(root,0);
}

/////////////////////////////////////////////////////////////
//重载二叉树<<
/////////////////////////////////////////////////////////////
template <class T>
ostream& operator << (ostream& out,BST<T>& node)
{
node.Output(out);
return out;
}


/////////////////////////////////////////////////////////////
//重载二叉树>>
/////////////////////////////////////////////////////////////
template <class T>
istream& operator >> (istream& in,BST<T>& node)
{
node.Input(in);
return in;
}

int main()
{
 BST<int> x=BST<int>();
 cin>>x;
 cout<<x;
 system("pause");
}

0 0
原创粉丝点击