.h文件:
/*二叉查找(排序)树*/
struct BTreeNode
{
ElemType data;
BTreeNode *left;
BTreeNode *right;
};
//#define FLAG
#ifdef FLAG
//二叉排序树的查找(递归)
int Find(BTreeNode *BST,ElemType &item)
{
if(BST == NULL)
return 0;
if(item.key == BST->data.key)
{
item = BST->data;
return 1;
}
else if(item.key < BST->data.key)
return Find(BST->left,item);
else if(item.key > BST->data.key)
return Find(BST->right,item);
}
//二叉查找树的插入(假设不允许具有相同值的结点存在)(递归)
void Insert(BTreeNode* &BST,const ElemType &item)
{
if(BST == NULL)
{
BTreeNode *p = new BTreeNode;
p->data = item;
p->left = p->right = NULL;
BST = p;
}
else if(item.key < BST->data.key)
Insert(BST->left,item);
else if(item.key > BST->data.key)
Insert(BST->right,item);
}
#endif
#ifndef FLAG
//二叉排序树的查找(非递归)
int Find(BTreeNode *BST,ElemType &item)
{
while(BST != NULL)
{
if(item.key == BST->data.key)
{
item = BST->data;
return 1;
}
else if(item.key < BST->data.key)
BST = BST->left;
else if(item.key > BST->data.key)
BST = BST->right;
}
return 0;
}
//二叉查找树的插入(假设不允许具有相同值的结点存在)(非递归)
void Insert(BTreeNode* &BST,const ElemType &item)
{
BTreeNode *t = BST; //指向当前待比较的结点
BTreeNode *parent = NULL; //指向t结点的双亲节点
while(t != NULL)
{
parent = t;
if(item.key < t->data.key)
t = t->left;
else
t= t->right;
}
BTreeNode *p = new BTreeNode;
p->data = item;
p->left = p->right = NULL;
if(parent == NULL)
BST = p;
else if(item.key < parent->data.key)
parent->left = p;
else if(item.key > parent->data.key)
parent->right = p;
}
#endif
//二叉排序树的更新(不可使用非递归的方法)
int Update(BTreeNode* &BST,const ElemType &item)
{
if(BST == NULL)
return 0;
if(item.key == BST->data.key)
{
BST->data = item;
return 1;
}
else if(item.key < BST->data.key)
return Update(BST->left,item);
else if(item.key > BST->data.key)
return Update(BST->right,item);
}
//二叉查找树的生成
void CreateBSTree(BTreeNode* &BST,ElemType a[],int n)
{
BST = NULL;
for(int i = 0; i < n; i++)
Insert(BST,a[i]);
}
//中序遍历
void InOrder(BTreeNode* BST)
{
if(BST != NULL)
{
InOrder(BST->left);
cout<<"("<<BST->data.key<<","<<BST->data.rest<<") ";
InOrder(BST->right);
}
}
//二叉查找树的删除
int Delete(BTreeNode* &BST,const ElemType &item)
{
BTreeNode *t = BST; //指向待比较的结点
BTreeNode *s = NULL; //指向t的双亲结点,从根结点开始比较
while(t != NULL)
{
if(item.key == t->data.key)
break;
else if(item.key < t->data.key)
{
s = t;
t = t->left;
}
else if(item.key > t->data.key)
{
s = t;
t = t->right;
}
}
if(t == NULL)
return 0;
/*分三种情况删除已查找到的t结点*/
//t结点为叶子节点
if(t->left == NULL && t->right == NULL)
{
if(t == BST)
BST = NULL;
else if(t == s->left)
s->left = NULL;
else if(t == s->right)
s->right = NULL;
delete t;
}
//t结点为单分支结点
else if(t->left == NULL || t->right == NULL)
{
//t结点为根结点
if(t == BST)
{
if(t->left == NULL)
BST = t->right;
else if(t->right == NULL)
BST = t->left;
}
else //t结点不是根结点,分四种情况
{
if(t == s->left && t->left != NULL)
s->left = t->left;
else if(t == s->left && t->right != NULL)
s->left = t->right;
else if(t == s->right && t->left != NULL)
s->right = t->left;
else if(t == s->right && t->right != NULL)
s->right = t->right;
}
delete t;
}
//t结点为双分支结点
else if(t->left != NULL && t->right != NULL)
{
BTreeNode *p = t; //p初始指向t结点
BTreeNode *q = t->left; //q初始指向p结点的左子树
//查找t结点的中序后继结点,查找结束后q结点为t结点的中序后继结点,p结点为q结点的双亲结点
while(q->right != NULL)
{
p = q;
q = q->right;
}
t->data = q->data;
//删除右子树为空的q结点,使得它的右子树链接到它所在的链接位置
if(p == t)
t->left = q->left;
else
p->right = q->left;
delete q;
}
return 1;
}
.cpp文件:
/*二叉查找树应用举例*/
#include <iostream>
using namespace std;
typedef struct student
{
int key; //整型学号域作为关键字域
int rest; //其他整型域
}ElemType;
#include "BSTree.h"
int main()
{
ElemType a[8];
for(int i = 0; i < 8; i++)
{
cin>>a[i].key>>a[i].rest;
}
BTreeNode *bst = NULL;
ElemType x = {28};
ElemType y = {20,37};
CreateBSTree(bst,a,8);
cout<<"中序遍历:"<<endl;
InOrder(bst);
cout<<endl;
if(Find(bst,x))
cout<<"查找成功!得到的x为:("<<x.key <<","<<x.rest<<")"<<endl;
if(Update(bst,y))
{
cout<<"更新成功:"<<endl;
InOrder(bst);
}
cout<<endl;
Delete(bst,x);
Delete(bst,y);
cout<<"删除关键字为28和20的元素后的中序遍历为:"<<endl;
InOrder(bst);
cout<<endl;
return 0;
}
/*
输入:
30 50 20 70 25 80 23 40 28 50 15 90 60 12 48 60
*/