根据二叉排序树的抽象数类型定义,使用链表实现一个二叉排序树。二叉排序树的基本功能: 1、建立 2、查找 3、插入 4、删除 5、销毁 编写测试main()函数测试算法的正确性。
时间: 2023-12-13 14:05:17 浏览: 91
好的,以下是使用链表实现二叉排序树的代码,包含基本功能和测试main()函数:
```c++
#include <iostream>
using namespace std;
// 二叉排序树的结点数据类型
typedef struct BSTNode {
int data;
struct BSTNode* lchild;
struct BSTNode* rchild;
} BSTNode, *BSTree;
// 初始化二叉排序树
void InitBSTree(BSTree& T) {
T = NULL;
}
// 向二叉排序树中插入一个结点
int InsertBSTree(BSTree& T, int key) {
if (!T) {
T = new BSTNode;
T->data = key;
T->lchild = T->rchild = NULL;
return 1;
}
else if (T->data > key) {
return InsertBSTree(T->lchild, key);
}
else if (T->data < key) {
return InsertBSTree(T->rchild, key);
}
else {
return 0; // 已经存在相同的结点,插入失败
}
}
// 在二叉排序树中查找一个结点
BSTree SearchBSTree(BSTree T, int key) {
if (!T || T->data == key) {
return T;
}
else if (T->data > key) {
return SearchBSTree(T->lchild, key);
}
else {
return SearchBSTree(T->rchild, key);
}
}
// 删除二叉排序树中的一个结点
int DeleteBSTree(BSTree& T, int key) {
if (!T) {
return 0; // 没有找到要删除的结点
}
else if (T->data > key) {
return DeleteBSTree(T->lchild, key);
}
else if (T->data < key) {
return DeleteBSTree(T->rchild, key);
}
else {
if (!T->lchild && !T->rchild) {
delete T;
T = NULL;
}
else if (T->lchild && !T->rchild) {
BSTree p = T;
T = T->lchild;
delete p;
}
else if (!T->lchild && T->rchild) {
BSTree p = T;
T = T->rchild;
delete p;
}
else {
BSTree p = T->lchild;
while (p->rchild) {
p = p->rchild;
}
T->data = p->data;
DeleteBSTree(T->lchild, p->data);
}
return 1;
}
}
// 销毁二叉排序树
void DestroyBSTree(BSTree& T) {
if (T) {
DestroyBSTree(T->lchild);
DestroyBSTree(T->rchild);
delete T;
T = NULL;
}
}
// 中序遍历二叉排序树
void InOrderBSTree(BSTree T) {
if (T) {
InOrderBSTree(T->lchild);
cout << T->data << " ";
InOrderBSTree(T->rchild);
}
}
// 测试main()函数
int main() {
BSTree T;
InitBSTree(T);
int arr[] = { 62, 88, 58, 47, 35, 73, 51, 99, 37, 93 };
int n = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < n; i++) {
InsertBSTree(T, arr[i]);
}
cout << "中序遍历二叉排序树:";
InOrderBSTree(T);
cout << endl;
int key = 58;
BSTree p = SearchBSTree(T, key);
if (p) {
cout << "查找到结点:" << p->data << endl;
}
else {
cout << "没有找到结点:" << key << endl;
}
key = 66;
int ret = DeleteBSTree(T, key);
if (ret) {
cout << "删除结点:" << key << " 成功!" << endl;
cout << "中序遍历二叉排序树:";
InOrderBSTree(T);
cout << endl;
}
else {
cout << "没有找到结点:" << key << ",删除失败!" << endl;
}
DestroyBSTree(T);
return 0;
}
```
运行结果:
```
中序遍历二叉排序树:35 37 47 51 58 62 73 88 93 99
查找到结点:58
没有找到结点:66,删除失败!
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)