实验目的: ⑴ 熟悉二叉排序树的定义和基本操作 ⑵ 掌握二叉排序树的链式存储结构设计与基本操作的实现 实验内容: 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; } 运行情况: 实验体会: 通过本次实验,我熟悉二叉排序树的定义和基本操作,掌握二叉排序树的链式存储结构设计与基本操作的实现。
|
15二叉排序树的实现
最新推荐文章于 2024-11-06 11:05:01 发布