|
| 1 | +# Binary Search Tree |
| 2 | + |
| 3 | +In computer science, binary search trees (BST), sometimes called |
| 4 | +ordered or sorted binary trees, are a particular type of container: |
| 5 | +data structures that store "items" (such as numbers, names etc.) |
| 6 | +in memory. They allow fast lookup, addition and removal of |
| 7 | +items, and can be used to implement either dynamic sets of |
| 8 | +items, or lookup tables that allow finding an item by its key |
| 9 | +(e.g., finding the phone number of a person by name). |
| 10 | + |
| 11 | +Binary search trees keep their keys in sorted order, so that lookup |
| 12 | +and other operations can use the principle of binary search: |
| 13 | +when looking for a key in a tree (or a place to insert a new key), |
| 14 | +they traverse the tree from root to leaf, making comparisons to |
| 15 | +keys stored in the nodes of the tree and deciding, on the basis |
| 16 | +of the comparison, to continue searching in the left or right |
| 17 | +subtrees. On average, this means that each comparison allows |
| 18 | +the operations to skip about half of the tree, so that each |
| 19 | +lookup, insertion or deletion takes time proportional to the |
| 20 | +logarithm of the number of items stored in the tree. This is |
| 21 | +much better than the linear time required to find items by key |
| 22 | +in an (unsorted) array, but slower than the corresponding |
| 23 | +operations on hash tables. |
| 24 | + |
| 25 | +A binary search tree of size 9 and depth 3, with 8 at the root. |
| 26 | +The leaves are not drawn. |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +## References |
| 31 | + |
| 32 | +[Wikipedia](https://en.wikipedia.org/wiki/Binary_search_tree) |
0 commit comments