比较全的二叉查找树(BinarySearchTree)的实现
新年到!中大黑熊祝大家新年快乐!在这里,po上二叉查找树的比较全的实现,我自己整理的,没有测试过,希望有错的能和我说一声,赐教赐教一下我哈!
#ifndef BinaryTree_H
#define BinaryTree_H
template<typename T>
struct BinaryNode
{
T element;
BinaryNode * left;
BinaryNode * right;
BinaryNode(const T & theElement,BinaryNode *lt = NULL,BinaryNode *rt = NULL):element(theElement),left(lt),right(rt) {}
};
template<typename T>
class BinarySearchTree
{
public:
BinarySearchTree();
BinarySearchTree(const BinarySearchTree & rhs);
~BinarySearchTree();
const T & findMin() const;
const T & findMax() const;
bool contains(const T & x) const;
bool isEmpty() const;
void printTree() const;
void makeEmpty();
void insert(const T & x);
void remove(const T & x);
const BinarySearchTree & operator=(const BinarySearchTree & rhs);
int countNodes() const;
int countLeaves() const;
int countFull() const;
bool check() const;
//在树上查找k1<=Keys(X)<=k2的值
void printRange(T & lower,T & upper,BinaryNode<T>* &root);
private:
BinaryNode * root;
void insert(const T & x,BinaryNode * &t) const
{
if(t == NULL)
t = new BinaryNode(x);
else if(x < t->element)
insert(x,t->left);
else if(x > t->element)
insert(x,t->right);
else
;
}
void remove(const T & x,BinaryNode * &t) const
{
if(t == NULL)
return ;
if(x < t->element)
remove(x,t->left);
else if(x > t->element)
remove(x,t->right);
else if(t->left != NULL && t->right != NULL) //Two children
{
t->element = findMin(t->right)->element;
remove(t->element,t->right);
}
else
{
BinaryNode * oldNode = t;
t = (t->left != NULL) ? t->left : t->right;
delete oldNode;
}
}
BinaryNode * findMin(BinaryNode * t) const
{
if(t == NULL)
return NULL;
else
{
while(t->left != NULL)
t = t->left;
return t;
}
}
BinaryNode * findMax(BinaryNode * t) const
{
if(t == NULL)
return NULL;
else
{
while(t->right != NULL)
t = t->right;
return t;
}
}
bool contains(const T & x,BinaryNode *t) const
{
if(t == NULL)
return false;
else if(x < t->element)
return contains(x,t->left);
else if(c > t->element)
return contains(x,t->right);
else
return true;
}
void makeEmpty(BinaryNode *t) const
{
if(t == NULL)
;
else
{
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
t = NULL;
}
}
BinaryNode * clone(BinaryNode * t) const
{
if(t == NULL)
return NULL;
else
return new BinaryNode(t->element,clone(t->left),clone(t->right));
}
void printTree(BinaryNode * &t) const
{//这里采用中缀遍历
cout << t->element << endl;
printTree(t->left);
printTree(t->right);
}
//统计t中结点的个数
int countNodes(BinaryNode * &t) const
{
if(t==NULL)
return 0;
else
return countNodes(t->left)+countNodes(t->right)+1;
}
//统计t中树叶的片数
int countLeaves(BinaryNode * &t) const
{
if(t == NULL)
return 0;
else if(t->left == NULL && t->right == NULL)
return 1;
else
return countLeaves(t->left)+countLeaves(t->right);
}
//统计t中满结点的个数
int countFull(BinaryNode * &t) const
{
if(t == NULL)
return 0;
int tIsFull = (t->left != NULL && t->right != NULL) ? 1:0;
return tIsFull + countFull(t->left) + countFull(t->right);
}
//测试一棵二叉树是否在每一个结点都满足查找树的序的性质
bool check(BinaryNode * &t) const
{
if(t->left == NULL && t->right == NULL)
return true;
else if(!(t->element > findMin(t)->element && t->element < findMax(t)->element))
return false;
check(t->left);
check(t->right);
}
};
#endif
#include "BinaryTree.h"
#include <iostream>
using namespace std;
template<typename T>
BinarySearchTree<T>::BinarySearchTree()
{
root = NULL;
}
template<typename T>
BinarySearchTree<T>::BinarySearchTree(const BinarySearchTree & rhs)
{
root = NULL;
*this = rhs;
}
template<typename T>
BinarySearchTree<T>::~BinarySearchTree()
{
makeEmpty();
}
template<typename T>
const T & BinarySearchTree<T>::findMin() const
{
if(!isEmpty())
return findMin(root)->element;
}
template<typename T>
const T & BinarySearchTree<T>::findMax() const
{
if(!isEmpty())
return findMax(root)->element;
}
template<typename T>
bool BinarySearchTree<T>::contains(const T & x) const
{
return contains(x,root);
}
template<typename T>
bool BinarySearchTree<T>::isEmpty() const
{
return root == NULL;
}
template<typename T>
void BinarySearchTree<T>::printTree() const
{
if(isEmpty())
cout << "Empty tree." << endl;
else
printTree(root);
}
template<typename T>
void BinarySearchTree<T>::makeEmpty()
{
makeEmpty(root);
}
template<typename T>
void BinarySearchTree<T>::insert(const T & x)
{
insert(x,root);
}
template<typename T>
void BinarySearchTree<T>::remove(const T & x)
{
remove(x,root);
}
/* Deep Copy*/
template<typename T>
const BinarySearchTree<T> &BinarySearchTree<T>::operator=(const BinarySearchTree<T> & rhs)
{
if( this != &rhs )
{
makeEmpty();
root = clone(rhs.root);
}
return *this;
}
template<typename T>
int BinarySearchTree<T>::countNodes() const
{
return countNodes(root);
}
template<typename T>
int BinarySearchTree<T>::countLeaves() const
{
return countLeaves(root);
}
template<typename T>
int BinarySearchTree<T>::countFull() const
{
return countFull(root);
}
template<typename T>
bool BinarySearchTree<T>::check() const
{
return check(root);
}
template<typename T>
void BinarySearchTree<T>::printRange(T & lower,T & upper,BinaryNode<T>* &root)
{
if(root != NULL)
{
if(lower <= t->element)
printRange(lower,upper,root->left);
if(lower <= t->element && t->element >= upper)
cout << t->element << endl;
if(t->element <= upper)
printRange(lower,upper,root->right);
}
}

浙公网安备 33010602011771号