In a Binary Search Tree (BST), all keys in left subtree of a key must be smaller and all keys in right subtree must be greater. So a Binary Search Tree by definition has distinct keys.
How to allow duplicates where every insertion inserts one more key with a value and every deletion deletes one occurrence?
A Simple Solution is to allow same keys on right side (we could also choose left side). For example consider insertion of keys 12, 10, 20, 9, 11, 10, 12, 12 in an empty Binary Search Tree
12 / \ 10 20 / \ / 9 11 12 / \ 10 12
A Better Solution is to augment every tree node to store count together with regular fields like key, left and right pointers.
Insertion of keys 12, 10, 20, 9, 11, 10, 12, 12 in an empty Binary Search Tree would create following.
12(3) / \ 10(2) 20(1) / \ 9(1) 11(1) Count of a key is shown in bracket
This approach has following advantages over above simple approach.
1) Height of tree is small irrespective of number of duplicates. Note that most of the BST operations (search, insert and delete) have time complexity as O(h) where h is height of BST. So if we are able to keep the height small, we get advantage of less number of key comparisons.
2) Search, Insert and Delete become easier to do. We can use same insert, search and delete algorithms with small modifications (see below code).
3) This approach is suited for self-balancing BSTs (AVL Tree, Red-Black Tree, etc) also. These trees involve rotations, and a rotation may violate BST property of simple solution as a same key can be in either left side or right side after rotation.
Below is C implementation of normal Binary Search Tree with count with every key. This code basically is taken from code for insert and delete in BST. The changes made for handling duplicates are highlighted, rest of the code is same.
Inorder traversal of the given tree 9(1) 10(2) 11(1) 12(3) 20(1) Delete 20 Inorder traversal of the modified tree 9(1) 10(2) 11(1) 12(3) Delete 12 Inorder traversal of the modified tree 9(1) 10(2) 11(1) 12(2) Delete 9 Inorder traversal of the modified tree 10(2) 11(1) 12(2)
We will soon be discussing AVL and Red Black Trees with duplicates allowed.
This article is contributed by Chirag. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Binary Search Tree | Set 1 (Search and Insertion)
- Binary Tree to Binary Search Tree Conversion
- Binary Tree to Binary Search Tree Conversion using STL set
- Binary Search Tree | Set 2 (Delete)
- Floor in Binary Search Tree (BST)
- Optimal Binary Search Tree | DP-24
- Iterative searching in Binary Search Tree
- Inorder Successor in Binary Search Tree
- Print all odd nodes of Binary Search Tree
- Construct a Binary Search Tree from given postorder
- Treap (A Randomized Binary Search Tree)
- Print all even nodes of Binary Search Tree
- Threaded Binary Search Tree | Deletion
- How to implement decrease key or change key in Binary Search Tree?
- Lowest Common Ancestor in a Binary Search Tree.