Data Structures: Binary tree traversal - binary-tree

Hi I'm a bit confused with this tree and need help in figuring out if I'm choosing the right answer.
Tree :
A
/ \
B C
/ \
D E
Lets do the traversal first:
In-order : BADCE
Pre-Order : ABCDE
Post-Order : BDECA
Questions:
Which of the following traversals yields BADEC?
a. only in-order
b. only level order
c. only post-order
d. only pre-order
e. pre-order and level order
f. in-order and level order
g. none of the above
Answer g
Which of the following is a post-order traversal of the BST?
a. ACEDB
b. ABDCE
c. BDECA
d. EDCBA
e. BADCE
f. BADEC
g. one of the above
Answer g
Can someone please confirm if I have done the traversal correctly and have chosen the correct answer for both question.
Thanks

The three traversal algorithms is a recursive algorithm. This means that in order to traverse the entire tree rooted at node A, the algorithm will split and finish the task in three parts:
traverse the subtree rooted at B (A's left child)
traverse the subtree rooted rooted at C (A's right child)
traverse/visit A itself
The order of the three tasks depends on which order you use:
- In-order (Left, Root, Right) does task1, task3, and then task2. - Pre-order (Root, Left, Right) does task3, task1, and then task2. - Post-order (Left, Right, Root) does task1, task2, and then task3
Continue the recursive algorithm: to traverse the subtree rooted at B, it will split the task further and traverse the subtree rooted at B's left child, the subtree rooted at B's right child, and then B.
The "splitting task" continues until the subtree to traverse contains only one root node. In this case, the algorithm visits the root node and returns to the remaining sub-tasks. Same thing happens to the subtree rooted at C, A's right child.
Here are the detailed steps to traverse the tree in the question in 3 different orders and to answer the questions using the traversal results:
In-order traversal (Left, Root, Right):
traverse subtree rooted at B
visit B
visit A
traverse subtree rooted at C
traverse C's left subtree
visit D
visit C
traverse C's right subtree
visit E
In-order: BADCE
Pre-order traversal (Root, Left, Right)
visit A
traverse subtree rooted at B
visit B
traverse subtree rooted at C
visit C
traverse C's left subtree
visit D
traverse C's right subtree
visit E
Pre-order: ABCDE
Post-order traversal (Left, Right, Root)
traverse subtree rooted at B
visit B
traverse subtree rooted at C
traverse C's left subtree
visit D
traverse C's right subtree
visit E
visit C
visit A
Post-order: BDECA
You can check if your traversal results are the same as above.
Looking at the traversal results, we know that the answer to your question 1 is g, and the answer to your question 2 is c.

Related

Is a height-balanced tree a tree where a node with exactly one child must have a leaf as its only child?

https://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees says:
A balanced binary tree is a binary tree structure in which the left and right subtrees of every node differ in height by no more than 1.
Is it equivalent to say that a binary tree is height-balanced if a node with exactly one child must have a leaf as its only child?
Is it equivalent to say that a binary tree is height-balanced if a node with exactly one child must have a leaf as its only child?
No. This is a correct observation about balanced binary trees, but it is not a complete definition of balanced binary trees. While all balanced trees have this property, it is easy to produce unbalanced trees that also have this property.
To start, your observation is correct when applied to balanced trees:
In a binary tree, a node with only one child still has two subtrees, one of which is empty and has a height of 0. By definition, in a balanced binary tree, the other non-empty subtree must have a height of 0 or 1. So, if the node has one child, then that child is necessarily a leaf node with a subtree height of 1, resulting in subtree heights of 0 and 1. Anything but a leaf node is an unbalanced tree with subtree heights of 0 and >1.
For example, consider a node A which has as its left child a leaf node B, and no right child:
A
/
B
The heights of A's left subtree is 1, and its right subtree is 0, so this is a balanced tree: The difference in heights is 1.
If A continues to have a single child B, and any node exists under B such that it is not a leaf, then A's left subtree height becomes greater than 1, while the right subtree height remains 0:
A
/
B
/
C
This is by definition not a balanced tree.
However, it is possible, and easy, to produce an unbalanced binary tree that still satisfies your modified definition:
a binary tree is height-balanced if a node with exactly one child must have a leaf as its only child?
The following tree satisfies this criteria: The only node with 1 child, D, has a leaf node as its child. However, the tree is clearly not balanced:
A
/ \
B C
/ \
D E
/
F
You could extend the left subtree indefinitely, and it would still satisfy your criteria; only the second to last node needs to have one child which is a leaf, all other nodes have either 0 or 2 children.
A
/ \
B C
/ \
D E
/ \
F G
/ \
H I
/ \
J K
/
L

Traversing a tree, predecessor and successor

I am facing a couple of doubts in tree data structure.
1) Is tree traversing (Preorder, Inorder, Postorder) possible in all types of trees or only binary trees.
2) If the first point is valid, then can we simply Inorder traverse a tree and store the elements in an array.
And then by using that array can we find predecessor and successor, as elements coming before and after a given element.
1.
Tree traversing (Preorder, Inorder, Postorder) possible in all types of trees
not only in binary trees.
every tree contains child nodes.( binary tree 2, ternary tree 3 etc).
We need to define our own way of traversing the trees.
lets take example of ternary tree.
Preorder : visit(root.data) - >root.left -> root.middle -> root.right
postorder: root.left -> root.middle - > root.right -> visit(root.data)
inorder : root.left -> root.middle -> visit(root.data) - > root.right
Preorder : visit all k child nodes( from left to right) after first visiting root node.
Postorder : visit all k child nodes (from left to right) before visiting root node.
Inorder : visit k/2 child nodes( from left to right) and visit root node then visit remaining k/2 child nodes.
we can store the tree in an array by inorder traversing.
we can find the predecessor and successor from that array by how we did inorder traversal.
for a binary tree, predecessor = left child, successor = right child
for ternary tree, predecessor = left child or middle child , successor = middle child or right child. ( we have to specify it based on our requirement)

