您的位置:首页 > 理论基础 > 数据结构算法

《数据结构与算法分析--C++描述》(第三版)学习笔记系列一:BST的实现

2009-10-04 17:18 946 查看
实现代码:

"BST.h":

#ifndef __BST_H__
#define __BST_H__

#include <iostream>
using namespace std;

//*********BST类的接口****************
//void insert(x)     插入
//void remove(x)     删除
//bool contains(x)     检查是否包含
//const Comparable& findMin()  查找最小元素
//const Comparable& findMax()  查找最大元素
//bool isEmpty()     判断是否为空
//void makeEmpty()     清空
//void printTree()     打印

template<typename Comparable>
class BST
{
public:
BST():root(NULL)
{
}

BST(const BST& rhs):root(NULL)
{
*this = rhs;
}

const BST& operator=(const BST& rhs)
{
if(this != &rhs)
{
makeEmpty();
root = clone(rhs.root);
}

return *this;
}

~BST()
{
makeEmpty();
}

//查找最小项
const Comparable& findMin() const
{ if(isEmpty())
return NULL;
return findMin(root)->element;
}

//查找最大项
const Comparable& findMax() const
{
if(!isEmpty())
return findMax(root)->element;
else
return NULL;
}

//判断x是否在树中
bool contains(const Comparable& x) const
{
return contains(x, root);
}

//判断树是否为空
bool isEmpty() const
{
return root == NULL;
}

//打印节点值
void printTree(ostream& out = cout) const
{
if(isEmpty())
out << "Empty tree" << endl;
else
printTree(root, out);
}

//清空树
void makeEmpty()
{
makeEmpty(root);
}

//插入x
void insert(const Comparable& x)
{
insert(x, root);
}

//删除x
void remove(const Comparable& x)
{
remove(x, root);
}

private:
//节点数据结构
struct BSTNode
{
Comparable element;
BSTNode* left;
BSTNode* right;
BSTNode(const Comparable & theElement, BSTNode* lp, BSTNode* rp ):element(theElement), left(lp), right(rp)
{
}
};
BSTNode* root;

void insert(const Comparable& x, BSTNode* &t)
{
if(NULL == t)
t = new BSTNode(x, NULL, NULL);
else if(x < t->element)
insert(x, t->left);
else if(t->element < x)
insert(x, t->right);
else
;     //已存在,暂无操作
}

//insert非递归版本
/*******************************************************
研究后再加入
*********************************************************/

void remove(const Comparable& x, BSTNode*& t)
{
if(NULL == t)
return;
else if(x < t->element)
remove(x, t->left);
else if(t->element < x)
remove(x, t->right);
else if(t->left != NULL && t->right != NULL) //两个孩子的情况
{
t->element = findMin(t->right)->element; //首先找到右子树中最小的项
remove(t->element, t->right);    //其次在右子树中删除最小的项
}
else           //一个孩子或叶子节点的情况
{
BSTNode* oldNode = t;
t = (t->left != NULL) ? t->left : t->right;
delete oldNode;
}

}

//remove非递归版本
/*******************************************************
研究后再加入
*********************************************************/

BSTNode* findMin(BSTNode* t) const
{
if(NULL == t)
return NULL;
if(t->left == NULL)
return t;
return findMin(t->left);
}

//findMin非递归版本
/*******************************************************
BSTNode* findMin(BSTNode* t) const
{
if(t != NULL)
while(t->left != NULL)
t = t->left;
return t;
}
*********************************************************/

BSTNode* findMax(BSTNode* t) const
{
if(NULL == t)
return NULL;
if(t->right == NULL)
return t;
return findMax(t->right);
}

//findMax非递归版本
/*******************************************************
BSTNode* findMax(BSTNode* t) const
{
if(t != NULL)
while(t->right != NULL)
t = t->right;
return t;
}
*********************************************************/

bool contains(const Comparable& x, BSTNode* t) const
{
if(t == NULL)
return false;
else if(x < t->element)
return contains(x, t->left);
else if(t->element < x)
return t->right;
else
return true;
}

//contains非递归版本
/*******************************************************
bool contains(const Comparable& x, BSTNode* t) const
{
while(t != NULL)
if(x < t->element)
t = t->left;
else if(t->element < x)
t = t->right;
else
return true;
return false;
}
*********************************************************/

void makeEmpty(BSTNode* &t)
{
if(t != NULL)
{
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
}
t = NULL;
}

void printTree(BSTNode* t, ostream& out) const
{
if(t != NULL)
{
printTree(t->left, out);
out << t->element << endl;
printTree(t->right, out);
}
}

BSTNode* clone(BSTNode* t) const
{
if(t == NULL)
return NULL;
else
return new BSTNode(t->element, clone(t->left), clone(t->right));
}

};

#endif //__BST_H__

测试代码:

"BSTTest.cpp":

#include "BST.h"

int main()
{
BST<int> BSTree;
cout << "此时树" << (BSTree.isEmpty()?"为":"不为") << "空" << endl;

BSTree.insert(10);
BSTree.insert(20);
BSTree.insert(30);
BSTree.insert(15);
BSTree.insert(25);
BSTree.insert(35);
BSTree.insert(13);
BSTree.insert(17);
BSTree.insert(22);
BSTree.insert(29);
cout << "依次插入10、20、30、15、25、35、13、17、22、29后输出节点值为: " << endl;
BSTree.printTree();
cout << "最小值为: " << BSTree.findMin() << "最大值为: " << BSTree.findMax() << endl;
cout << "35" << (BSTree.contains(35)?"在":"不在") << "树中。" << endl;
cout << "100" << (BSTree.contains(35)?"在":"不在") << "树中。" << endl;
cout << "此时树" << (BSTree.isEmpty()?"为":"不为") << "空" << endl;
BSTree.remove(0);
BSTree.remove(20);
BSTree.remove(40);
BSTree.remove(30);
BSTree.remove(20);
BSTree.remove(10);
cout << "依次删除0、20、40、30、20、20、10后输出节点值为: " << endl;
BSTree.printTree();
cout << "最小值为: " << BSTree.findMin() << "最大值为: " << BSTree.findMax() << endl;
cout << "此时树" << (BSTree.isEmpty()?"为":"不为") << "空" << endl;

BSTree.makeEmpty();
cout << "清空后输出节点值为: " << endl;
BSTree.printTree();
cout << "最小值为: " << BSTree.findMin() << "最大值为: " << BSTree.findMax() << endl;
cout << "此时树" << (BSTree.isEmpty()?"为":"不为") << "空" << endl;

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