CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2003
    Posts
    155

    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

  2. #2
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    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
  •  





Click Here to Expand Forum to Full Width

Featured