数据结构和算法:12.二叉搜索树

具体代码请看:NDKPractice项目的datastructure37binarysearchtree

1. 二叉搜索树的定义:

定义:比它(当前根节点)小的放左边,比它(当前根节点)大的放右边

普通二叉搜索树的中序遍历,就是从小到大的排序 (数据排序)

2.删除

当删除节点的左右两个子节点都不为 NULL 的情况下,从左边找最大值来替代或者从右边找最小值

3.代码

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
template<class K, class V>
struct TreeNode {
public:
TreeNode<K,V> *left = NULL;
TreeNode<K,V> *right = NULL;
K key = NULL;
V value = NULL;

TreeNode(TreeNode<K,V> *pNode){
this->left = pNode->left;
this->right = pNode->right;
this->key = pNode->key;
this->value = pNode->value;
}

TreeNode(K key, V value):key(key),value(value){
}

};


// 二叉搜索树
template<class K, class V>
class BST {
int count = 0; // 总个数
TreeNode<K,V> *root = NULL; // 根节点

public:
BST(){}

~BST(){
deleteAllNode(root);
root = NULL;
}

// 新增
void put(K key,V value){
root = addNode(root,key,value);
}

// 获取
V get(K key){
TreeNode<K,V> *node = root;
while (node){
if(node->key == key)
return node->value;
else if(node->key > key)
node = node->left;
else if(node->key < key)
node = node->right;
}
return NULL;
}

int size(){
return count;
}

// 是否包含
bool contains(K key){
TreeNode<K,V> *node = root;
while (node){
if(node->key == key)
return true;
else if(node->key > key)
node = node->left;
else if(node->key < key)
node = node->right;
}
return false;
}



// 移除
void remove(K key){
root = removeNode(root,key);
}

// 中序遍历 就是相当于从小到大的升序遍历了
void infixOrderTraverse(void(*log)(int,int)){
infixOrderTraverse(root,log);
}



private:
// 删除所有的数据
void deleteAllNode(TreeNode<K,V> *pNode){
if(pNode->left)
deleteAllNode(pNode->left);
if(pNode->right)
deleteAllNode(pNode->right);
delete(pNode);
}

TreeNode<K,V> *addNode(TreeNode<K,V> *pNode,K key,V value){
if(!pNode){
count++;
return new TreeNode<K,V>(key,value);
}

if(pNode->key > key){
pNode->left = addNode(pNode->left,key,value);
} else if (pNode->key < key){
pNode->right = addNode(pNode->right,key,value);
}else{
pNode->value = value;
}

return pNode;
}

TreeNode<K,V> *removeNode(TreeNode<K,V> *pNode,K key){
if(pNode->key > key)
pNode->left = removeNode(pNode->left,key);
else if(pNode->key < key)
pNode->right = removeNode(pNode->right);
else{ // 相等找到了
count --;
if(pNode->left == NULL && pNode->right == NULL){
delete(pNode);
return NULL;
}else if(pNode->left == NULL){
TreeNode<K, V> *right = pNode->right;
delete (pNode);
return right;
} else if (pNode->right == NULL) {
TreeNode<K, V> *left = pNode->left;
delete (pNode);
return left;
} else {
// 左右两子树都不为空(把左子树的最大值作为根,或者右子树的最小值作为根)
TreeNode<K, V> *successor = new TreeNode<K, V>(maximum(pNode->left));
successor->left = deleteMax(pNode->left);
count++;
successor->right = pNode->right;
delete (pNode);
return successor;
}
}
return pNode;
}


TreeNode<K, V> *deleteMax(TreeNode<K, V> *pNode) {
if (pNode->right == NULL) {
TreeNode<K, V> *left = pNode->left;
delete (pNode);
count--;
return left;
}
pNode->right = deleteMax(pNode->right);
return pNode;
}

// 查找当前树的左边的最大值 (右边的最小值怎么找?)
TreeNode<K, V> *maximum(TreeNode<K, V> *pNode) {
// 不断的往右边找,直到找到右子树为空节点
if (pNode->right == NULL) {
return pNode;
}
return maximum(pNode->left);
}

void infixOrderTraverse(TreeNode<K,V> *pNode,void(*log)(int,int)){
// 先左孩子
infixOrderTraverse(pNode->left,log);

// 再根节点
log(pNode->key,pNode->value);

// 再右孩子
infixOrderTraverse(pNode->right,log);
}
};
-------------本文结束感谢您的阅读-------------
坚持原创技术分享,您的支持将鼓励我继续创作!
0%