The traversal of a BST - algorithm

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.

Related

Algorithm for "balanced" breadth-first search

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.

AVL TREE traversal in order

Can someone help me, how I could traversal a balanced binary tree in order without recursion, stack, or morris traversal. I want to traverse it iteratively without modifying the tree.
Thank you so much.
In the case where there are no duplicate keys, this corresponds to the tree representing a set (or map.) In that case, a backtracking approach will be O(log n) (AVL tree property) per key. One could get a faster run time by storing the nodes, (such as in recursion,) but often this is unfeasible.
If current is not null, descend next with the current key as the target. Whenever the left is taken, first assign an ancestor (before descending.)
There are three cases: current was null -> next = root; current == next has a right child, next <- next.right; or next does not have a right child, next <- ancestor (if it does not exist, finished.)
In the first two cases, descend on the left until you hit a leaf.
I'll use Wikipedia AVL Tree article example, without the balance factors, (this will work on any binary tree, but one is not guaranteed performance.)
current
path
result
null
root J
C
C
ancestor D
D
D
ancestor F
F
F
right G
G
G
ancestor J
J
J
right P
N
N
ancestor L
L
L
ancestor P
P
P
right V
Q
Q
ancestor S
S
S
right U
U
U
ancestor V
V
V
right X
X
X
ancestor null
null
If the tree can have duplicate entries, this might be said to be a multiset. In this case, this will not work because this relies on the keys being unique.

Finding Preoder from Just Inorder Traversal?

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.

Binary Tree from inorder and postorder

I have just started studying Binary Tree. Is there an algorithm to find out the binary tree structure,given the Inorder and Postorder OR Inorder and Preorder? I have been trying to do it manually,but it never comes out correct.For eg.-These two are valid Inorder and Postorder traversal of a given tree:
Inorder: D B F E A G C L J H K
Postorder : D F E B G L J K H C A
Clearly A is the root as it is the last element in Postorder. Now looking in Inorder,the left subtree becomes: {D B F E} and right subtree becomes: {G C L J H K}. The root of right subtree would be the second last element in preorder i.e C. I can now further divide the right subtree(with C as root), giving {G} as right subtree and {L J H K} as left. Therefore I have this structure:
A
\
C
/
G
But,whatever algorithm I apply,next seems to work differently for different trees . Someone please explain.
If I understand what your asking, your trying to reverse engineer the underlying structure for a given binary tree search algorithm given the raw data in it's pre and post state. If this is the case you could be down a difficult road since although the basic algorithm is the same, there could be nuances to it depending on the developer that build the algorithm since in practice it is often the case the developers do not build a pure implementation of these algorithms.
If your just trying to get a better understanding of binary trees, this may explain it a little better: http://www.youtube.com/watch?v=ZkH3SSPwcwI
Let the inorder and preorder traversals be given in the arrays iorder and porder respectively.
The function to build the tree will be denoted by buildTree(i,j,k) where i,j refer to the range of the inorder array to be looked at and k is the position in the preorder array.
Initial call will be buildTree(0,n-1,0)
The algorithm has the following steps:
Traverse porder from start. The first node is the root, then we have the left subtree and then the right subtree. Create a node with this as the element.
Search the node in the iorder array. Suppose its found at x. Decrement k. k refers to the position in the porder array we are currently at. k has to be passed by reference.
Finally populate the left child and right child with the return value of the recursive calls
left child = buildTree(i,x-1,k)
right child = buildTree(x+1,j,k)
In the end return the node
PS: Got the code accepted with the above algorithm at
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=477

Is it possible to create a binary non-unique tree using the preorder and postorder sequences?

Is it possible to create a binary non-unique tree using the preorder and postorder sequences ?If so, how can this be done ? For example how could I make a non-unique tree for:
Preorder:
B C I J K H D E F G
Postorder:
I H K J C G F E D B
How many could there be ?
preorder psedo-code:
preorder (tree)
{
if tree isn't empty then
{
print key[tree]
preorder left[tree]
preorder right[tree]
}
}
and post order is:
postorder (tree)
{
if tree isn't empty then
{
preorder left[tree]
preorder right[tree]
print key[tree]
}
}
so from inorder order we can conclude:
"B" must be the root
"C" must be "B"'s child
"G" must be the the max value (the most far right node in the tree) or the min value in the left sub-tree (the most far left node in the left sub-tree) - in that case "G" must be a leaf and "F" must be "G"'s parent
and from postorder order we can conclude:
"I" must be a leaf and the min value (the most right node in the tree).
"H" must be "I"'s parent ("I" is "H" left child) in case I has no children, else "H" is the next far left child in the tree.
from here it's like a Sudoku:
and yes: by using preorder and postorder outputs you can build a tree in only one way.
Yes, it is possible to construct different binary trees that have the same pre- and postorder sequences. To generate such different trees, look for subtrees where either the left or the right child is empty and simply swap the children.
This minimal example
a a
/ vs. \
b b
shows two trees that both have the preordering a b and the postordering b a.

Resources