7

I have two binary trees and I want to merge them. My first question is that whether we can merge two binary trees and if yes how efficiently I can perform the merge operations and what are the various ways i can perform the merging operations. ..?

templatetypedef
  • 328,018
  • 92
  • 813
  • 992
saurabh ranu
  • 1,231
  • 6
  • 18
  • 24
  • 6
    Merging binary trees is trivial, just link the root of one as a child of one of the leaf nodes of the other. Did you have some other structure that you want to preserve, like being ordered or balanced? – JGWeissman Aug 22 '11 at 19:26
  • lets start with simple unordered unbalanced tree . You said it's trivial so can u just show me how it's done..? – saurabh ranu Aug 22 '11 at 19:29

5 Answers5

22

1) Flatten both the trees in to sorted lists.
2) Merge the lists from what you got in 1)
3) Construct the tree out of what you got in 2)

hari
  • 8,775
  • 24
  • 67
  • 104
  • 2
    You can flatten the trees into lists in O(n) time using a standard in-order walk. Merging two sorted lists can be done in O(n) time as well. Once you've merged the lists, you can construct the BST in O(n) time by recursively constructing trees out of the left and right halves, then gluing them together. The overall complexity is therefore O(n). – templatetypedef Aug 22 '11 at 19:32
  • @templatetypedef: Thanks for the answer. Yes, complexity is O(n). – hari Aug 22 '11 at 19:34
  • @saurabh ranu: I do not think so. – hari Aug 22 '11 at 19:50
  • log(n) time complexity - not possible – Rami Yampolsky Jun 02 '14 at 09:54
5

This algorithm might help you.

Dan W
  • 5,447
  • 4
  • 30
  • 43
  • That has nothing to do with trees. – SLaks Aug 22 '11 at 19:30
  • 1
    @SLaks Yes it does. Since we know they are BST, we can arrange the trees into a sorted list. Once we have the 2 sorted lists, this algorithm applies. – Dan W Aug 22 '11 at 19:34
2

Not considering efficiency this answer may work Algorithm of combining two binary trees? . If sorted or balanced, discussion on efficiency in How to merge two BST's efficiently? and Concatenating/Merging/Joining two AVL trees

Community
  • 1
  • 1
David Andersson
  • 689
  • 4
  • 8
0

A tree is also a graph, so output the edge vertex-pairs (u,v) for each tree, and then union these edge sets, and output the resulting graph.

The issue arises with how to map vertices in the one tree to vertices in the other (e.g. we have edge pair (5,9) in tree 1 and edge pair (5,6) in tree 2, do these 5s correspond to the same vertex ? ).

Coming up with a numbering of vertices (perhaps that assigns numbers to each vertex in an incomplete binary tree, as if it were a complete binary tree, so in other words assigns the vertices in any partial binary tree to slots of a hypothetical complete binary tree of which that tree is a subtree), that somehow provides an desirable equivalence is something that works.

user2530580
  • 119
  • 1
  • 5
0

Create a new node and point one tail at the head of one of the trees and point the other tail at the head of the other tree. Perhaps you need to clarify your question to be more specific. What relationships are you trying to preserve?

jones
  • 341
  • 1
  • 4
  • 13