Help : Merging Binary Search Trees

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ptrSriram

    #1

    Help : Merging Binary Search Trees

    Can someone help me with an algorithm to merge two binary search trees.

    One method I thought of was to flatten both the trees into sorted
    lists(inorder traversal),merg e those two sorted lists, and build a
    binary search tree from the new list.
    But this seems to be expensive in terms of space. Can this be done more
    efficiently ?
    Please help me.

  • Walter Roberson

    #2
    Re: Help : Merging Binary Search Trees

    In article <1134194691.873 260.210430@g14g 2000cwa.googleg roups.com>,
    ptrSriram <sriram.ptr@gma il.com> wrote:[color=blue]
    >Can someone help me with an algorithm to merge two binary search trees.[/color]
    [color=blue]
    >One method I thought of was to flatten both the trees into sorted
    >lists(inorde r traversal),merg e those two sorted lists, and build a
    >binary search tree from the new list.
    >But this seems to be expensive in terms of space. Can this be done more
    >efficiently ?[/color]

    That depends. There are a number of different types of binary trees.
    Is there some particular property imposed on the trees you are
    using, such as that they must be "balanced" ? Do your tree entries
    have back-pointers or just downward pointers?
    --
    "No one has the right to destroy another person's belief by
    demanding empirical evidence." -- Ann Landers

    Comment

    • Chuck F.

      #3
      Re: Help : Merging Binary Search Trees

      Walter Roberson wrote:[color=blue]
      > ptrSriram <sriram.ptr@gma il.com> wrote:
      >[color=green]
      >> Can someone help me with an algorithm to merge two binary
      >> search trees.[/color]
      >[color=green]
      >> One method I thought of was to flatten both the trees into
      >> sorted lists(inorder traversal),merg e those two sorted lists,
      >> and build a binary search tree from the new list. But this
      >> seems to be expensive in terms of space. Can this be done
      >> more efficiently ?[/color]
      >
      > That depends. There are a number of different types of binary
      > trees. Is there some particular property imposed on the trees
      > you are using, such as that they must be "balanced" ? Do your
      > tree entries have back-pointers or just downward pointers?[/color]

      I think the question should be answered in terms of a minimal
      organization. i.e. the fundamental data item should be very
      similar to:

      struct itemlink {
      struct itemlink *left, *right;
      void *dataptr;
      };

      together with some routine that will return a -1, 0, +1 comparison
      result when passed two struct itemlink * pointers. Then the trees
      to be merged are represented by two struct itemlink * pointers to
      the roots of the two trees.

      --
      Read about the Sony stealthware that is a security leak, phones
      home, and is generally illegal in most parts of the world. Also
      the apparent connivance of the various security software firms.
      This is my sixth column for Wired.com: It’s a David and Goliath story of the tech blogs defeating a mega-corporation. On Oct. 31, Mark Russinovich broke the story in his blog: Sony BMG Music Entertainment distributed a copy-protection scheme with music CDs that secretly installed a rootkit on computers. This software tool is run without your knowledge or consent—if it’s loaded on your computer with a CD, a hacker can gain and maintain access to your system and you wouldn’t know it. The Sony code modifies Windows so you can’t tell it’s there, a process called “cloaking” in the hacker world. It acts as spyware, surreptitiously sending information about you to Sony. And it can’t be removed; trying to get rid of it ...

      Comment

      • Thad Smith

        #4
        Re: Help : Merging Binary Search Trees

        ptrSriram wrote:[color=blue]
        > Can someone help me with an algorithm to merge two binary search trees.
        >
        > One method I thought of was to flatten both the trees into sorted
        > lists(inorder traversal),merg e those two sorted lists, and build a
        > binary search tree from the new list.
        > But this seems to be expensive in terms of space.[/color]

        Why would it be expensive in space? If the data is in a tree, each item
        should be held in a node with two or more pointers. You should be able
        to link the items into list, merge them, and place them in another tree
        without any additional space.

        --
        Thad

        Comment

        Working...