时间:2021-05-19
复制代码 代码如下:
#include <iostream>
using namespace std;
//枚举类,前中后三种遍历方式
enum ORDER_MODE
{
ORDER_MODE_PREV = 0,
ORDER_MODE_MID,
ORDER_MODE_POST
};
//树节点的结构体
template <class T>
struct BinaryNode
{
Telement;
BinaryNode*left;
BinaryNode*right;
BinaryNode(const T& theElement,
BinaryNode *lt,
BinaryNode *rt):
element(theElement),
left(lt),
right(rt)
{
}
};
template <class T>
class BinarySearchTree
{
private:
BinaryNode<T>*m_root;
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<T>* &t) ;
void remove(const T& x, BinaryNode<T>* &t) ;
BinaryNode<T>* findMin( BinaryNode<T>* t) const;
BinaryNode<T>* findMax( BinaryNode<T>* t) const;
bool contains(const T& x, const BinaryNode<T>* t) const;
void makeEmpty(BinaryNode<T>* &t);
void printTreeInPrev(BinaryNode<T>* t) const;
void printTreeInMid(BinaryNode<T>* t)const;
void printTreeInPost(BinaryNode<T>* t)const;
};
//构造方法
template <class T>
BinarySearchTree<T>::BinarySearchTree()
{
m_root = NULL;
}
//使用另一棵二叉搜索树的构造函数
template <class T>
BinarySearchTree<T>:: BinarySearchTree(const BinarySearchTree& rhs)
{
m_root = rhs.m_root;
}
//析构函数,释放内存
template <class T>
BinarySearchTree<T>:: ~BinarySearchTree()
{
makeEmpty();
}
// 判断x元素是否存在
template <class T>
bool BinarySearchTree<T>::contains(const T& x) const
{
return contains(x, m_root);
}
//递归调用
template <class T>
bool BinarySearchTree<T>::contains(const T& x, const BinaryNode<T>* t) const
{
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 <class T>
const T& BinarySearchTree<T>::findMin() const
{
return findMin(m_root)->element;
}
//递归搜索树中最小值
template <class T>
BinaryNode<T>* BinarySearchTree<T>::findMin( BinaryNode<T>* t) const
{
//二叉树的一个特点就是左子叶的值比根节点小, 右子叶的比根节点的大
if (!t)
return NULL;
if (t->left == NULL)
return t;
else
return findMin(t->left);
}
// 寻找树中最大值
template <class T>
const T& BinarySearchTree<T>::findMax() const
{
return findMax(m_root)->element;
}
//递归寻找树中最大值
template <class T>
BinaryNode<T>* BinarySearchTree<T>::findMax( BinaryNode<T>* t) const
{
//二叉树的一个特点就是左子叶的值比根节点小, 右子叶的比根节点的大
if (t != NULL)
while (t->right != NULL)
t = t->right;
return t;
}
// 插入元素
template <class T>
void BinarySearchTree<T>:: insert(const T& x)
{
insert(x, m_root);
}
//递归插入
template <class T>
void BinarySearchTree<T>::insert(const T& x, BinaryNode<T>* &t)
{
if (t == NULL)
t = new BinaryNode<T>(x, NULL, NULL);//注意这个指针参数是引用
else if (x < t->element)
insert(x, t->left);
else if (x > t->element)
insert(x, t->right);
else
;//do nothing
}
//移除元素
template <class T>
void BinarySearchTree<T>::remove(const T& x)
{
return remove(x, m_root);
}
//递归移除
template <class T>
void BinarySearchTree<T>::remove(const T& x, BinaryNode<T>* &t)
{
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> *oldNode = t;
t = (t->left != NULL) ? t->left : t->right;
delete oldNode;
}
}
}
//清空二叉树
template <class T>
void BinarySearchTree<T>::makeEmpty()
{
makeEmpty(m_root);
}
//递归清空
template <class T>
void BinarySearchTree<T>::makeEmpty(BinaryNode<T>* &t)
{
if (t)
{
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
}
t = NULL;
}
// 打印二叉搜索树
template <class T>
void BinarySearchTree<T>::printTree(ORDER_MODE eOrderMode ) const
{
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 <class T>
void BinarySearchTree<T>::printTreeInPrev(BinaryNode<T>* t) const
{
if (t)
{
cout << t->element;
printTreeInPrev(t->left);
printTreeInPrev(t->right);
}
}
//中序打印
template <class T>
void BinarySearchTree<T>::printTreeInMid(BinaryNode<T>* t) const
{
if (t)
{
printTreeInPrev(t->left);
cout << t->element;
printTreeInPrev(t->right);
}
}
//后序打印
template <class T>
void BinarySearchTree<T>::printTreeInPost(BinaryNode<T>* t) const
{
if (t)
{
printTreeInPost(t->left);
printTreeInPost(t->right);
cout << t->element;
}
}
```
测试代码
===
```C++
#include "BinarySearchTree.h"
int main()
{
BinarySearchTree<int> binaryTree;
binaryTree.insert(5);
binaryTree.insert(1);
binaryTree.insert(2);
binaryTree.insert(3);
binaryTree.insert(6);
binaryTree.insert(8);
//测试前中后序打印
cout <<endl<<"前序:"<<endl;
binaryTree.printTree(ORDER_MODE_PREV);
cout <<endl<<"中序:"<<endl;
binaryTree.printTree(ORDER_MODE_MID);
cout <<endl<<"后序:"<<endl;
binaryTree.printTree(ORDER_MODE_POST);
cout <<endl;
//测试基本操作
bool b = binaryTree.contains(1);
cout<< "是否存在1:"<<b<<endl;
int x = binaryTree.findMin();
cout << "最小值为:"<< x <<endl;
x = binaryTree.findMax();
cout << "最大值为:"<< x <<endl;
binaryTree.remove(2);
cout << "移除元素2之后"<<endl;
//测试前中后序打印
cout <<endl<<"前序:"<<endl;
binaryTree.printTree(ORDER_MODE_PREV);
cout <<endl<<"中序:"<<endl;
binaryTree.printTree(ORDER_MODE_MID);
cout <<endl<<"后序:"<<endl;
binaryTree.printTree(ORDER_MODE_POST);
cout <<endl;
return 0;
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了C语言判定一棵二叉树是否为二叉搜索树的方法。分享给大家供大家参考,具体如下:问题给定一棵二叉树,判定该二叉树是否是二叉搜索树(BinarySear
JavaScript中的搜索二叉树实现,供大家参考,具体内容如下二叉搜索树(BST,BinarySearchTree),也称二叉排序树或二叉查找树二叉搜索树是一
本文实例讲述了Java二叉搜索树遍历操作。分享给大家供大家参考,具体如下:前言:在上一节Java二叉搜索树基础中,我们对树及其相关知识做了了解,对二叉搜索树做了
二叉排序树(BST)又称二叉查找树、二叉搜索树二叉排序树(BinarySortTree)又称二叉查找树。它或者是一棵空树;或者是具有下列性质的二叉树:1.若左子
什么是二叉树二叉树就是树的每个节点最多只能有两个子节点什么是二叉搜索树二叉搜索树在二叉树的基础上,多了一个条件,就是二叉树在插入值时,若插入值比当前节点小,就插