0

If two binary search tree is given how do i check if one is sub tree of other. Is the same algorithm that checks if one tree is the sub tree of other true in this case ?

Algo from here Find whether a tree is a subtree of other

Traverse T1. If the current node is equal to the root node of T2, traverse both of the trees (T2 and the current subtree of T1) at the same time. Compare the current node. If they are always equal, T2 is a subtree of T1

Kindly suggest

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
sreeprasad
  • 3,242
  • 3
  • 27
  • 33
  • Probably you first find the very first similar node/item in the larger tree that is in the shorter tree and then continue onwards until either all nodes are exhausted from the shorter tree in which case shorter one is a sub-tree or there is dissimilarity found prior to the end of the shorter tree. In which case the shorter tree is not a sub. –  Apr 24 '11 at 14:32
  • Wajih - I don't understand your comment, you have not defined "similar". Please see my answer. – Robin Green Apr 24 '11 at 14:33

1 Answers1

0

Yes, a binary tree is a particular kind of tree, so because this algorithm works for any tree, it will certainly work for a binary tree.

Robin Green
  • 32,079
  • 16
  • 104
  • 187
  • I meant is there a more quicker solution or some other way to determine this because the trees are binary search trees and not any tree. – sreeprasad Apr 24 '11 at 16:35