Please refer below post before reading about AVL tree handling of duplicates.
This is to augment AVL 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
Below is implementation of normal AVL Tree with count with every key. This code basically is taken from code for insert and delete in AVL tree. The changes made for handling duplicates are highlighted, rest of the code is same.
The important thing to note is changes are very similar to simple Binary Search Tree changes.
Pre order traversal of the constructed AVL tree is 9(2) 5(2) 7(1) 10(1) 17(1) Pre order traversal after deletion of 9 9(1) 5(2) 7(1) 10(1) 17(1)
Thanks to Rounaq Jhunjhunu Wala for sharing initial code. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- BST to a Tree with sum of all smaller keys
- Print BST keys in the given range
- Find All Duplicate Subtrees
- Duplicate subtree in Binary Tree | SET 2
- Check if a Binary Tree (not BST) has duplicate values
- Check if a Binary Tree contains duplicate subtrees of size 2 or more
- Count the nodes of a tree whose weighted string does not contain any duplicate characters
- Mimimum number of leaves required to be removed from a Tree to satisfy the given condition
- Farthest distance of a Node from each Node of a Tree
- Sum of Nodes and respective Neighbors on the path from root to a vertex V
- Queries to find the Minimum Weight from a Subtree of atmost D-distant Nodes from Node X
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Find all root to leaf path sum of a Binary Tree
- Number of spanning trees of a weighted complete Graph