-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.cpp
More file actions
114 lines (86 loc) · 2.12 KB
/
BST.cpp
File metadata and controls
114 lines (86 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include<iostream>
using namespace std;
#define ENDFLAG 999
//数据结构定义
typedef struct BSTNode {
int data; //结点数据域
BSTNode* lchild, * rchild; //左右孩子指针
}BSTNode, * BSTree;
//操作实现
// 二叉排序树的递归查找
BSTree SearchBST(BSTree T, int key) {
if ((!T) || key == T->data)
return T;
else if (key < T->data)
return SearchBST(T->lchild, key);
else return SearchBST(T->rchild, key);
}
//算法7.5 二叉排序树的插入
void InsertBST(BSTree& T, int e) {
//当二叉排序树T中不存在关键字等于e.key的数据元素时,则插入该元素
if (!T) {
BSTree S;
S = new BSTNode;
S->data = e;
S->lchild = S->rchild = NULL;
T = S;
}
else if (e < T->data)
InsertBST(T->lchild, e);
else if (e > T->data)
InsertBST(T->rchild, e);
}
// 二叉排序树的创建
void CreateBST(BSTree& T) {
//依次读入一个关键字为key的结点,将此结点插入二叉排序树T中
T = NULL;
int e;
cout << "输入结点信息,输入‘999’结束" << endl;
cin >> e;
while (e != ENDFLAG) {
InsertBST(T, e);
cin >> e;
}
}
//中序遍历验证BST树是否为递增序列
void InOrderTraverse(BSTree& T) {
if(T){
InOrderTraverse(T->lchild);
cout << T->data << " ";
InOrderTraverse(T->rchild);
}
}
int main()
{
BSTree T = NULL;
int choose=-1,x;
cout << " 注意:先执行1号操作" << endl;
while (choose != 0) {
cout << " 【1.创建一个二叉排序树 2.中序遍历二叉树 3.查找 4.插入 0.退出 】" << endl;
cout << " >>>请输入要进行的操作:" << endl;
cin >> choose;
switch (choose) {
case 1:
CreateBST(T);
cout << "创建成功!已保存。" << endl;
break;
case 2:
InOrderTraverse(T);
cout << endl;
break;
case 3:
cout << "输入要查找的信息:" << endl;
cin >> x;
if (SearchBST(T, x))
cout << "查找成功!" << endl;
else cout << "查找失败,没有此元素!" << endl;
break;
case 4:
cout << "输入要插入的元素:" << endl;
cin >> x;
InsertBST(T, x);
cout << "插入成功,已保存。" << endl;
}
}
return 0;
}