Skip to content

Commit dac5c15

Browse files
committed
添加二叉查找树数据结构代码
1 parent 83b4cc1 commit dac5c15

File tree

5 files changed

+238
-0
lines changed

5 files changed

+238
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*test*
2+
3+
*.o
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 二叉查找树
2+
3+
## 实现
4+
5+
**二叉查找树**数据结构的实现,存储元素值类型设定为`int`,且**不存储重复元素**
6+
7+
|编程语言 |文件名 |
8+
|:----------|:----------------------|
9+
|`C++` |`bst.cpp`, `bst.hpp` |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/* 二叉查找树 - 数据结构
2+
* Binary Search Tree - Data Structure
3+
*
4+
* 该二叉查找树的实现不存储重复元素。
5+
*/
6+
7+
#include "bst.hpp"
8+
9+
/* 二叉查找树初始化
10+
* 初始化为空树
11+
*/
12+
BSTree::BSTree(void)
13+
{
14+
this->root = NULL;
15+
return;
16+
}
17+
18+
/* 二叉查找树初始化
19+
* 根据给定值初始化根节点
20+
*/
21+
BSTree::BSTree(const ValueType key)
22+
{
23+
this->root = new BSTreeNode(key);
24+
return;
25+
}
26+
27+
/* 二叉查找树销毁
28+
* 递归销毁子树及其存储值并释放其内存
29+
*/
30+
BSTree::~BSTree(void)
31+
{
32+
if (root != NULL) delete this->root;
33+
return;
34+
}
35+
36+
/* 二叉查找树查找
37+
* 在该二叉查找树中查找是否存在指定值的元素
38+
* 存在则返回对应二叉查找树节点指针,否则返回NULL
39+
*/
40+
BSTreeNode *BSTree::Find(const ValueType key)
41+
{
42+
BSTreeNode *node = this->root;
43+
44+
while (node != NULL && node->value != key) {
45+
node = key < node->value ? node->left : node->right;
46+
}
47+
return node;
48+
}
49+
50+
/* 二叉查找树查找最小元素值
51+
* 在该二叉查找树中查找出存储最小元素的节点并返回其指针
52+
*/
53+
BSTreeNode *BSTree::FindMin(void)
54+
{
55+
BSTreeNode *node = this->root;
56+
57+
while (node != NULL && node->left != NULL) {
58+
node = node->left;
59+
}
60+
return node;
61+
}
62+
63+
/* 二叉查找树查找最大元素值
64+
* 在该二叉查找树中查找出最大元素值并返回
65+
*/
66+
BSTreeNode *BSTree::FindMax(void)
67+
{
68+
BSTreeNode *node = this->root;
69+
70+
while (node != NULL && node->right != NULL) {
71+
node = node->right;
72+
}
73+
return node;
74+
}
75+
76+
/* 二叉查找树插入
77+
* 在该二叉查找树中插入一个元素
78+
* 如果该元素已经存在则不做操作
79+
*/
80+
void BSTree::Insert(const ValueType key)
81+
{
82+
if (this->root == NULL) {
83+
this->root = new BSTreeNode(key);
84+
return;
85+
}
86+
87+
BSTreeNode *node = this->root;
88+
89+
while (node->value != key) {
90+
if (key < node->value) {
91+
node = node->left == NULL ? new BSTreeNode(key) : node->left;
92+
} else {
93+
node = node->right == NULL ? new BSTreeNode(key) : node->right;
94+
}
95+
}
96+
return;
97+
}
98+
99+
/* 二叉查找树删除
100+
* 在该二叉查找树中删除元素
101+
* 如果不存在对应元素则不删除任何元素直接返回
102+
*/
103+
void BSTree::Delete(const ValueType key)
104+
{
105+
if (this->root != NULL) this->root = this->root->Delete(key);
106+
return;
107+
}
108+
109+
/* 二叉查找树节点初始化
110+
* 存储值设置为给定值
111+
*/
112+
BSTreeNode::BSTreeNode(const ValueType key)
113+
{
114+
this->value = key;
115+
this->left = NULL;
116+
this->right = NULL;
117+
return;
118+
}
119+
120+
/* 二叉查找树节点销毁
121+
* 将会递归销毁子树并释放内存
122+
*/
123+
BSTreeNode::~BSTreeNode(void)
124+
{
125+
if (this->left != NULL) delete this->left;
126+
if (this->right != NULL) delete this->right;
127+
return;
128+
}
129+
130+
/* 二叉查找树节点删除
131+
* 在以该二叉查找树节点为根节点的树中删除元素
132+
* 返回删除元素后的新二叉查找树根节点指针
133+
* 没有待删除元素则不做操作直接返回自身
134+
*/
135+
BSTreeNode *BSTreeNode::Delete(const ValueType key)
136+
{
137+
// 待删除节点不是自己
138+
if (key < this->value) {
139+
if (this->left != NULL) {
140+
this->left = this->left->Delete(key);
141+
}
142+
return this;
143+
}
144+
if (key > this->value) {
145+
if (this->right != NULL) {
146+
this->right = this->right->Delete(key);
147+
}
148+
return this;
149+
}
150+
151+
// 待删除节点是自己
152+
153+
// 待删除节点左右子树都不为空
154+
if (this->left != NULL && this->right != NULL) {
155+
// 查找右子树最小元素节点
156+
BSTreeNode *min_node = this->right;
157+
while (min_node->left != NULL) {
158+
min_node = min_node->left;
159+
}
160+
// 更新与删除
161+
this->value = min_node->value;
162+
this->right = this->right->Delete(min_node->value);
163+
return this;
164+
}
165+
166+
// 待删除节点为叶子节点
167+
if (this->left == NULL && this->right == NULL) {
168+
delete this;
169+
return NULL;
170+
}
171+
172+
// 待删除节点仅有一颗子树
173+
BSTreeNode *tmp = this->left == NULL ? this->right : this->left;
174+
delete this;
175+
return tmp;
176+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* 二叉查找树 - 数据结构
2+
* Binary Search Tree - Data Structure
3+
*
4+
* 该二叉查找树的实现不存储重复元素。
5+
*/
6+
7+
#ifndef _BINARY_SEARCH_TREE_CPP_
8+
#define _BINARY_SEARCH_TREE_CPP_
9+
10+
#include <stdio.h>
11+
12+
typedef int ValueType;
13+
14+
class BSTree;
15+
class BSTreeNode;
16+
17+
class BSTree {
18+
public:
19+
BSTree(void);
20+
BSTree(const ValueType key);
21+
~BSTree(void);
22+
BSTreeNode *Find(const ValueType key);
23+
BSTreeNode *FindMin(void);
24+
BSTreeNode *FindMax(void);
25+
void Insert(const ValueType key);
26+
void Delete(const ValueType key);
27+
private:
28+
BSTreeNode *root;
29+
};
30+
31+
class BSTreeNode {
32+
friend class BSTree;
33+
public:
34+
BSTreeNode(const ValueType key);
35+
~BSTreeNode(void);
36+
37+
ValueType value;
38+
BSTreeNode *left;
39+
BSTreeNode *right;
40+
private:
41+
BSTreeNode *Delete(const ValueType key);
42+
};
43+
44+
#endif
+6
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
# 算法与数据结构
2+
3+
## 列表
4+
5+
|算法与数据结构 |文件夹 |
6+
|:--------------|:------------------|
7+
|二叉查找树 |`BinarySearchTree` |

0 commit comments

Comments
 (0)