• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Handle duplicates in Binary Search Tree

April 17, 2017 by Dhaval Dave

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 and duplicates in binary search tree are not allowed.

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 with count. For example consider insertion of keys 3, 6, 7, 8, 8, 8, 10, 12, 12 in an empty Binary Search Tree

         8(3)
       /     \      
    6(1)      10(1)
  /    \        \
3(1)    7(1)     12(2)

This count store requires changing the structure of Binary Search tree to

struct node
{
    int key;
    int count;
    struct node *left, *right;
};

And We may need to change the code of Functions of “Insert” “Find” and “Delete”.

Lets see for Insert

struct node* insert(struct node* node, int key)
{
    if (node == NULL) return newNode(key);
 
    if (key == node->key)
    {
       (node->count)++;
        return node;
    }
 
    if (key < node->key)
        node->left  = insert(node->left, key);
    else
        node->right = insert(node->right, key);
 
    return node;
}

Now Based upon Above code You can try to write code for “Search a value in BST with Duplicate” and Delete a node in BST with duplicate”.
PS : In delete a node in BST, Handling of adjusment of tree branches should happen.

Solution 2 :

If change in Structure of Binary Search Tree is not allowed, We can think of Extra memory like Hashmap to keep the count of nodes of BST to support duplicates in Binary Search Tree. ie Hashmap below is with above tree without count.

 3 - 1
 6 - 1
 7 - 1
 8 - 3
10 - 1
12 - 2

Solution 3:

We can think of keeping same keys on left side (we could also choose right side) to support duplicates in Binary Search Tree. For example consider insertion of keys 3, 6, 7, 8, 8, 8, 10, 12, 12  in an empty Binary Search Tree

              8
          /       \
        8         12
      /           /     \
    8           10      12
  /
 6   
/  \
3  7

 

 

Similar Articles

Filed Under: Amazon Interview Question, Flipkart Interview Questions, Hacker Earth Questions, Interview Questions, Microsoft Interview Questions, problem Tagged With: Binary Search Tree, Binary Tree

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Introduction To Number Theory ( Part 1 )

Maximum size of square sub matrix with all 1’s in a binary matrix

Find if a binary tree is height balanced ?

Facebook Interview Question : Interleave List

CodeChef’ RRCOPY

Level order traversal in Spiral form

Rectangular chocolate bar Create at least one piece which consists of exactly nTiles tiles

Minimum insertions to form a palindrome

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

Python List

VMWare SDEII Interview

Circular Linked List

Skiing on Mountains Matrix

Search element in a matrix with all rows and columns in sorted order

Reversal of LinkedList

Urban Ladder Written Test.

C Program for TAIL command of UNIX

Check if an array has duplicate numbers in O(n) time and O(1) space

Find an index i such that Arr [i] = i in array of n distinct integers sorted in ascending order.

‘N’ Story Building, with 1,2,3 steps how many ways can a person reach top of building.

Given array of 0’s and 1’s. All 0’s are coming first followed by 1’s. find the position of first 1

Get Minimum element in O(1) from input numbers or Stack

Get K Max and Delete K Max in stream of incoming integers

Print all nodes that are at distance k from a leaf node

Given a sorted array and a number x, find the pair in array whose sum is closest to x

In Given LinkedList Divide LL in N Sub parts and delete first K nodes of each part

Implement a generic binary search algorithm for Integer Double String etc

Possible sizes of bus to carry n groups of friends

Daughter’s Age VeryGood Puzzle

Coin Collection Dynamic Programming

Copyright © 2025 · Genesis Framework · WordPress · Log in