|
| 1 | +--- |
| 2 | +title: Binary Search Tree |
| 3 | +tags: |
| 4 | + - Tree |
| 5 | + - Binary Search |
| 6 | + - BST |
| 7 | +--- |
| 8 | + |
| 9 | +A Binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child. |
| 10 | + |
| 11 | +For a binary tree to be a binary search tree, the values of all the nodes in the left sub-tree of the root node should be smaller than the root node's value. Also the values of all the nodes in the right sub-tree of the root node should be larger than the root node's value. |
| 12 | + |
| 13 | +<figure markdown="span"> |
| 14 | + |
| 15 | +<figcaption>a simple binary search tree</figcaption> |
| 16 | +</figure> |
| 17 | + |
| 18 | +## Insertion Algorithm |
| 19 | + |
| 20 | +1. Compare values of the root node and the element to be inserted. |
| 21 | +2. If the value of the root node is larger, and if a left child exists, then repeat step 1 with root = current root's left child. Else, insert element as left child of current root. |
| 22 | +3. If the value of the root node is lesser, and if a right child exists, then repeat step 1 with root = current root's right child. Else, insert element as right child of current root. |
| 23 | + |
| 24 | +## Deletion Algorithm |
| 25 | +- Deleting a node with no children: simply remove the node from the tree. |
| 26 | +- Deleting a node with one child: remove the node and replace it with its child. |
| 27 | +- Node to be deleted has two children: Find inorder successor of the node. Copy contents of the inorder successor to the node and delete the inorder successor. |
| 28 | +- Note that: inorder successor can be obtained by finding the minimum value in right child of the node. |
| 29 | + |
| 30 | +## Sample Code |
| 31 | + |
| 32 | +```c |
| 33 | +// C program to demonstrate delete operation in binary search tree |
| 34 | +#include<stdio.h> |
| 35 | +#include<stdlib.h> |
| 36 | + |
| 37 | +struct node |
| 38 | +{ |
| 39 | + int key; |
| 40 | + struct node *left, *right; |
| 41 | +}; |
| 42 | + |
| 43 | +// A utility function to create a new BST node |
| 44 | +struct node *newNode(int item) |
| 45 | +{ |
| 46 | + struct node *temp = (struct node *)malloc(sizeof(struct node)); |
| 47 | + temp->key = item; |
| 48 | + temp->left = temp->right = NULL; |
| 49 | + return temp; |
| 50 | +} |
| 51 | + |
| 52 | +// A utility function to do inorder traversal of BST |
| 53 | +void inorder(struct node *root) |
| 54 | +{ |
| 55 | + if (root != NULL) |
| 56 | + { |
| 57 | + inorder(root->left); |
| 58 | + printf("%d ", root->key); |
| 59 | + inorder(root->right); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/* A utility function to insert a new node with given key in BST */ |
| 64 | +struct node* insert(struct node* node, int key) |
| 65 | +{ |
| 66 | + /* If the tree is empty, return a new node */ |
| 67 | + if (node == NULL) return newNode(key); |
| 68 | + |
| 69 | + /* Otherwise, recur down the tree */ |
| 70 | + if (key < node->key) |
| 71 | + node->left = insert(node->left, key); |
| 72 | + else |
| 73 | + node->right = insert(node->right, key); |
| 74 | + |
| 75 | + /* return the (unchanged) node pointer */ |
| 76 | + return node; |
| 77 | +} |
| 78 | + |
| 79 | +/* Given a non-empty binary search tree, return the node with minimum |
| 80 | + key value found in that tree. Note that the entire tree does not |
| 81 | + need to be searched. */ |
| 82 | +struct node * minValueNode(struct node* node) |
| 83 | +{ |
| 84 | + struct node* current = node; |
| 85 | + |
| 86 | + /* loop down to find the leftmost leaf */ |
| 87 | + while (current->left != NULL) |
| 88 | + current = current->left; |
| 89 | + |
| 90 | + return current; |
| 91 | +} |
| 92 | + |
| 93 | +/* Given a binary search tree and a key, this function deletes the key |
| 94 | + and returns the new root */ |
| 95 | +struct node* deleteNode(struct node* root, int key) |
| 96 | +{ |
| 97 | + // base case |
| 98 | + if (root == NULL) return root; |
| 99 | + |
| 100 | + // If the key to be deleted is smaller than the root's key, |
| 101 | + // then it lies in left subtree |
| 102 | + if (key < root->key) |
| 103 | + root->left = deleteNode(root->left, key); |
| 104 | + |
| 105 | + // If the key to be deleted is greater than the root's key, |
| 106 | + // then it lies in right subtree |
| 107 | + else if (key > root->key) |
| 108 | + root->right = deleteNode(root->right, key); |
| 109 | + |
| 110 | + // if key is same as root's key, then This is the node |
| 111 | + // to be deleted |
| 112 | + else |
| 113 | + { |
| 114 | + // node with only one child or no child |
| 115 | + if (root->left == NULL) |
| 116 | + { |
| 117 | + struct node *temp = root->right; |
| 118 | + free(root); |
| 119 | + return temp; |
| 120 | + } |
| 121 | + else if (root->right == NULL) |
| 122 | + { |
| 123 | + struct node *temp = root->left; |
| 124 | + free(root); |
| 125 | + return temp; |
| 126 | + } |
| 127 | + |
| 128 | + // node with two children: Get the inorder successor (smallest |
| 129 | + // in the right subtree) |
| 130 | + struct node* temp = minValueNode(root->right); |
| 131 | + |
| 132 | + // Copy the inorder successor's content to this node |
| 133 | + root->key = temp->key; |
| 134 | + |
| 135 | + // Delete the inorder successor |
| 136 | + root->right = deleteNode(root->right, temp->key); |
| 137 | + } |
| 138 | + return root; |
| 139 | +} |
| 140 | +``` |
| 141 | +
|
| 142 | +## Time Complexity |
| 143 | +
|
| 144 | +The worst case time complexity of search, insert, and deletion operations is $\mathcal{O}(h)$ where h is the height of Binary Search Tree. In the worst case, we may have to travel from root to the deepest leaf node. The height of a skewed tree may become $N$ and the time complexity of search and insert operation may become $\mathcal{O}(N)$. So the time complexity of establishing $N$ node unbalanced tree may become $\mathcal{O}(N^2)$ (for example the nodes are being inserted in a sorted way). But, with random input the expected time complexity is $\mathcal{O}(NlogN)$. |
| 145 | +
|
| 146 | +However, you can implement other data structures to establish Self-balancing binary search tree (which will be taught later), popular data structures that implementing this type of tree include: |
| 147 | +
|
| 148 | +- 2-3 tree |
| 149 | +- AA tree |
| 150 | +- AVL tree |
| 151 | +- B-tree |
| 152 | +- Red-black tree |
| 153 | +- Scapegoat tree |
| 154 | +- Splay tree |
| 155 | +- Treap |
| 156 | +- Weight-balanced tree |
0 commit comments