二叉树实现
#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");
}
總結
- 上一篇: 初学者如何开发出高质量J2EE系统
- 下一篇: Qt实现3D纹理渲染自由旋转空间立方体