Solved

b-tree with duplicate keys

Posted on 2006-11-02
1
429 Views
Last Modified: 2008-03-17
If multiple identical keys are put into a b-tree, how would it be possible to retrieve all of them?  If for example, you have 5 keys per node, and you insert 15 keys into an empty tree, the root node would be split.  Then during a retrieval, the retrieval function would find all duplicate keys in the root node, and then continue searching in the right node, but the keys in the left node would be lost.

So, how is this problem usually handled?  Do you need to make some kind of separate nodes that store all duplicate keys?
0
Comment
Question by:chsalvia
1 Comment
 
LVL 45

Accepted Solution

by:
Kdo earned 250 total points
ID: 17858611

When a b-tree allows multiple keys, certain rules must be defined.

For instance, when searching for a key you have the option of allowing duplicates in only one of the branches or both.

Searches are much faster when you only have to search one direction.  Though there is added overhead in maintaining the tree.

- Assume that a page holds 10 keys and we add thse 10 keys to a page:   A B C D E F G H I J
- Now we add a duplicate key B.  The most straight-forward approach is to split the page at the duplicate.  Either before or after the target key.  Let's pick before.

These two pages now contain the keys: A and  B B C D E F G H I J

Now we want to add another B to the page.  It's full and the target key already occupies the first position.  We can force another split after the B and continue adding B keys to the page.   Or we can move right to overflow blocks.

We can create a left node and put the duplicate B keys in it.


Note that balancing a tree can be a bit more of a challenge when duplicate keys are allowed and this technique is used.  The trade off is that the search doesn't have to play "what if".  If there are duplicate keys the search will be able to quickly locate them.


Good Luck,
Kent
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now