#define _BST_H_
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW_s -2
typedef int Status;
typedef int TElemType;
{
TElemType data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
BiTree CreatBST(BiTree &T);
Status DestroyBST(BiTree &T);
BiTree SearchBST(BiTree T, TElemType key);
Status SearchBST(BiTree T, TElemType key, BiTree f, BiTree &p);
Status InsertBST(BiTree &T, TElemType e);
Status DeleteBST(BiTree &T, TElemType key);
Status Delete(BiTree &p);
void InOrderTraverse(BiTree T);
#endif
#include"BST.h"
#include<iostream>
using namespace std;
/*//建立二叉排序树算法1
BiTree CreatBST(BiTree &T)
{
TElemType key;
int i,n;
T = NULL;
cout << "请输入节点总数:" << endl;
cin>>n;
for (i = 1; i <= n;i++)
{
cin >> key;
InsertBST(T, key);
//InOrderTraverse(T);
}
return T;
}
*/
//建立二叉排序树算法2
BiTree CreatBST(BiTree &T)
{
TElemType key;
int i;
T = NULL;
cin >> key;
while (key != -1)//-1未输入结束标志
{
InsertBST(T, key);
cin >> key;
}
return T;
}
//销毁二叉树
Status DestroyBST(BiTree &T)
{
if (T)
{
DestroyBST(T->lchild);
DestroyBST(T->rchild);
T = NULL;
}
return OK;
}
//二叉排序树的查找
BiTree SearchBST(BiTree T, TElemType key)
{
if ((!T)||(T->data==key))
{
return T;
}
else
{
if (T->data > key)
{
return SearchBST(T->lchild, key);
}
else
{
return SearchBST(T->rchild, key);
}
}
}
//插入二叉排序树
Status InsertBST(BiTree &T, TElemType key)
{
BiTree p, s;
if (!SearchBST(T, key, NULL, p))
{
s = (BiTree)malloc(sizeof(BiTNode));
s->data = key;
s->lchild = s->rchild = NULL;
if (!p)
{
T = s;
}
else if (p->data > key)
{
p->lchild = s;
}
else
{
p->rchild = s;
}
return TRUE;
}
else
{
return FALSE;
}
}
//删除二叉排序树
Status DeleteBST(BiTree &T, TElemType key)
{
if (!T)
{
return FALSE;
}
else
{
if (T->data == key)
{
Delete(T);
return OK;
}
else if (T->data > key)
{
DeleteBST(T->lchild, key);
}
else
{
DeleteBST(T->rchild, key);
}
}
}
//删除节点
Status Delete(BiTree &p)
{
BiTree q, s;
if (!p->rchild)
{
q = p;
p = p->lchild;
free(q);
}
else if (!p->lchild)
{
q = p;
p = p->rchild;
free(q);
}
else
{
q = p;
s = p->lchild;
while (s->rchild)
{
q = s;
s = s->rchild;
}
p->data = s->data;
if (q != p)
{
q->rchild = s->lchild;
}
else
{
q->lchild = s->lchild;
delete s;
}
}
return TRUE;
}
//中序遍历
void InOrderTraverse(BiTree T)
{
if (T)
{
InOrderTraverse(T->lchild);
cout << T->data << " ";
InOrderTraverse(T->rchild);
}
}
// 查找(插入的辅助使用)
Status SearchBST(BiTree T, TElemType key, BiTree f, BiTree &p)
{
if (!T)// 查找不成功
{
p = f;
return FALSE;
}
else if (key == T->data) // 查找成功
{
p = T;
return TRUE;
}
else if (key < T->data) // 在左子树中继续查找
{
if (SearchBST(T->lchild, key, T, p) == TRUE)
return TRUE;
else
return FALSE;
}
else // 在右子树中继续查找
{
if (SearchBST(T->rchild, key, T, p) == TRUE)
return TRUE;
else
return FALSE;
}
}
#include<iostream>
using namespace std;
{
BiTree T;
int select,n;
TElemType key;
CreatBST(T);
do
{
cout << "1.查找二叉排序树!" << endl;
cout << "2.插入二叉排序树" << endl;
cout << "3.删除二叉排序树!" << endl;
cout << "4.中序遍历!" << endl;
cout << "0.操作结束!" << endl;
cout << "请选择:" << endl;
cin >> select;
switch (select)
{
case 1:
cout << "请输入值:" << endl;
cin >> key;
if (!SearchBST(T, key))
{
cout << "未找到!" << endl;
}
else
{
cout << "找到了!" << endl;
}
break;
case 2:
cout << "请输入要插入的值:" << endl;
cin >> key;
if (InsertBST(T, key) == TRUE)
{
cout << "插入成功!" << endl;
InOrderTraverse(T);
cout << endl;
}
else
{
cout << "插入失败!" << endl;
}
break;
case 3:
cout << "请输入要删除的值:" << endl;
cin >> key;
if (DeleteBST(T,key) == TRUE)
{
cout << "删除成功!" << endl;
}
else
{
cout << "二叉排序树中无该值!" << endl;
}
break;
case 4:
InOrderTraverse(T);
cout << endl;
break;
default:
cout << "输入错误!" << endl;
}
}while (select != 0);
}