-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySortSearch.cpp
More file actions
250 lines (198 loc) · 4.04 KB
/
BinarySortSearch.cpp
File metadata and controls
250 lines (198 loc) · 4.04 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* @Author: zhangkangbin
* @Date: 2022-09-20 20:30:39
* @LastEditors: zhangkangbin
* @LastEditTime: 2022-11-06 15:13:46
* @FilePath: \C_Study\chapter7_search\BinarySortSearch.cpp
* 二叉排序树,又称二叉查找树。
*/
#include <stdio.h>
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *left;
Node *right;
};
class Search
{
private:
// Node *mHead = NULL;
public:
Node *mHead = NULL;
bool searchData(int value);
bool recursionSearchData(Node *node, int value);
void addData(int value);
bool addData(Node *&node, int value);
bool addDataHead(int value);
};
bool Search::addDataHead(int value)
{
if (mHead == NULL)
{
mHead = new Node();
mHead->data = value;
mHead->left = mHead->right = NULL;
return true;
}
return addData(mHead, value);
}
/**
* 递归查找到,添加数据的位置。
*
* @param node
* @param value
* @return bool
*/
bool Search::addData(Node *&node, int value)
{
if (node == NULL)
{
node = new Node();
node->data = value;
node->left = node->right = NULL;
return true;
}
if (node->data == value)
{
//相同值,插入失败。其实可以改改。
return false;
}
//从左找插入点。
if (node->data > value)
{
return addData(node->left, value);
}
else
{
//从右找插入点。
return addData(node->right, value);
}
return false;
}
/**
* 非递归方式实现
*
* @param value
*/
void Search::addData(int value)
{
Node *child = new Node();
child->data = value;
if (mHead == NULL)
{
mHead = child;
mHead->data = value;
mHead->left = mHead->right = NULL;
return;
}
Node *p = mHead;
while (p)
{
if (p->data > value)
{
if (p->left == NULL)
{
child->left = child->right = NULL;
p->left = child;
return;
}
p = p->left;
}
else
{ //往右查找插入位置。
if (p->right == NULL)
{
child->left = child->right = NULL;
p->right = child;
return;
}
p = p->right;
}
}
}
/**
* 查找
*
* @param key
* @return boo
*/
bool Search::searchData(int key)
{
if (mHead == NULL)
{
return false;
}
Node *p = mHead;
while (p)
{
if (p->data == key)
{
cout << "\n find data: " << key << "\n";
return true;
}
//如果顶点大于key,那么key在左边。
if (p->data > key)
{
p = p->left;
}
else
{
p = p->right;
}
}
cout << "\n not find data: " << key << "\n";
return false;
}
/**
* 用递归,查找数据
*
* @param key
* @return true
* @return false
*/
bool Search::recursionSearchData(Node *node, int key)
{
if (node == NULL)
{
cout << "\n not find data: " << key << "\n";
return false;
}
if (node->data == key)
{
cout << "\n find data: " << key << "\n";
return true;
}
if (node->data > key)
{
return recursionSearchData(node->left, key);
}
else
{
return recursionSearchData(node->right, key);
}
}
int main()
{
int list[6] = {6, 5, 7, 3, 1};
Search search;
for (int i = 0; i < 5; i++)
{
// search.addData(list[i]);
//插入数据
search.addDataHead(list[i]);
}
/* search.searchData(7);
search.searchData(6);
search.searchData(1);
search.searchData(8); */
//递归查找数据
search.recursionSearchData(search.mHead, 7);
search.recursionSearchData(search.mHead, 6);
search.recursionSearchData(search.mHead, 1);
search.recursionSearchData(search.mHead, 8);
search.recursionSearchData(search.mHead, 0);
return 0;
}