#include
using namespace std;
//枚举类,前中后三种遍历方式
enum ORDER_MODE
{
ORDER_MODE_PREV = 0,
ORDER_MODE_MID,
ORDER_MODE_POST
};
//树节点的结构体
template
struct BinaryNode
{
T element;
BinaryNode *left;
BinaryNode *right;
BinaryNode(const T& theElement,
BinaryNode *lt,
BinaryNode *rt):
element(theElement),
left(lt),
right(rt)
{
}
};
template
class BinarySearchTree
{
private:
BinaryNode
public:
BinarySearchTree();
BinarySearchTree(const BinarySearchTree& rhs);
~BinarySearchTree();
const T& findMin() const;
const T& findMax() const;
bool contains(const T& x) const;
void printTree(ORDER_MODE eOrderMode = ORDER_MODE_PREV) const;
void makeEmpty();
void insert(const T& x);
void remove(const T& x);
private:
void insert(const T& x, BinaryNode
void remove(const T& x, BinaryNode
BinaryNode
BinaryNode
bool contains(const T& x, const BinaryNode
void makeEmpty(BinaryNode
void printTreeInPrev(BinaryNode
void printTreeInMid(BinaryNode
void printTreeInPost(BinaryNode
};
//构造方法
template
BinarySearchTree
{
m_root = NULL;
}
//使用另一棵二叉搜索树的构造函数
template
BinarySearchTree
{
m_root = rhs.m_root;
}
//析构函数,释放内存
template
BinarySearchTree
{
makeEmpty();
}
// 判断x元素是否存在
template
bool BinarySearchTree
{
return contains(x, m_root);
}
//递归调用
template
bool BinarySearchTree
{
if (!t)
return false;
else if (x < t->element)
return contains(x, t->left);
else if (x > t->element)
return contains(x, t->right);
else
return true;
}
// 寻找树中的最小值
template
const T& BinarySearchTree
{
return findMin(m_root)->element;
}
//递归搜索树中最小值
template
BinaryNode
{
//二叉树的一个特点就是左子叶的值比根节点小, 右子叶的比根节点的大
if (!t)
return NULL;
if (t->left == NULL)
return t;
else
return findMin(t->left);
}
// 寻找树中最大值
template
const T& BinarySearchTree
{
return findMax(m_root)->element;
}
//递归寻找树中最大值
template
BinaryNode
{
//二叉树的一个特点就是左子叶的值比根节点小, 右子叶的比根节点的大
if (t != NULL)
while (t->right != NULL)
t = t->right;
return t;
}
// 插入元素
template
void BinarySearchTree
{
insert(x, m_root);
}
//递归插入
template
void BinarySearchTree
{
if (t == NULL)
t = new BinaryNode
else if (x < t->element)
insert(x, t->left);
else if (x > t->element)
insert(x, t->right);
else
;//do nothing
}
//移除元素
template
void BinarySearchTree
{
return remove(x, m_root);
}
//递归移除
template
void BinarySearchTree
{
if (t == NULL)
return;
if (x < t->element)
remove(x, t->left);
else if (x > t->element)
remove (x, t->right);
else // now ==
{
if (t->left != NULL &&
t->right != NULL)//two child
{
t->element = findMin(t->right)->element;
remove(t->element, t->right);
}
else
{
BinaryNode
t = (t->left != NULL) ? t->left : t->right;
delete oldNode;
}
}
}
//清空二叉树
template
void BinarySearchTree
{
makeEmpty(m_root);
}
//递归清空
template
void BinarySearchTree
{
if (t)
{
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
}
t = NULL;
}
// 打印二叉搜索树
template
void BinarySearchTree
{
if (ORDER_MODE_PREV == eOrderMode)
printTreeInPrev(m_root);
else if (ORDER_MODE_MID == eOrderMode)
printTreeInMid(m_root);
else if (ORDER_MODE_POST == eOrderMode)
printTreeInPost(m_root);
else
;//do nothing
}
//前序打印
template
void BinarySearchTree
{
if (t)
{
cout << t->element;
printTreeInPrev(t->left);
printTreeInPrev(t->right);
}
}
//中序打印
template
void BinarySearchTree
{
if (t)
{
printTreeInPrev(t->left);
cout << t->element;
printTreeInPrev(t->right);
}
}
//后序打印
template
void BinarySearchTree
{
if (t)
{
printTreeInPost(t->left);
printTreeInPost(t->right);
cout << t->element;
}
}
```
测试代码
===
```C++
#include "BinarySearchTree.h"
//测试基本操作 cout << "移除元素2之后"< //测试前中后序打印 return 0;
int main()
{
BinarySearchTree
binaryTree.insert(5);
binaryTree.insert(1);
binaryTree.insert(2);
binaryTree.insert(3);
binaryTree.insert(6);
binaryTree.insert(8);
//测试前中后序打印
cout <
cout <
cout <
cout <
bool b = binaryTree.contains(1);
cout<< "是否存在1:"< int x = binaryTree.findMin();
cout << "最小值为:"<< x <
cout << "最大值为:"<< x <
cout <
cout <
cout <
cout <
}