二叉查找树的基类实现(五)
下面是树的实现,它的数据域只需要一个根节点
完成的主要工作是插入,删除和查找
BSTree.h 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
BSTree.cpp 文件
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 |
|
二叉查找树的基类实现(终)
下面举一个具体使用这个数据结构的例子,由于前面的缺点,这里
显得比较麻烦,也比较耗资源,但确实使用到了多态的好处
main.cpp 文件
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
|
#include <iostream>
#include "BSTree.h"
#include "IntClass.h"
#include "StringClass.h"
using
namespace
std;
int
main()
{
BSTree bstree;
BSTree bstreeStr;
bstree.insert(
new
IntClass(10),
new
StringClass(
"Tao"
,3));
bstree.insert(
new
IntClass(5),
new
StringClass(
"Tao"
,3));
bstree.insert(
new
IntClass(15),
new
StringClass(
"Lily"
,4));
bstree.insert(
new
IntClass(3),
new
StringClass(
"Tom"
,3));
bstree.insert(
new
IntClass(7),
new
StringClass(
"John"
,4));
bstree.insert(
new
IntClass(13),
new
StringClass(
"Peter"
,5));
bstree.insert(
new
IntClass(17),
new
StringClass(
"Joson"
,5));
bstree.insert(
new
IntClass(2),
new
StringClass(
"Tao"
,3));
bstree.insert(
new
IntClass(4),
new
StringClass(
"Tao"
,3));
bstree.insert(
new
IntClass(6),
new
StringClass(
"Tao"
,3));
bstree.insert(
new
IntClass(8),
new
StringClass(
"Lucy"
,4));
bstree.insert(
new
IntClass(11),
new
StringClass(
"Jim"
,3));
bstree.insert(
new
IntClass(14),
new
StringClass(
"Brown"
,5));
bstree.insert(
new
IntClass(16),
new
StringClass(
"Tao"
,3));
bstree.insert(
new
IntClass(18),
new
StringClass(
"Tao"
,3));
bstree.insert(
new
IntClass(1),
new
StringClass(
"Tao"
,3));
bstree.insert(
new
IntClass(9),
new
StringClass(
"Tao"
,3));
bstree.insert(
new
IntClass(12),
new
StringClass(
"Tao"
,3));
cout<<
"-------------------------------------------------"
<<endl;
cout<<
"The total tree is like this:"
<<endl;
bstree.outPut();
cout<<
"-------------------------------------------------"
<<endl;
cout<<
"The sub-tree is like this:"
<<endl;
bstree.ascend(bstree.get(
new
IntClass(15)));
cout<<
"-------------------------------------------------"
<<endl;
bstreeStr.insert(
new
StringClass(
"Jim"
,3),
new
StringClass(
"Hello, I'm a student"
,20));
bstreeStr.insert(
new
StringClass(
"Lucy"
,4),
new
StringClass(
"Hello, I'm a teacher"
,20));
bstreeStr.insert(
new
StringClass(
"Brown"
,5),
new
StringClass(
"Hello, I'm a doctor"
,19));
bstreeStr.insert(
new
StringClass(
"Lily"
,4),
new
StringClass(
"Hello, I'm a actor"
,18));
bstreeStr.insert(
new
StringClass(
"Tao"
,3),
new
StringClass(
"Hello, I'm a student"
,20));
bstreeStr.insert(
new
StringClass(
"Peter"
,5),
new
StringClass(
"Hello, I'm a teacher"
,20));
bstreeStr.insert(
new
StringClass(
"John"
,4),
new
StringClass(
"Hello, I'm a doctor"
,19));
bstreeStr.insert(
new
StringClass(
"Tony"
,4),
new
StringClass(
"Hello, I'm a actor"
,18));
bstreeStr.insert(
new
StringClass(
"Linda"
,5),
new
StringClass(
"Hello, I'm a student"
,20));
bstreeStr.insert(
new
StringClass(
"Jurcy"
,5),
new
StringClass(
"Hello, I'm a teacher"
,20));
bstreeStr.insert(
new
StringClass(
"Chern"
,5),
new
StringClass(
"Hello, I'm a doctor"
,19));
bstreeStr.insert(
new
StringClass(
"Rone"
,4),
new
StringClass(
"Hello, I'm a actor"
,18));
bstreeStr.outPut();
cout<<
"-------------------------------------------------"
<<endl;
}
|
1
|
输出结果如下:
|
附:前面要使用一个队列来进行树的层次遍历,这里将它给出,这是一个模板类
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
|
#ifndef QUEUE_H
#define QUEUE_H
#define MAXLEN 20
#include <assert.h>
template
<
class
T>
class
Queue
{
private
:
T data[MAXLEN];
int
head,end;
public
:
Queue();
bool
EnQueue(T next);
T DeQueue();
bool
isFull();
bool
isEmpty();
};
#endif
template
<
class
T>
Queue<T>::Queue()
{
head = 0;
end = 0;
}
template
<
class
T>
bool
Queue<T>::EnQueue(T next)
{
if
(isFull())
return
false
;
data[end] = next;
end=end+1;
if
(end >= MAXLEN)end = 0;
return
true
;
}
template
<
class
T>
T Queue<T>::DeQueue()
{
assert
(!isEmpty());
int
temp=head;
head=head+1;
if
(head>=MAXLEN)
head=0;
return
data[temp];
}
template
<
class
T>
bool
Queue<T>::isFull()
{
if
(end+1==head || (head==0 && end == MAXLEN-1))
return
true
;
return
false
;
}
template
<
class
T>
bool
Queue<T>::isEmpty()
{
if
(head == end)
return
true
;
return
false
;
}
|