ACM模版
二叉查找树
#include <iostream>
template<typename T>
class BSTNode
{
public:
T _key;
BSTNode *_lchild;
BSTNode *_rchild;
BSTNode *_parent;
BSTNode(T key , BSTNode *lchild, BSTNode *rchild, BSTNode *parent) :
_key(key), _lchild(lchild), _rchild(rchild), _parent(parent) {};
};
template<typename T>
class BSTree
{
private:
BSTNode<T>* _Root;
public:
BSTree() : _Root(NULL) {};
~BSTree() {};
void insert (T key);
BSTNode<T>* search(T key);
void preOrder();
void inOrder();
void postOrder();
BSTNode<T>* minimumNode();
BSTNode<T>* maximumNode ();
T minimumKey();
T maximumKey();
void print();
void remove(T key);
BSTNode<T>* predecessor(BSTNode<T>* x);
BSTNode<T>* sucessor(BSTNode<T>* x);
void destory ();
private:
void insert(BSTNode<T>* &tree, BSTNode<T>* z);
BSTNode<T>* search(BSTNode<T>* &tree, T key) const;
void preOrder(BSTNode<T>*& tree) const;
void inOrder(BSTNode<T>*& tree) const;
void postOrder(BSTNode<T>*& tree) const;
BSTNode<T>* minimumNode(BSTNode<T>*& tree);
BSTNode<T>* maximumNode (BSTNode<T>*& tree);
void print(BSTNode<T>*& tree);
BSTNode<T>* remove(BSTNode<T>*& tree, BSTNode<T>* z);
void destory(BSTNode<T>*& tree);
};
template<typename T>
void BSTree<T>::preOrder(BSTNode<T>*& tree) const
{
if(tree)
{
std::cout << tree->_key << " ";
preOrder(tree->_lchild);
preOrder(tree->_rchild);
}
}
template<typename T>
void BSTree<T>::preOrder()
{
preOrder(_Root);
}
template <typename T>
void BSTree<T>::inOrder(BSTNode<T>*&tree) const
{
if(tree)
{
inOrder(tree->_lchild);
std::cout<<tree->_key<<" ";
inOrder(tree->_rchild);
}
}
template<typename T>
void BSTree<T>::inOrder()
{
inOrder(_Root);
}
template <typename T>
void BSTree<T>::postOrder(BSTNode<T>*&tree) const
{
if(tree)
{
postOrder(tree->_lchild);
postOrder(tree->_rchild);
std::cout<<tree->_key<<" ";
}
}
template<typename T>
void BSTree<T>::postOrder()
{
postOrder(_Root);
}
template<typename T>
void BSTree<T> ::insert(BSTNode<T>* &tree,BSTNode<T>* z)
{
BSTNode<T>* parent = NULL;
BSTNode<T>* temp = tree;
while(temp!=NULL)
{
parent= temp;
if(z->_key>temp->_key)
temp= temp->_rchild;
else
temp=temp->_lchild;
}
z->_parent = parent;
if(parent==NULL)
tree = z;
else if(z->_key>parent->_key)
parent->_rchild = z;
else
parent->_lchild = z;
}
template <typename T>
void BSTree<T>::insert(T key)
{
BSTNode<T>* z= new BSTNode<T>(key,NULL,NULL,NULL);
if(!z)
return ;
insert(_Root,z);
}
template <typename T>
BSTNode<T>* BSTree<T>::search(BSTNode<T>*& tree,T key) const
{
BSTNode<T>* temp = tree;
while(temp != NULL)
{
if(temp->_key == key)
return temp;
else if(temp->_key>key)
temp = temp->_lchild;
else
temp = temp->_rchild;
}
return NULL;
}
template <typename T>
BSTNode<T> * BSTree<T>::search(T key)
{
return search(_Root,key);
}
template <typename T>
BSTNode<T>* BSTree<T>::minimumNode(BSTNode<T>*&tree)
{
BSTNode<T>* temp = tree;
while(temp->_lchild)
{
temp= temp->_lchild;
}
return temp;
}
template<typename T>
BSTNode<T>* BSTree<T>::minimumNode()
{
return minimumNode(_Root);
}
template<typename T>
BSTNode<T>* BSTree<T>::maximumNode(BSTNode<T>* &tree)
{
BSTNode<T>* temp=tree;
while(temp->_rchild)
{
temp= temp->_rchild;
}
return temp;
}
template<typename T>
BSTNode<T>* BSTree<T>::maximumNode()
{
return maximumNode(_Root);
}
template<typename T>
T BSTree<T>::minimumKey()
{
BSTNode<T> *temp = minimumNode(_Root);
return temp->_key;
}
template<typename T>
T BSTree<T>::maximumKey()
{
BSTNode<T> *temp = maximumNode(_Root);
return temp->_key;
}
template<typename T>
void BSTree<T>::print(BSTNode<T>*& tree)
{
if(tree)
{
if(tree->_lchild)
{
std::cout << "节点" << tree->_key << "有左孩子为" << tree->_lchild->_key << std::endl;
}
else
{
std::cout << "节点" << tree->_key << "无左孩子" << std::endl;
}
if(tree->_rchild)
{
std::cout << "节点" << tree->_key << "有右孩子为" << tree->_rchild->_key << std::endl;
}
else
{
std::cout << "节点" << tree->_key << "无右孩子" << std::endl;
}
print(tree->_lchild);
print(tree->_rchild);
}
}
template<typename T>
void BSTree<T>::print()
{
print(_Root);
}
template <typename T>
BSTNode<T>* BSTree<T>::predecessor(BSTNode<T>* x)
{
if(x->_key == minimumNode(_Root)->_key)
{
return NULL;
}
BSTNode <T> * y = NULL;
y = search(_Root,x->_key);
if(y==NULL) return NULL;
if(y->_lchild!=NULL)
return maximumNode(y->_lchild);
BSTNode<T>* parent = y->_parent;
if(parent->_rchild == y)
return parent;
while(parent!=NULL&&parent->_rchild==NULL)
{
parent=parent->_parent;
}
return parent;
}
template <typename T>
BSTNode<T>* BSTree<T>::sucessor(BSTNode<T>* x)
{
if(x->_key==maximumNode(_Root)->_key)
return NULL;
BSTNode<T>* y = NULL;
y = search(_Root,x->_key);
if(!y)
return NULL;
if(y->_rchild!=NULL)
return minimumNode(y->_rchild);
BSTNode <T>* parent = y->_parent;
if(y->_parent->_lchild == y)
return parent;
while(parent!=NULL)
{
if(parent->_lchild!=NULL&&parent!=y->_parent)
return parent;
parent=parent->_parent;
}
return NULL;
}
template <class T>
BSTNode<T>* BSTree<T>::remove(BSTNode<T>* &tree, BSTNode<T> *z)
{
BSTNode<T> *x=NULL;
BSTNode<T> *y=NULL;
if ((z->_lchild == NULL) || (z->_rchild == NULL) )
y = z;
else
y = sucessor(z);
if (y->_lchild != NULL)
x = y->_lchild;
else
x = y->_rchild;
if (x != NULL)
x->_parent = y->_parent;
if (y->_parent == NULL)
tree = x;
else if (y == y->_parent->_lchild)
y->_parent->_lchild = x;
else
y->_parent->_rchild= x;
if (y != z)
z->_key = y->_key;
return y;
}
template<typename T>
void BSTree<T>::remove(T key)
{
BSTNode<T> *z, *node;
if ((z = search(_Root, key)) != NULL)
if ( (node = remove(_Root, z)) != NULL)
delete node;
}
template<typename T>
void BSTree<T>::destory(BSTNode<T>*& tree)
{
if(tree->_lchild!=NULL)
destory(tree->_lchild);
if(tree->_rchild!=NULL)
destory(tree->_rchild);
if(tree->_lchild==NULL&&tree->_rchild==NULL)
{
delete(tree);
tree = NULL;
}
}
template<typename T>
void BSTree<T>::destory()
{
destory(_Root);
}
int main()
{
BSTree<int> s ;
int a ;
std::cout << "请输入二叉树结点以构造二叉查找树:" << std::endl;
while(std::cin >> a )
s.insert(a);
std::cin.clear();
std::cout << "前序遍历二叉查找树:" << std::endl;
s.postOrder();
std::cout << std::endl;
std::cout << "中序遍历二叉查找树:" << std::endl;
s.inOrder();
std::cout << std::endl;
std::cout << "后序遍历二叉查找树:" << std::endl;
s.postOrder();
std::cout << std::endl;
std::cout << "打印二叉查找树" << std::endl;
s.print();
std::cout << "请输入要查找的数:" << std::endl;
while(std::cin >> a)
{
BSTNode<int>* findnode = s.search(a);
if(!findnode)
{
std::cout << "查找失败" << std::endl;
s.insert(a);
std::cout << "已经将" << a << "插入二叉查找树,现在二叉查找树为:" << std::endl;
s.inOrder();
std::cout << std::endl;
}
else
{
std::cout << findnode->_key << "查找成功" << std::endl;
}
}
std::cin.clear();
std::cout << "请输入结点以查找其前驱节点" << std::endl;
BSTNode<int>* findPreNode= new BSTNode<int>(1,NULL,NULL,NULL);
while(std::cin >> findPreNode->_key)
{
BSTNode<int>* preNode ;
if((preNode= s.predecessor(findPreNode))!=NULL)
{
std::cout << "其前驱结点为:";
std::cout << preNode->_key << std::endl;
}
else
{
std::cout << "没有前驱结点" << std::endl;
}
if((preNode= s.sucessor(findPreNode))!=NULL)
{
std::cout << "其后继结点为:";
std::cout << preNode->_key << std::endl;
}
else
{
std::cout << "没有后继结点" << std::endl;
}
}
std::cin.clear();
std::cout << "请输入要删除的结点:" << std::endl;
while(std::cin >> a)
{
s.remove(a);
std::cout << "删除后的二叉排序树:" << std::endl;
s.inOrder();
}
BSTNode<int>* maxNode = s.minimumNode();
if(!maxNode)
{
std::cout << "最小的节点为:" << maxNode->_key << std::endl;
}
BSTNode<int>* minNode = s.maximumNode();
if(!minNode)
{
std::cout << "最大的节点为:" << minNode->_key << std::endl;
}
std::cout << "销毁二叉树" << std::endl;
s.destory();
s.inOrder();
return 0;
}