Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is inorder and preorder traversal useful for creating an algorithm to decide if T2 is a subtree of T1

I'm looking at an interview book and the question is:

You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of T1.

The authors mentions this as a possible solution:

Note that the problem here specifies that T1 has millions of nodes—this means that we should be careful of how much space we use. Let’s say, for example, T1 has 10 million nodes—this means that the data alone is about 40 mb. We could create a string representing the inorder and preorder traversals. If T2’s preorder traversal is a substring of T1’s preorder traversal, and T2’s inorder traversal is a substring of T1’s inorder traversal, then T2 is a substring of T1.

I'm not quite sure the logic behind as to why if these are true:

  • T2-preorder-traversal-string is a substring of T1-preorder-traversal-string
  • T2-inorder-traversal-string is a substring of T1-inorder-traversal-string

That T2 must be a substring (although I assume the author means subtree) of T1. Can I get an explanation to this logic?

EDIT: User BartoszMarcinkowski brings up a good point. Assume both trees have no duplicate nodes.

like image 705
But I'm Not A Wrapper Class Avatar asked Jan 20 '14 16:01

But I'm Not A Wrapper Class


People also ask

Why do we use inorder traversal?

In-order traversal is very commonly used in binary search trees because it returns values from the underlying set in order, according to the comparator that set up the binary search tree (hence the name). Post-order traversal while deleting or freeing nodes and values can delete or free an entire binary tree.

What is the use of inorder preorder and postOrder?

For Inorder, you traverse from the left subtree to the root then to the right subtree. For Preorder, you traverse from the root to the left subtree then to the right subtree. For Post order, you traverse from the left subtree to the right subtree then to the root.

Why do we need preorder traversal?

Preorder traversal is used to create a copy of the tree. Preorder traversal is also used to get prefix expression on an expression tree.

What is the Speciality about the inorder traversal of a binary search tree?

What is the speciality about the inorder traversal of a binary search tree? Explanation: As a binary search tree consists of elements lesser than the node to the left and the ones greater than the node to the right, an inorder traversal will give the elements in an increasing order.


2 Answers

I think it is not true. Consider:

T2:

  2
 / \
1   3

inorder 123 preorder 213

and

T1:

      0
     / \
    3   3
   / \ 
  1   1
 / \ 
0   2


inorder 0123103 preorder 0310213

123 is substring of 0123103, 213 is substring of 0310213, but T2 is not subtree of T1.

like image 161
Bartosz Marcinkowski Avatar answered Oct 05 '22 23:10

Bartosz Marcinkowski


Here is a counter-example to the method.

Consider the tree T1:

  B
 / \
A   D
   / \
  C   E
       \
        F

And the sub-tree T2:

  D
 / \
C   E

The relevant traversals are:

  • T1 pre-order: BADCEF
  • T2 pre-order: DCE
  • T1 in-order: ABCDEF
  • T2 in-order: CDE

While DCE is in BADCEF and CDE is in ABCDEF, T2 is not actually a sub-tree of T1. The author's definition of sub-tree must have been different or it was just a mistake.

Related question: Determine if a binary tree is subtree of another binary tree using pre-order and in-order strings

like image 45
Daniel Imms Avatar answered Oct 06 '22 00:10

Daniel Imms