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.
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.
I'm having some trouble trying to represent and manipulate dependency graphs in this scenario:
a node has some dependencies that have to be solved
every path must not have dependencies loops (like in DAG graphs)
every dependency could be solved by more than one other node
I starts from the target node and recursively look for its dependencies, but have to mantain the above properties, in particular the third one.
Just a little example here:
I would like to have a graph like the following one
(A)
/ \
/ \
/ \
[(B),(C),(D)] (E)
/\ \
/ \ (H)
(F) (G)
which means:
F,G,C,H,E have no dependencies
D dependends on H
B depends on F and G
A depends on E and
B or
C or
D
So, if I write down all the possible topological-sorted paths to A I should have:
E -> F -> G -> B -> A
E -> C -> A
E -> H -> D -> A
How can I model a graph with these properties? Which kind of data structure is the more suitable to do that?
You should use a normal adjacency list, with an additional property, wherein a node knows its the other nodes that would also satisfy the same dependency. This means that B,C,D should all know that they belong to the same equivalence class. You can achieve this by inserting them all into a set.
Node:
List<Node> adjacencyList
Set<Node> equivalentDependencies
To use this data-structure in a topo-sort, whenever you remove a source, and remove all its outgoing edges, also remove the nodes in its equivalency class, their outgoing edges, and recursively remove the nodes that point to them.
From wikipedia:
L ← Empty list that will contain the sorted elements
S ← Set of all nodes with no incoming edges
while S is non-empty do
remove a node n from S
add n to tail of L
for each node o in the equivalency class of n <===removing equivalent dependencies
remove j from S
for each node k with an edge e from j to k do
remove edge e from the graph
if k has no other incoming edges then
insert k into S
for each node m with an edge e from n to m do
remove edge e from the graph
if m has no other incoming edges then
insert m into S
if graph has edges then
return error (graph has at least one cycle)
else
return L (a topologically sorted order)
This algorithm will give you one of the modified topologicaly-sorted paths.
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
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.
I've given the result of in-order traversal of a binary tree (not binary search tree) as:
E, D, B, A, G, F, H, C
Now I've to find out the result of the post-order traversal of the same tree for which the in-order traversal is given.
Can anyone suggest me any algorithm for this ?
P.S: Is there any way to sketch the tree itself from the in-order result ?
You can't do that. Your example might represent multiple trees, for example :
E D
\ / \
D E B
\ \
B A
\ \
A G ...
\ \
G F
\ \
F G
\ \
H C
\
C
You need at least two orders to reconstruct the tree, and you can only give an order when you have the tree at hand.