2

For example:

two trees :

       8                   9
   5        7          4       20
                    30

become one tree?

moinudin
  • 134,091
  • 45
  • 190
  • 216
wam090
  • 2,823
  • 8
  • 33
  • 36

3 Answers3

5

Without more details/constraints, the simplest solution is to take a leaf node of either tree, remove it, and use it as the root to the newly created three.

In your example:

             30
    8                   9
5        7          4       20

This works because your trees don’t appear to follow any particular order, don’t appear to be balanced, nor have any other constraints.

Since any leaf node will do, this is an O(n) operation in the worst case (traverse one of the trees in any order until we encounter the first leaf, remove it, add child links to both trees’ roots).

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 1
    I don't see how you get the resulting tree using that algorithm. And it's O(N) in the worst case of a degenerate tree (linked list). – moinudin Jan 08 '11 at 18:01
  • @marcog: you are right about the runtime. I was assuming the uppermost node would be found. Stupid of course. Still, the tree can be got using the algorithm by (arbitrarily) descending down the leftmost branches in the second tree. – Konrad Rudolph Jan 08 '11 at 18:09
  • Ah, I see what you're doing now. Nice solution. +1 – moinudin Jan 08 '11 at 18:11
4

The easiest way to merge two binary trees is to take iterate down the left child of one tree until reaching a node without a left child. Then add the other tree's root as the left child. The resulting tree for your example will be:

            8
        5        7
    9
  4  20
30

However, notice how the resulting tree in this example is very unbalanced. This will result in inefficient operations on the resulting tree, should this be the intention. A better solution is to evenly distribute the nodes of from second tree into the first tree. One way of achieving this is to recursively add the root and left subtree of the second tree to the left subtree of the first tree, and the right subtree to the right subtree. For a slightly more even distribution, randomly select which side to allocate the root to at each step.

Note that the binary trees here are not binary search trees. Working with BST's is a slightly more interesting case as you have to ensure the resulting tree is also a valid BST. For those interested, here's a solution for that problem: Search for the root value of tree 2 in tree 1. If we reach a dead-end without having found this value, we can just insert tree 2 into the location where the value would be were it in the tree. If we do find the value, then we can replace that node with tree 2. Then take the sub-tree rooted at the node we displaced and insert it into tree 2 using the same algorithm.

moinudin
  • 134,091
  • 45
  • 190
  • 216
0

Basic answer would be just to add all the elements in the smaller tree into the bigger tree.

Another possiblity would be to look into B-Trees (self balancing trees)

Kurru
  • 14,180
  • 18
  • 64
  • 84
  • You could probably do better than this if you have more implementation details. But if this isn't your core functionality you should avoid too much optimization. This is better because will let you change implementation of the tree in the future. – Mihai Toader Jan 08 '11 at 17:35
  • 2
    B-Trees are very different structures, and more importantly they aren't binary trees. – moinudin Jan 08 '11 at 17:37
  • Yes, but if the asker wasn't aware of them, I thought it would be good to mention it – Kurru Jan 08 '11 at 19:04