3
3
_ Read this in other languages:_
4
4
[ _ Português_ ] ( README.pt-BR.md )
5
5
6
- In computer science, ** binary search trees** (BST), sometimes called
7
- ordered or sorted binary trees, are a particular type of container:
8
- data structures that store "items" (such as numbers, names etc.)
9
- in memory. They allow fast lookup, addition and removal of
10
- items, and can be used to implement either dynamic sets of
11
- items, or lookup tables that allow finding an item by its key
6
+ In computer science, ** binary search trees** (BST), sometimes called
7
+ ordered or sorted binary trees, are a particular type of container:
8
+ data structures that store "items" (such as numbers, names etc.)
9
+ in memory. They allow fast lookup, addition and removal of
10
+ items, and can be used to implement either dynamic sets of
11
+ items, or lookup tables that allow finding an item by its key
12
12
(e.g., finding the phone number of a person by name).
13
13
14
- Binary search trees keep their keys in sorted order, so that lookup
15
- and other operations can use the principle of binary search:
16
- when looking for a key in a tree (or a place to insert a new key),
17
- they traverse the tree from root to leaf, making comparisons to
18
- keys stored in the nodes of the tree and deciding, on the basis
19
- of the comparison, to continue searching in the left or right
20
- subtrees. On average, this means that each comparison allows
21
- the operations to skip about half of the tree, so that each
22
- lookup, insertion or deletion takes time proportional to the
23
- logarithm of the number of items stored in the tree. This is
24
- much better than the linear time required to find items by key
25
- in an (unsorted) array, but slower than the corresponding
14
+ Binary search trees keep their keys in sorted order, so that lookup
15
+ and other operations can use the principle of binary search:
16
+ when looking for a key in a tree (or a place to insert a new key),
17
+ they traverse the tree from root to leaf, making comparisons to
18
+ keys stored in the nodes of the tree and deciding, on the basis
19
+ of the comparison, to continue searching in the left or right
20
+ subtrees. On average, this means that each comparison allows
21
+ the operations to skip about half of the tree, so that each
22
+ lookup, insertion or deletion takes time proportional to the
23
+ logarithm of the number of items stored in the tree. This is
24
+ much better than the linear time required to find items by key
25
+ in an (unsorted) array, but slower than the corresponding
26
26
operations on hash tables.
27
27
28
28
A binary search tree of size 9 and depth 3, with 8 at the root.
29
29
The leaves are not drawn.
30
30
31
- ![ Binary Search Tree] ( https://upload.wikimedia.org/wikipedia/commons/d/da/Binary_search_tree.svg )
31
+ ![ Trie] ( ./images/binary-search-tree.jpg )
32
+
33
+ * Made with [ okso.app] ( https://okso.app ) *
32
34
33
35
## Pseudocode for Basic Operations
34
36
@@ -45,7 +47,7 @@ insert(value)
45
47
end if
46
48
end insert
47
49
```
48
-
50
+
49
51
``` text
50
52
insertNode(current, value)
51
53
Pre: current is the node to start from
@@ -84,8 +86,8 @@ contains(root, value)
84
86
end if
85
87
end contains
86
88
```
87
-
88
-
89
+
90
+
89
91
### Deletion
90
92
91
93
``` text
@@ -186,7 +188,7 @@ findNode(root, value)
186
188
end if
187
189
end findNode
188
190
```
189
-
191
+
190
192
### Find Minimum
191
193
192
194
``` text
@@ -200,7 +202,7 @@ findMin(root)
200
202
findMin(root.left)
201
203
end findMin
202
204
```
203
-
205
+
204
206
### Find Maximum
205
207
206
208
``` text
@@ -214,7 +216,7 @@ findMax(root)
214
216
findMax(root.right)
215
217
end findMax
216
218
```
217
-
219
+
218
220
### Traversal
219
221
220
222
#### InOrder Traversal
@@ -244,7 +246,7 @@ preorder(root)
244
246
end if
245
247
end preorder
246
248
```
247
-
249
+
248
250
#### PostOrder Traversal
249
251
250
252
``` text
@@ -258,7 +260,7 @@ postorder(root)
258
260
end if
259
261
end postorder
260
262
```
261
-
263
+
262
264
## Complexities
263
265
264
266
### Time Complexity
0 commit comments