How can I draw a Binary Search Tree using below data.
Inorder :- C, B, D, A, E, G, H, F
Preorder :- A, B, C, D, E, F, G, H
Is there any generic algorithm? Can anyone help me to solve this example?
From the preorder, we can infer that A is a root of the tree.
Then from the inorder, we can infer that C B D form the left sub-tree, and E G H F form the right sub-tree.
Consider the left sub-tree consists of C B D:
From the preorder, we can infer that B is the root. And then from the inorder, we know that C and D are the left child and right child respectively.
Consider the right sub-tree consists of E G H F:
From the preorder, we can infer that E is the root; and from the inorder, G H F belong to the right sub-tree.
Consider the sub-tree G H F:
The preorder tells us that F is the root, and from the inorder, we can infer that G H belong to the left sub-tree of F
Consider G H:
From preorder, G is the root and H is the right child
Therefore the tree is given below:
A
/ \
B E
/ \ \
C D F
/
G
\
H
Related
I'm looking for references for an algorithm that conducts a breadth-first tree search in a balanced manner that is resilient in a situation where
we expect most nodes to have few children, but
a few nodes may have a many (possibly infinitely many) children.
Consider this simple tree (modified from this post):
A
/ \
B C
/ / \
D E F
| /|\ \
G HIJ... K
Depth-first visits nodes in this order:
A B D G C E H I J ... F K
Breadth-first visits nodes in this order:
A B C D E F G H I J ... K
The balanced breadth-first algorithm that I have in mind would visit nodes in this order:
A B C D E G F H K I J ...
Note how
we visit G before F, and
we visit K after H but before I.
G is deeper than F, but it is an only child of B whereas F is a second child of C and must share its search priority with E. Similarly between K and the many children H, I, J, ... of E.
I call this "balanced" because a node with lots of children cannot choke the algorithm. Concretely, if E has 𝜔 (infinitely) many nodes then a pure breadth-first strategy would never reach K, whereas the "balanced" algorithm would still reach K after H but before the other children of E.
(The reader who does not like 𝜔 can attain a similar effect with a large but still finite number such as "the greatest number of steps any practical search algorithm will ever make, plus 1".)
I can only imagine that this style of search or something like it must have been the subject of much research and practical application. I would be grateful to be pointed in the right direction. Thank you.
Transform your tree to a different kind of representation. In this new representation, each node has at most two links: one to its leftmost child, and one to its right sibling.
A
/ \
B C
/ / \
D E F
| /|\ \
G HIJ... K
⇓
A
/
B --> C
/ /
D E -> F
| / \
G / K
/
H -> I -> J -> ...
Then treat this representation as a normal binary tree, and traverse it breadth-first. It may have an infinite height, but like with any binary tree, the width at any particular level is finite.
depth-first-search, breadth-first-search, A*-search (and others) only differ in how you handle the list of "nodes still to visit".
(I assume you always process the node at the start of the list next)
depth-first-search: append new nodes at the front of the list
breadth-first-search: add new nodes to the end of the list
A*-search: insert new nodes in the list according to costs+heuristic
So you need to formalize how to insert new nodes to the list to fulfill your requirements.
Let G be an undirected graph. Consider a depth-first traversal of G, and let T be the resulting depth-first search tree. Let u be a vertex in G and let v be the first new (unvisited) vertex visited after visiting u in the traversal. Which of the following statements is always true?
(A) {u,v} must be an edge in G, and u is a descendant of v in T
(B) {u,v} must be an edge in G, and v is a descendant of u in T
(C) If {u,v} is not an edge in G then u is a leaf in T
(D) If {u,v} is not an edge in G then u and v must have the same parent in T.
=========================================================================
Correct answer is (C)
But I'm stuck at (B), I'm not getting any counter example for (B)
Consider the following graph (which is in fact a tree).
G := (V, E) where
V := {a, u, v} and E := {{a,u}, {a,v}}.
A depth-first traversal in G can visit first a, then u and finally v. At the point where u is visited, v is the next unvisited node. However, {u, v} is not an edge in E, hence statement (B) is false.
I ran into a Mid-Exam Question, that took 4 days ago, I couldent underestand it!
Suppose we have the answer given when we have an inorder traversal of a tree then how come we will find out the solution in case of a preorder traversal. I have the following example with me : When inorder traversing a tree resulted E A C K F H D B G;
what would the preorder traversal return?
a. FAEKCDBHG
b. FAEKCDHGB
c. EAFKHDCBG
d. FEAKDCHBG
Who can help me in a learning manner?
EDIT:
I know the answer is : FAEKCDHGB. but how this calculated?
So the inorder is:
E A C K F H D B G
And the preorder must be from:
a. FAEKCDBHG
b. FAEKCDHGB
c. EAFKHDCBG
d. FEAKDCHBG
You should proceed by drawing the tree for each of these options while also making it fit with the inorder traversal and see for which one that is possible.
To do that, for each character in the preorder traversal, split the inorder traversal in two around that character.
a.
We know F must be the root. Split the inorder traversal around F:
| F |
E A C K H D B G
The next character in the preorder traversal is A. Split the subtree containing A around A:
| F |
| A | H D B G
E C K
The next is E. Nothing to split. The next is K:
| F |
| A | H D B G
E C
K
The next is C. Nothing to split.
The next is D:
| F |
| A | | D |
E C H B G
K
The next is B:
| F |
| A | | D |
E C H B
K G
And we're done, there will be no more splits possible. Now run the preorder traversal on this tree, you'll get:
F A E C K D H B G
Which is not what we started with, so we reached a contradiction. So it cannot be a. Do the same for the others.
The answer is undefined.
Have a look at those two trees:
E
\
A
\
C
\
K
\
F
\
H
\
D
\
B
\
G
And:
A
/ \
E C
\
K
\
F
\
H
\
D
\
B
\
G
Those two trees have the same in-order traversal, but the first has the pre-order traversal of EACKFHDBG, while the second has the in-order traversal of AECKFHDBG
So, given an in-order traversal, there is much more than one possible binary tree that fit that traversal. This is because of the fact that there are n! possible permutations (and thus in-order traversal), while there are much more binary trees. This guarantees (pigeonhole principle) that there is (at least one) in-order traversal that fits multiple trees.
One of the b-trees possible is :
So, the pre order traversal returns:
H K A E C F B D G
Another one is:
For which preorder traversal gives:
H A E K C F B D G
Yet another one is:
For which preorder traversal gives:
K A E C H F B D G
,which is none of your options. Comments by anyone are welcome as I cannot come up with any other binary tree for the given inorder traversal given..
I too had this question in my exams but came to know that b is the answer from google search of the question. Lolz.
In my exam, I faced the following question:
(a) was very easy, but, for (b), I am totally confused. What does is mean to make no assumptions? If I do not assume it is a binary tree how can I solve it (because pre-order and post-order are related to BST)? Without assuming it is a BST, I don't know how to start?
Could you please guide me?
b) Well, these orders seen consistent with a binary tree ordering:
V W B C Y Z N A M L P
C B Z N Y W M P L A V
First, note the root is V because it's both first in the preorder and last in the postorder:
V W B C Y Z N A M L P
C B Z N Y W M P L A V
Then look at W and A, they are, respectively, they are first left child and the last right child of the root. A in the preorder marks the place where the traversal transitions from the left subtree of the root to the right subtree of the root. W in the postorder marks the same place. Note that that when you split the traversals, A and W are adjacent positions:
V W B C Y Z N A M L P
C B Z N Y W M P L A V
Now you are left to solve the same problem for the sequences:
W B C Y Z N
C B Z N Y W
and
A M L P
M P L A
For example, the next step for the first sequence would be:
W B C Y Z N
C B Z N Y W
Hope this helps.
I need your help
Is it possible to have a binary search tree which pre-order and in-order traversals generate the same result ?
I've tried to take an example tree consisting of 7 nodes , and i labeled the nodes from a to g ..
this is my tree :
a
b c
d e f g
where a is the root , b and c are its children , d and e are b's childrens , and f and g are c's children
The pre-order traversal gives this result : a b d e c f g
The in-order traversal gives this result : d b e a f c g
So in order to get the same result i need that a = d = e and f = c .. which is not possible since it is a BST ..
Could you just check if it was correct ?
And if my idea about traversals is correct ?
Regards ,
If you have a tree with only right children(i.e. a list) you will have its preorder being equal to its inorder traversal.
This sounds like homework, so I won't go into too much detail. But yes, it is possible to have a binary tree with identical pre- and in-order traversals. Consider how you might make one with two nodes. Then consider how you might make one with three.