Table of contents
- foreword
- What is a binary search tree
- Build a binary search tree
- Operations on Binary Search Trees
- Insert data into binary search tree
- Find data in a binary search tree
- Delete a node in a binary search tree
- predecessor successor node
- Three cases of deleting a node
- Implementation code
- full code
- Summarize
foreword
Earlier we introduced the data structure of the binary tree and the traversal algorithm of the binary tree. In this article, we will learn about a special binary tree—BST Binary Search Tree (BST Binary Search Tree), also called binary sorting tree and binary search tree.
What is a binary search tree
A binary search tree is first of all a binary tree, and it also meets the following characteristics:
- For any non-empty node, the value on its left subtree must be less than the current value ;
- For any non-empty node, the value on its right subtree must be greater than the current value ;
- Any subtree satisfies the above conditions;
As shown below:
The above picture is a binary search tree. Let’s take the root node as an example. The value of the root node is 71, and the values of its left subtree are 22, 35, 46, 53 and 66 respectively, which satisfy the left The subtree is smaller than the current value; the values of its right subtree are 78, 87 and 98 respectively, these values satisfy the requirement that the right subtree is greater than the current value; and so on, so the above figure is a binary search tree.
According to the characteristics of the binary search tree, we can also get the following conclusions:
- The left subtree and right subtree of any node of the binary search tree are a binary search tree;
- The smallest node of the binary search tree is the leaf node in the lower left corner of the entire tree ;
- The largest node of the binary search tree is the leaf node in the lower right corner of the whole tree ;
Build a binary search tree
We now use JavaScript to build a binary search tree. We must know that a binary search tree is also composed of nodes. Here we create class
a node class,
The sample code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Here a node consists of four parts, which are a pointer to the left subtree, a pointer to the right subtree, a pointer to the parent node, and the current value.
Operations on Binary Search Trees
We have already introduced the traversal operation of the binary tree in the previous article, and we will not repeat it here. Here we mainly introduce the following operations:
- insert operation
- find operation
- delete operation
Insert data into binary search tree
The idea of inserting data into a binary search tree is as follows:
- Determine
root
whether it is empty, if it is empty, create root; - If
root
it is not empty, you need to judge whether the inserted node is larger or smallerval
than the root node ;val
- If it is smaller than the root node, it means it is a node of the left subtree;
- If it is larger than the root node, it means it is the node of the right subtree;
- The above two steps are repeated until a point is found. If the point is smaller than the value we want to insert and there is no right subtree, use this point as its right leaf node; if the point is greater than the value we want to insert and does not exist For the right subtree, use this point as its left leaf node.
The sample code is as follows:
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 |
|
A method is defined in the class insertNode
, which accepts a value or an array of value types and inserts it into the binary search tree; for the insertion method, we define a private #insertNode
method for node insertion.
In order to see the effect, we define a static method here for in-order traversal (because the order of in-order traversal is left-root-right, in-order sorting is used in the binary search tree, and the final results are sorted from small to large ) this tree, and returns an array,
The sample code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
The test code is as follows:
const tree = new BinarySearchTree()
tree.insertNode([71, 35, 87, 22, 53, 46, 66, 78, 98])
const arr = BinarySearchTree.inorder(tree.root)
console.log(arr) // [ 22, 35, 46, 53, 66,71, 78, 87, 98 ]
The final tree structure is as follows:
Find data in a binary search tree
现在我们封装一个find
方法,用于查找二叉搜索树中的某个数据,假如我们查找66这个数据,利用上面那个树,
其查找思路如下图所示:
递归方式实现如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
迭代方式实现如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
两者相对来说,使用迭代的方式更优一些。
删除二叉搜索树的某个节点
前驱后继节点
在开始删除二叉搜索树中的某个节点之前,我们先来了解一下什么是前驱和后继节点;
- 前驱节点指的是使用中序遍历当前二叉搜索树时,当前节点的上一个节点就是前驱节点,换一种说法就是在二叉搜索树中,当前节点的左子树的最大值,就是该节点的前驱节点;
- 后继节点指的是使用中序遍历当前二叉搜索树时,当前节点的下一个节点就是后继节点,换一种说法就是在二叉搜索树中,当前节点的右子树的最小值,就是该节点的后继节点;
如下图所示:
了解了什么是前驱和后继节点之后,现在我们来开始删除某个节点。
删除一个节点的三种情况
当删除的节点是叶子节点时,只需要将指向它的指针修改为null
,即可,如下图所示:
当需要删除的节点存在一个子节点时, 需要将要删除节点的子节点的parent
指针指向要删除节点的父节点,然后将当前要删除节点的父节点指向子节点即可,
如下图所示:
当需要删除的节点存在一个子节点时, 删除步骤如下:
- 找到当前节点的前驱或者后继节点,这里选择后继;
- Then assign the value of the successor node to the current node;
- Delete the successor node.
As shown below:
Now that we have analyzed these situations, let's implement them through code.
Implementation code
The implementation code is as follows:
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 |
|
full code
The complete code in this article is as follows:
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 |
|