Solved

b-tree with duplicate keys

Posted on 2006-11-02
1
461 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

821 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