15二叉排序树的实现

实验目的:

⑴ 熟悉二叉排序树的定义和基本操作

⑵ 掌握二叉排序树的链式存储结构设计与基本操作的实现

实验内容:

1、采用书上第 127 页定义的二叉链表存储表示,编程实现二叉排序树的下列基本操作。

1)创建二叉排序树 2)二叉排序树的查找 3)二叉排序树的插入 4)二叉排序树的删除

程序清单:

#include<iostream>

using namespace std;

#include<malloc.h>

#define OK 1

#define ERROR 0

#define OVERFLOW -2

typedef int Status;

typedef char TElemType;

typedef int KeyType;

typedef struct

{

       KeyType key;

}ElemType;

typedef struct BiTNode

{

       ElemType data;

       BiTNode* lchild, * rchild;

}BiTNode, * BiTree;

BiTree SearchBST(BiTree T, KeyType key)

{

       if (!T || key==T->data.key)

       {

              return T;

       }

       else if (key < T->data.key)

       {

              return SearchBST(T->lchild, key);

       }

       else

              return SearchBST(T->rchild, key);

}

Status SearchBST1(BiTree T, KeyType key, BiTree f, BiTree& p)

{

       if (!T)

       {

              p = f;

              return ERROR;

       }

       else if (key==T->data.key)

       {

              p = T;

              return OK;

       }

       else if (key < T->data.key)

              return SearchBST1(T->lchild, key, T, p);

       else

              return SearchBST1(T->rchild, key, T, p);

}

Status InsertBST(BiTree& T, ElemType e)

{

       BiTree p = NULL;

       if (!SearchBST1(T,e.key,NULL,p))

       {

              BiTree s = (BiTree)malloc(sizeof(BiTNode));

              s->data = e;

              s->lchild = s->rchild = NULL;

              if (!p)

              {

                     T = s;

              }

              else if (e.key < p->data.key)

                     p->lchild = s;

              else

                     p->rchild = s;

              return OK;

       }

       else

              return ERROR;

}

Status PrintElement(ElemType data)

{

       cout << data.key << " ";

       return OK;

}

//先序遍历二叉排序树

Status PreOrderTraverse(BiTree T, Status(*Visit)(ElemType data))

{

       if (!T)

              return ERROR;

       else

       {

              Visit(T->data);

              PreOrderTraverse(T->lchild, Visit);

              PreOrderTraverse(T->rchild, Visit);

       }

       return OK;

}

//中序遍历二叉树

Status InOrderTraverse(BiTree T, Status(*Visit)(ElemType data))

{

       if (!T)

              return ERROR;

       else

       {

              InOrderTraverse(T->lchild, Visit);

              Visit(T->data);

              InOrderTraverse(T->rchild, Visit);

       }

       return OK;

}

//后序遍历二叉树

Status PostOrderTraverse(BiTree T, Status(*Visit)(ElemType data))

{

       if (!T)

              return ERROR;

       else

       {

              PostOrderTraverse(T->lchild, Visit);

              PostOrderTraverse(T->rchild, Visit);

              Visit(T->data);

       }

       return OK;

}

Status Delete(BiTree& p)

{

       BiTree q = NULL;

       BiTree s = NULL;

       if (!p->rchild)

       {

              q = p;

              p = p->lchild;

              delete q;

       }

       else if (!p->lchild)

       {

              q = p;

              p = p->rchild;

              delete 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 OK;

}

Status DeleteBST(BiTree& T, KeyType key)

{

       if (!T)

              return ERROR;

       else

       {

              if (key == T->data.key)

                     return Delete(T);

              else if (key < T->data.key)

              {

                     return DeleteBST(T->lchild, key);

              }

              else

                     return DeleteBST(T->rchild, key);

       }

}

int main()

{

       BiTree T = NULL, s;

       int n, i;

       ElemType e;

       cout << "输入二叉排序树的结点数:";

       cin >> n;

       cout << "输入" << n << "个结点的值:" << endl;

       for (i = 1; i <= n; i++)

       {

              cin >> e.key;

              InsertBST(T, e);

       }

       cout << "先序遍历二叉排序树,结果是:" << endl;

       PreOrderTraverse(T, PrintElement);

       cout << endl;

       cout << "中序遍历二叉排序树,结果是:" << endl;

       InOrderTraverse(T, PrintElement);

       cout << endl;

       cout << "后序遍历二叉排序树,结果是:" << endl;

       PostOrderTraverse(T, PrintElement);

       cout << endl;

       cout << "输入查找结点的值:" << endl;

       cin >> e.key;

       s = SearchBST(T, e.key);

       if (s)

              printf("已找到!结点的八进制地址为:%o\n", s);

       else

              printf("没找到!\n");

       cout << "输入插入结点的值:" << endl;

       cin >> e.key;

       InsertBST(T, e);

       cout << "先序遍历二叉排序树,结果是:" << endl;

       PreOrderTraverse(T, PrintElement);

       cout << endl;

       cout << "中序遍历二叉排序树,结果是:" << endl;

       InOrderTraverse(T, PrintElement);

       cout << endl;

       cout << "后序遍历二叉排序树,结果是:" << endl;

       PostOrderTraverse(T, PrintElement);

       cout << endl;

       cout << "输入删除结点的值:" << endl;

       cin >> e.key;

       DeleteBST(T, e.key);

       cout << "先序遍历二叉排序树,结果是:" << endl;

       PreOrderTraverse(T, PrintElement);

       cout << endl;

       cout << "中序遍历二叉排序树,结果是:" << endl;

       InOrderTraverse(T, PrintElement);

       cout << endl;

       cout << "后序遍历二叉排序树,结果是:" << endl;

       PostOrderTraverse(T, PrintElement);

       cout << endl;

       return 0;

}

运行情况:

实验体会:

通过本次实验,我熟悉二叉排序树的定义和基本操作,掌握二叉排序树的链式存储结构设计与基本操作的实现。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值