二叉搜索树
#include<iostream>
using namespace std;
template<class type>
class Node{
type data;
Node<type>* leftChild,rightChild,parent;
Node(type key):data(key){}
};
template<class type>
class BinaryTree{
public:
Node<type>* root;
BinaryTree():root(NULL){};
bool isEmpty();
BSTree();
void preOrder();
void inOrder();
void postOrder();
Node<type>* search(type key);
Node<type>* iterativeSearch(type key);
type minNode();
type maxNode();
Node<type>* successor(Node<type>* x);
Node<type>* predecessor(Node<type>* x);
void insert(type key);
bool remove(type key);
void preOrder(Node<type>* n) const;
void inOrder(Node<type>* n) const;
void postOrder(Node<type>* n) const;
Node<type>* search(Node<type>* root,type key);
Node<type>* iterativeSearch(Node<type>* r,type key);
/*
// 删除结点(key为节点键值)
void remove(T key);
// 打印二叉树
void print();
*/
};
template<class type>
bool BinaryTree<type>::isEmpty(){
if(root==NULL){
return true;
}else{
return false;
}
}
template<class type>
void BinaryTree<type>::preOrder(){
preOrder(root);
}
template<class type>
void BinaryTree<type>::preOrder(Node<type>* a)const{
if(a!=NULL){
cout<<a->data<<" ";
preOrder(a->leftChild);
preOrder(a->rightChild);
}
}
template<class type>
void BinaryTree<type>::inOrder(){
inOrder(root);
}
template<class type>
void BinaryTree<type>::inOrder(Node<type>* root)const{
if(root!=NULL){
inOrder(root->leftChild);
cout<<root->data<<" ";
inOrder(root->rightChild);
}
}
template<class type>
void BinaryTree<type>::postOrder(){
postOrder(root);
}
template<class type>
void BinaryTree<type>::postOrder(Node<type>* root)const{
if(root!=NULL){
postOrder(root->leftChild);
cout<<root->data<<" ";
postOrder(root->rightChild);
}
}
template<class type>
Node<type>* BinaryTree<type>::search(type key){
search(root,key);
}
template<class type>
Node<type>* BinaryTree<type>::search(Node<type>* root,type key){
if(root==NULL||root->data==key){
return root;
}
if(root->data>key){
return search(root->leftChild,key);
}else{
return search(root->rightChild,key);
}
}
template<class type>
Node<type>* BinaryTree<type>::iterativeSearch(type key){
return iterativeSearch(root,key);
}
template<class type>
Node<type>* BinaryTree<type>::iterativeSearch(Node<type>* t,type key){
if(t==NULL||t->data==key){
return t;
}
while((t->data!=key)&&(t!=NULL)){
if(t->data>key){
t = t->leftChild;
}else{
t = t->rightChild;
}
}
return t;
}
template<class type>
type BinaryTree<type>::minNode(){
if(root==NULL){
return NULL;
}
while(root->leftChild){
root = root->leftChild;
}
return root->data;
}
template<class type>
type BinaryTree<type>::maxNode(){
if(root==NULL){
return NULL;
}
while(root->rightChild){
root = root->rightChild;
}
return root->data;
}
template<class type>
Node<type>* BinaryTree<type>::successor(Node<type>* x){
if(x->rightChild){
return x->rightChild;
}
Node<type>* y = x->parent;
while((y!=NULL)&&(x!=y->rightChild)){
x = y;
y = x->parent;
}
return y;
}
template<class type>
Node<type>* BinaryTree<type>::predecessor(Node<type>* x){
if(x->leftChild){
return x->leftChild;
}
Node<type>* y = x->parent;
while((y!=NULL)&&(x!=y->leftChild)){
x = y;
y = x->parent;
}
return y;
}
template<class type>
void BinaryTree<type>::insert(type key){
Node<type> newNode(key),y(NULL);
if(root==NULL){
root = newNode;
}
y = root;
while(true){
if(y->data>newNode->data){
if(y->leftChild!=NULL){
y = y->leftChild;
}else{
y->leftChild = newNode;
newNode->parent = y;
return;
}
}else{
if(y->rightChild!=NULL){
y = y->rightChild;
}else{
y->rightChild = newNode;
newNode->parent = y;
return;
}
}
}
}
template<class type>
bool BinaryTree<type>::remove(type key){
Node<type>* tmp = root;
if(root==NULL){
cout<<"NOOOOO!"<<endl;
return false;
}
while(tmp!=NULL){
if(tmp->data>key){
tmp = tmp->leftChild;
}else if(tmp->data<key){
tmp = tmp->rightChild;
}else{
break;
}
}
Node<type>* n ;
if((tmp->leftChild==NULL)&&(tmp->rightChild==NULL)){
tmp->parent = NULL;
}else if((tmp->leftChild==NULL)||(tmp->rightChild==NULL)){
(tmp->leftChild==NULL)?n = tmp->rightChild : n = tmp->leftChild;
tmp->parent = n->parent;
return true;
}
}