二叉查找树性质:对于树中的每个结点X,它的左子树中所有项元素的值小于X中的项,而它的右子树中所有项的值大于X中的项。简单的介绍一些操作:
(1)insert (插入)
先遍历树,如果找到要插入的值X,则什么也不做。否则将X插入到遍历路径的最后一点上,其算法实现采用递归。X小于t->element,向左子树遍历,X大于t->element,向右子树遍历,直到t==NULL,插入X。
(2)remove(删除)
如果结点是叶子结点,直接删除;如果结点有一个儿子,将儿子结点指针赋值给父亲结点指针,再把原父亲结点指针删除;如果结点有2个儿子,一般删除策略是用其右子树的最小的数据代替该结点的数据并递归地删除那个最小数据结点(这种策略的依据是右子树的所有元素大于该结点的元素,所以必须找右子树中最小元素的填充该结点;或者左子树中最大的填充该结点)
(3)前序遍历输出:它的诸儿子结点输出之前被输出(结点,左,右)
(4)中序遍历输出:左,结点,右
(5)左,右,结点
实现代码如下:
Adt.h
#pragma once
template <typename Comparable>
struct BinaryNode
{
Comparable element;
BinaryNode *left;
BinaryNode *right;
BinaryNode(Comparable theElement,BinaryNode *le,BinaryNode *rt):element(theElement),left(le),right(rt){}
};
enum MODE
{
PRE,
MID,
POST
};
template <typename Comparable>
class BinarySearchTree
{
public:
BinarySearchTree();
BinarySearchTree(BinarySearchTree &rhs);
~BinarySearchTree();
Comparable findmin(); //查找最小
Comparable findmax(); //查找最大
void insert(Comparable x);//插入元素
void remove(Comparable x);//删除指定元素
bool contains(Comparable x);//是否包含某一元素
void makeEmpty(BinaryNode<Comparable> *&t);
void printtree(MODE mode); //打印该树,方式有:前序遍历、中序遍历、后序遍历
private:
BinaryNode<Comparable>*root;
BinaryNode<Comparable> *findmin(BinaryNode<Comparable> *t);
BinaryNode<Comparable> *findmax(BinaryNode<Comparable> *t);
void insert(Comparable x,BinaryNode<Comparable> *&t);//t是指针引用,否则形参的变换不会影响到实参的变换
void remove(Comparable x,BinaryNode<Comparable> *&t);
bool contains(Comparable x,BinaryNode<Comparable> *t);
void printpre(BinaryNode<Comparable> *t); //前序遍历
void printmid(BinaryNode<Comparable> *t); //中序遍历
void printpost(BinaryNode<Comparable> *t); //后序遍历
};
Adt.cpp
#include "stdafx.h"
#include"Adt.h"
#include<iostream>
using namespace std;
template <typename Comparable>
BinarySearchTree<Comparable>::BinarySearchTree()
{
root=NULL;
}
template <typename Comparable>
BinarySearchTree<Comparable>::BinarySearchTree(BinarySearchTree &rhs)
{
root=rhs.root;
}
template <typename Comparable>
BinarySearchTree<Comparable>::~BinarySearchTree()
{
makeEmpty(root);
}
template <typename Comparable>
void BinarySearchTree<Comparable>::makeEmpty(BinaryNode<Comparable> *&t)
{
if(t!=NULL)
{
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
}
t=NULL;
}
//public中函数
template <typename Comparable>
Comparable BinarySearchTree<Comparable>::findmin() //最小元素一定在左子树中,所以一直向左子树遍历,直到最后一个,便是最小的
{
return findmin(root)->element;
}
template <typename Comparable>
Comparable BinarySearchTree<Comparable>::findmax()//最大元素一定在右子树中,所以一直向右子树遍历,直到最后一个,便是最大的
{
return findmax(root)->element;
}
template <typename Comparable>
void BinarySearchTree<Comparable>::insert(Comparable x)
{
insert(x,root);
}
template <typename Comparable>
void BinarySearchTree<Comparable>::remove(Comparable x)
{
remove(x,root);
}
template <typename Comparable>
bool BinarySearchTree<Comparable>::contains(Comparable x)
{
return contains(x,root);
}
template <typename Comparable>
void BinarySearchTree<Comparable>::printtree(MODE mode)
{
if(mode==PRE)
printpre(root);
else if(mode==MID)
printmid(root);
else if(mode==POST)
printpost(root);
else
;
}
//private
template <typename Comparable>
BinaryNode<Comparable>* BinarySearchTree<Comparable>::findmin(BinaryNode<Comparable> *t)
{
if(t==NULL)
return NULL;
if(t->left==NULL)
return t;
else
findmin(t->left);
}
template <typename Comparable>
BinaryNode<Comparable>* BinarySearchTree<Comparable>::findmax(BinaryNode<Comparable> *t)
{
if(t==NULL)
return NULL;
if(t->right==NULL)
return t;
else
findmax(t->right);
}
template <typename Comparable>
void BinarySearchTree<Comparable>::insert(Comparable x, BinaryNode<Comparable>* &t)
{
if(t==NULL)
t=new BinaryNode<Comparable>(x,NULL,NULL);
else if(x<t->element)
insert(x,t->left);
else if(x>t->element)
insert(x,t->right);
else
;
}
template <typename Comparable>
void BinarySearchTree<Comparable>::remove(Comparable x, BinaryNode<Comparable> *&t)
{
if(t==NULL)
return ;
else 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)
{
t->element=findmin(t->right)->element;
remove(t->element,t->right);
}
else
{
BinaryNode<Comparable> *oldNode=t;
t=(t->left !=NULL) ? t->left:t->right;
delete oldNode;
}
}
template <typename Comparable>
bool BinarySearchTree<Comparable>::contains(Comparable x, BinaryNode<Comparable> *t)
{
if(t==NULL)
return false;
else if(x<t->element)
contains(x,t->left);
else if(x>t->element)
contains(x,t->right);
else
return true;
}
template <typename Comparable>
void BinarySearchTree<Comparable>::printpre(BinaryNode<Comparable> *t)
{
if(t!=NULL)
{
cout<<t->element<<" ";
printpre(t->left);
printpre(t->right);
}
}
template <typename Comparable>
void BinarySearchTree<Comparable>::printmid(BinaryNode<Comparable> *t)
{
if(t!=NULL)
{
printmid(t->left);
cout<<t->element<<" ";
printmid(t->right);
}
}
template <typename Comparable>
void BinarySearchTree<Comparable>::printpost(BinaryNode<Comparable> *t)
{
if(t!=NULL)
{
printpost(t->left);
printpost(t->right);
cout<<t->element<<" ";
}
}
AlgorithmADT.cpp
#include "stdafx.h"
#include"Adt.h"
#include"Adt.cpp"
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
BinarySearchTree<int> tree;
tree.insert(6);
tree.insert(8);
tree.insert(2);
tree.insert(1);
tree.insert(4);
tree.insert(3);
tree.printtree(PRE);//前序遍历
cout<<endl;
tree.printtree(MID);//中序遍历
cout<<endl;
tree.printtree(POST);//后序遍历
return 0;
}