1.二叉检索树的属性:
对于二叉检索树的一个值为K的结点,该结点左子树中任意一个结点的值都小于K;该结点右子树中任意一个结点的值都大于或等于K。如果按照中序遍历将各个结点打印出来,就会得到由小到大排序的结点
2.部分方法解释:
2.1 find:如果树T为空,返回false;如果存储在T中的项为it,返回true。若以上两种情况都不成立,就对T的一个子树进行递归调用
2.2 insert:遍历树,将树插入到最后一个空结点上
2.3 remove:如果是叶结点,则立即被删除。如果结点有一个儿子,则该结点可以在其父结点调整它的链以绕过它后被删除,如图:
当被删除结点有两个儿子时,一般的删除策略是用其右子树最小的数据代替该结点的值并递归的删除那个结点,如图:
3,代码实现:
#include<iostream>
using namespace std;
//Binary Search Tree implementation
template<typename E>
class BST{
private:
//Simple binary tree node implementation
struct BSTNode{
E element; //The node's value
BSTNode* left; //Pointer to the left child
BSTNode* right; //Pointer to the right child
BSTNode() {left=right=NULL;}
BSTNode(const E& e, BSTNode* l=NULL, BSTNode* r=NULL)
{element=e; left=l; right=r;}
};
BSTNode* root; //Root of the BST
int number; //Number of nodes in the BST
// Private "helper" functions
void clearhelp(BSTNode* root){
if(root==NULL) return;
clearhelp(root->left);
clearhelp(root->right);
delete root;
}
void inserthelp(BSTNode * & root, const E& it){
if(root==NULL)
root=new BSTNode(it,NULL,NULL);
else if(it<root->element){
inserthelp(root->left, it);
}
else {
inserthelp(root->right, it);
}
}
BSTNode* getmin(BSTNode* root) const {
if(root==NULL)
return NULL;
if(root->left==NULL)
return root;
return getmin(root->left);
}
BSTNode* getmax(BSTNode* root) const {
if(root==NULL)
return NULL;
if(root->right==NULL)
return root;
return getmax(root->right);
}
void deletemin(BSTNode * & root){
if(root->left==NULL){
BSTNode* temp=root;
root=root->right;
delete temp;
}
else{
deletemin(root->left);
}
}
void removehelp(BSTNode * & root, const E& it){
if(root==NULL) return; //it is not in the tree
else if(it<root->element) //find it
removehelp(root->left, it);
else if(it>root->element)
removehelp(root->right, it);
else{ //Found: remove it
BSTNode* temp=root;
if(root->left==NULL){ //Only a right child
root=root->right; //so point to right
delete temp;
}else if(root->right==NULL){ //Only a left child
root=root->left; //so point to left
delete temp;
}else{ //Both children are not empty
root->element=getmin(root->right)->element;
deletemin(root->right);
}
}
}
bool findhelp(BSTNode* root, const E& it)const{
if(root==NULL) //empty tree
return false;
if(it<root->element)
return findhelp(root->left, it); //Check left
if(it>root->element)
return findhelp(root->right, it); //Check right
return true;
}
void printhelp(BSTNode* root)const{ //inorder traversal
if(root==NULL)
return;
printhelp(root->left);
cout << root->element << " ";
printhelp(root->right);
}
public:
BST() {root=NULL; number=0;} //constructor
~BST() {clearhelp(root);} //destructor
//Reinitialize tree
void clear() {clearhelp(root); root=NULL; number=0;}
//insert a record into the tree
void insert(const E& it){
inserthelp(root, it);
number++;
}
//remove a record from the tree
bool remove(const E& it){
if(find(it)){ //first find it
removehelp(root, it);
number--;
return true;
}
else return false;
}
//determine whether the record is in the tree
bool find(const E& it) const {
return findhelp(root, it);
}
//find the minimum reconrd
const E& findMin()const {
BSTNode* temp=getmin(root);
if(temp==NULL)
return NULL;
else
return temp->element;
}
//find the maximum record
const E& findMax()const {
BSTNode* temp=getmax(root);
if(temp==NULL)
return NULL;
else
return temp->element;
}
//the number of the node
int size() {return number;}
//determine whether it's empty
bool isEmpty() {return (number==0);}
//inorder traversal
void print()const{
if(root==NULL) cout << "The BST is empty.\n";
else {printhelp(root); cout << endl;}
}
};