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
|
#ifndef BinaryTree_H
#define BinaryTree_H
template<typename T>
struct
BinaryNode
{
T element;
BinaryNode * left;
BinaryNode * right;
BinaryNode(
const
T & theElement,BinaryNode *lt = NULL,BinaryNode *rt = NULL):element(theElement),left(lt),right(rt) {}
};
template<typename T>
class
BinarySearchTree
{
public
:
BinarySearchTree();
BinarySearchTree(
const
BinarySearchTree & rhs);
~BinarySearchTree();
const
T & findMin()
const
;
const
T & findMax()
const
;
bool
contains(
const
T & x)
const
;
bool
isEmpty()
const
;
void
printTree()
const
;
void
makeEmpty();
void
insert(
const
T & x);
void
remove(
const
T & x);
const
BinarySearchTree &
operator
=(
const
BinarySearchTree & rhs);
int
countNodes()
const
;
int
countLeaves()
const
;
int
countFull()
const
;
bool
check()
const
;
//在树上查找k1<=Keys(X)<=k2的值
void
printRange(T & lower,T & upper,BinaryNode<T>* &root);
private
:
BinaryNode * root;
void
insert(
const
T & x,BinaryNode * &t)
const
{
if
(t == NULL)
t =
new
BinaryNode(x);
else
if
(x < t->element)
insert(x,t->left);
else
if
(x > t->element)
insert(x,t->right);
else
;
}
void
remove(
const
T & x,BinaryNode * &t)
const
{
if
(t == NULL)
return
;
if
(x < t->element)
remove(x,t->left);
else
if
(x > t->element)
remove(x,t->right);
else
if
(t->left != NULL && t->right != NULL)
//Two children
{
t->element = findMin(t->right)->element;
remove(t->element,t->right);
}
else
{
BinaryNode * oldNode = t;
t = (t->left != NULL) ? t->left : t->right;
delete oldNode;
}
}
BinaryNode * findMin(BinaryNode * t)
const
{
if
(t == NULL)
return
NULL;
else
{
while
(t->left != NULL)
t = t->left;
return
t;
}
}
BinaryNode * findMax(BinaryNode * t)
const
{
if
(t == NULL)
return
NULL;
else
{
while
(t->right != NULL)
t = t->right;
return
t;
}
}
bool
contains(
const
T & x,BinaryNode *t)
const
{
if
(t == NULL)
return
false
;
else
if
(x < t->element)
return
contains(x,t->left);
else
if
(c > t->element)
return
contains(x,t->right);
else
return
true
;
}
void
makeEmpty(BinaryNode *t)
const
{
if
(t == NULL)
;
else
{
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
t = NULL;
}
}
BinaryNode * clone(BinaryNode * t)
const
{
if
(t == NULL)
return
NULL;
else
return
new
BinaryNode(t->element,clone(t->left),clone(t->right));
}
void
printTree(BinaryNode * &t)
const
{
//这里采用中缀遍历
cout << t->element << endl;
printTree(t->left);
printTree(t->right);
}
//统计t中结点的个数
int
countNodes(BinaryNode * &t)
const
{
if
(t==NULL)
return
0;
else
return
countNodes(t->left)+countNodes(t->right)+1;
}
//统计t中树叶的片数
int
countLeaves(BinaryNode * &t)
const
{
if
(t == NULL)
return
0;
else
if
(t->left == NULL && t->right == NULL)
return
1;
else
return
countLeaves(t->left)+countLeaves(t->right);
}
//统计t中满结点的个数
int
countFull(BinaryNode * &t)
const
{
if
(t == NULL)
return
0;
int
tIsFull = (t->left != NULL && t->right != NULL) ? 1:0;
return
tIsFull + countFull(t->left) + countFull(t->right);
}
//测试一棵二叉树是否在每一个结点都满足查找树的序的性质
bool
check(BinaryNode * &t)
const
{
if
(t->left == NULL && t->right == NULL)
return
true
;
else
if
(!(t->element > findMin(t)->element && t->element < findMax(t)->element))
return
false
;
check(t->left);
check(t->right);
}
};
#endif
|
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
|
#include "BinaryTree.h"
#include <iostream>
using
namespace
std;
template<typename T>
BinarySearchTree<T>::BinarySearchTree()
{
root = NULL;
}
template<typename T>
BinarySearchTree<T>::BinarySearchTree(
const
BinarySearchTree & rhs)
{
root = NULL;
*
this
= rhs;
}
template<typename T>
BinarySearchTree<T>::~BinarySearchTree()
{
makeEmpty();
}
template<typename T>
const
T & BinarySearchTree<T>::findMin()
const
{
if
(!isEmpty())
return
findMin(root)->element;
}
template<typename T>
const
T & BinarySearchTree<T>::findMax()
const
{
if
(!isEmpty())
return
findMax(root)->element;
}
template<typename T>
bool
BinarySearchTree<T>::contains(
const
T & x)
const
{
return
contains(x,root);
}
template<typename T>
bool
BinarySearchTree<T>::isEmpty()
const
{
return
root == NULL;
}
template<typename T>
void
BinarySearchTree<T>::printTree()
const
{
if
(isEmpty())
cout <<
"Empty tree."
<< endl;
else
printTree(root);
}
template<typename T>
void
BinarySearchTree<T>::makeEmpty()
{
makeEmpty(root);
}
template<typename T>
void
BinarySearchTree<T>::insert(
const
T & x)
{
insert(x,root);
}
template<typename T>
void
BinarySearchTree<T>::remove(
const
T & x)
{
remove(x,root);
}
/* Deep Copy*/
template<typename T>
const
BinarySearchTree<T> &BinarySearchTree<T>::
operator
=(
const
BinarySearchTree<T> & rhs)
{
if
(
this
!= &rhs )
{
makeEmpty();
root = clone(rhs.root);
}
return
*
this
;
}
template<typename T>
int
BinarySearchTree<T>::countNodes()
const
{
return
countNodes(root);
}
template<typename T>
int
BinarySearchTree<T>::countLeaves()
const
{
return
countLeaves(root);
}
template<typename T>
int
BinarySearchTree<T>::countFull()
const
{
return
countFull(root);
}
template<typename T>
bool
BinarySearchTree<T>::check()
const
{
return
check(root);
}
template<typename T>
void
BinarySearchTree<T>::printRange(T & lower,T & upper,BinaryNode<T>* &root)
{
if
(root != NULL)
{
if
(lower <= t->element)
printRange(lower,upper,root->left);
if
(lower <= t->element && t->element >= upper)
cout << t->element << endl;
if
(t->element <= upper)
printRange(lower,upper,root->right);
}
}
|