|
1 | 1 | # Binary Search Tree (BST)
|
2 | 2 |
|
3 |
| -A binary search tree is a special kind of [binary tree](../Binary Tree/) (a tree in which each node has at most two children) that performs insertions and deletions such that the tree is always sorted. |
| 3 | +A binary search tree is a special kind of [binary tree](../Binary%20Tree/) (a tree in which each node has at most two children) that performs insertions and deletions such that the tree is always sorted. |
4 | 4 |
|
5 | 5 | If you don't know what a tree is or what it is for, then [read this first](../Tree/).
|
6 | 6 |
|
@@ -49,7 +49,7 @@ If we were looking for the value `5` in the example, it would go as follows:
|
49 | 49 |
|
50 | 50 | 
|
51 | 51 |
|
52 |
| -Thanks to the structure of the tree, searching is really fast. It runs in **O(h)** time. If you have a well-balanced tree with a million nodes, it only takes about 20 steps to find anything in this tree. (The idea is very similar to [binary search](../Binary Search) in an array.) |
| 52 | +Thanks to the structure of the tree, searching is really fast. It runs in **O(h)** time. If you have a well-balanced tree with a million nodes, it only takes about 20 steps to find anything in this tree. (The idea is very similar to [binary search](../Binary%20Search) in an array.) |
53 | 53 |
|
54 | 54 | ## Traversing the tree
|
55 | 55 |
|
@@ -535,7 +535,7 @@ The code for `successor()` works the exact same way but mirrored:
|
535 | 535 |
|
536 | 536 | Both these methods run in **O(h)** time.
|
537 | 537 |
|
538 |
| -> **Note:** There is a cool variation called a ["threaded" binary tree](../Threaded Binary Tree) where "unused" left and right pointers are repurposed to make direct links between predecessor and successor nodes. Very clever! |
| 538 | +> **Note:** There is a cool variation called a ["threaded" binary tree](../Threaded%20Binary%20Tree) where "unused" left and right pointers are repurposed to make direct links between predecessor and successor nodes. Very clever! |
539 | 539 |
|
540 | 540 | ### Is the search tree valid?
|
541 | 541 |
|
@@ -713,11 +713,11 @@ The root node is in the middle; a dot means there is no child at that position.
|
713 | 713 |
|
714 | 714 | A binary search tree is *balanced* when its left and right subtrees contain roughly the same number of nodes. In that case, the height of the tree is *log(n)*, where *n* is the number of nodes. That's the ideal situation.
|
715 | 715 |
|
716 |
| -However, if one branch is significantly longer than the other, searching becomes very slow. We end up checking way more values than we'd ideally have to. In the worst case, the height of the tree can become *n*. Such a tree acts more like a [linked list](../Linked List/) than a binary search tree, with performance degrading to **O(n)**. Not good! |
| 716 | +However, if one branch is significantly longer than the other, searching becomes very slow. We end up checking way more values than we'd ideally have to. In the worst case, the height of the tree can become *n*. Such a tree acts more like a [linked list](../Linked%20List/) than a binary search tree, with performance degrading to **O(n)**. Not good! |
717 | 717 |
|
718 | 718 | One way to make the binary search tree balanced is to insert the nodes in a totally random order. On average that should balance out the tree quite nicely. But it doesn't guarantee success, nor is it always practical.
|
719 | 719 |
|
720 |
| -The other solution is to use a *self-balancing* binary tree. This type of data structure adjusts the tree to keep it balanced after you insert or delete nodes. See [AVL tree](../AVL Tree) and [red-black tree](../Red-Black Tree) for examples. |
| 720 | +The other solution is to use a *self-balancing* binary tree. This type of data structure adjusts the tree to keep it balanced after you insert or delete nodes. See [AVL tree](../AVL%20Tree) and [red-black tree](../Red-Black%20Tree) for examples. |
721 | 721 |
|
722 | 722 | ## See also
|
723 | 723 |
|
|
0 commit comments