BST with duplicates

ε祈祈猫儿з 提交于 2019-12-03 13:46:55
Sazzadur Rahaman

Rule to insert in a binary Search tree without duplicate is:

  1. Go left if element is less than root
  2. Go right if the element is greater than root.

And to allow duplicate entries you have to modify the rule like bellow:

  1. Go left if the element is less or equal root
  2. Go right if the element is greater than root.

or

  1. Go left if the element is less than root
  2. Go right if the element is greater or equal root.

or

  1. Go left if the element is less than root
  2. Go right if the element is greater than root.
  3. Increase the count if the element is equal to the root.

So your BST for word "RABSAB", with duplicates can be like:

     R
    / \
   A   S
  / \
 A   B
    /
   B

Or,

     R
    / \
   A   S
    \
     A
      \
       B
        \
         B

or

    R(1)
   /  \
  /    \
 A(2)  S(1)
  \
   \
   B(2)

In First two cases, both insertion and search becomes bit complex! You will find it here with great deal of explanation!

And the third case is somewhat easier to maintain.

All of them are used successfully to allow duplicates, now the choice is yours!

One option is to modify the tree so that one branch will include the duplicates, for example have the left branches hold nodes that are less than or equal to the parent, alternatively have the right branches hold nodes that are greater than or equal to the parent

Another option is to store all duplicates in a node, so instead of

class Node {
    Node left, right;
    Object data;
}

you would instead have

class Node {
    Node left, right;
    List data;
}

or

class Node {
    Node left, right;
    Object data;
    int count;
}

In a normal BST insertion and search both occur based on less than(>) and greater than (<) rule.

You could instead try insertion on less than equal to (>=) or greater than equal to (<=) and try using the same rule for searching.

Alternatively you could include an array in every node to accommodate duplicate elements.

For your input RABPAB, You can create a BST by using a LIST to store all the equal valued keys. All the equal valued keys are placed in the same level using a data structure capable of storing it.

The BST will look something like this,

     R
    / \
A--A   P
    \
  B--B

The Java code for your BST storing integer values could be like this,

class Node 
{
    Node left, right;
    int data[maxvalue];
}

Here maxvalue is the maximum possible equal valued keys.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!