Binary tree preorder visit pseudocode

Have been searching on web for more than 5 hours and cant find a general BT Preorder visit pseudocode.
Thanks in advance.
I just find short pseudocodes like this
Algorithm postorder(T, v)
Input: A binary tree T and a node v of T.
Output: Depends on the action performed on a visit to a node.
if T.hasLeft(v)
postorder(T, T.left(v)) // recursively traverse left subtree
if T.hasRight(v)
postorder(T, T.right(v)) // recursively traverse right subtree
visit node v
The difference between preorder, inorder, and postorder is simply the order in which the nodes are visited, relative to the children:
You posted this:
Algorithm postorder(T, v)
Input: A binary tree T and a node v of T.
Output: Depends on the action performed on a visit to a node.
if T.hasLeft(v)
postorder(T, T.left(v)) // recursively traverse left subtree
if T.hasRight(v)
postorder(T, T.right(v)) // recursively traverse right subtree
visit node v
To change among the behaviors, change the order of execution. Here's some generic code:
AnyOrder:
AnyOrder(T, v, order)
if order is 'pre'
visit(v)
AnyOrder(T, T.left(v), order)
if order is 'in'
visit(v)
AnyOrder(T, T.right(v), order)
if order is 'post'
visit(v)

Finding all subtrees of n-ary tree

I am trying to find all subtrees of n-ary tree. Only BFS or DFS does not work. Because the tree is not binary. For example:
1
/ \
2 3
/ \ /|\
4 6 5 7 8
/ \
9 10
I want to show all subtrees including this one
1
/ \
2 3
\ |
6 7
How can I extract this subtree from original one?
To generate all (graph-theoretic) subtrees of a given tree, I will need some auxiliary notions.
A subtree is a connected subgraph pf a tree, or equivalently, a subgraph which is also a tree.
A descendant tree of a rooted tree is either the original rooted tree itself, or a rooted tree which is a child of one of its vertices. (Won't give an exact definition here as it should be clear from the notion of a tree as a recursive data structure).
A rooted subtree of a rooted tree is a subtree that has the same root as the original rooted tree. We can get a rooted subtree of a rooted tree by computing rooted subtrees of (some of) immediate children of the root, and combining those with the original root.
Note that an arbitrary subtree is a rooted subtree of a descendant tree.
I will deal with non-empty trees for simplicity.
-- a (rooted) tree is a root node and a list of children attached to it
data Tree a = Node a [Tree a] deriving Show
It is straightforward to get the descendants:
-- a descendant tree is either a tree itself,
-- or a descendant of a child of its root
descendants :: Tree a -> [Tree a]
descendants t#(Node a ts) = t : concatMap descendants ts
Rooted subtrees are not much harder:
-- to get a rooted subtree, take a root, choose which children to
-- retain, take a rooted subtree of each retained child,
-- and attach the results to a copy of the root
rootedSubtrees :: Tree a -> [Tree a]
rootedSubtrees (Node a ts) = [Node a tts |
tts <- choices (map rootedSubtrees ts)]
-- this function receives a list of lists and generates all lists that
-- contain 0 or 1 element from each input list
-- for ex. choices ["ab", "cd"] = ["","c","d","a","ac","ad","b","bc","bd"]
choices :: [[a]] -> [[a]]
choices [] = [[]]
choices (xs:xxs) = cs ++ [x:c | x <- xs, c <- cs] where cs = choices xxs
Finally, the list of arbitrary subtrees is
subtrees :: Tree a -> [Tree a]
subtrees t = concatMap rootedSubtrees (descendants t)
You could do the following.
For each vertex in the tree you decide whether to cut the subtree with root the vertex or to keep exploring. Your number of choices is ~2^(number of nodes). Note it is note exactly 2^(number of nodes) (it's less but still exponential) since after you cut a subtree you don't explore it.
For each configuration of choices for each vertex print the tree using DFS.
Your example is the configuration in which the subtree with roots 4, 5, 8 were cut.
The cuts could be done implicitly by having flags for each node.

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