|
-
March 17th, 2004, 07:20 PM
#1
2-3 tree implementation from sorted array in O(n)-time
Hello,
I've a quick question - anyone have an idea on what an efficient manner of building a 2-3 tree from a sorted array A[1..n] in O(n) time could be?
Thanks!
Alexandre
-
March 18th, 2004, 11:13 AM
#2
I guess you could just build a perfectly balanced binary tree and then pretend that it's a 2-3 tree (it would be an all 2 tree).
The algorithm could easily be done in a divide and conquer style with recursion. Split the array in 2 and call recursively until you arrive at an interval which contains 1 or 0 entries. if it's 1, generate a new leaf, otherwise pass back null. Then at the recursion level above, combine the outputs of the two previous ones into an internal node. Running time would be O(N) and recursion depth would be log(N). I guess you can easily convert this to an iterative solution as well.
Yves
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|