Serialize a tree using DFS - algorithm

According to this post on Wikipedia, given a tree with distinct elements, either pre-order or post-order paired with in-order is sufficient to describe the tree uniquely. However, pre-order with post-order leaves some ambiguity in the tree structure.
I am looking for a quick example that would demonstrate this claim.
So given the following tree:
The orderings are:
Pre Order: 1, 2, 4, 3, 5, 7, 8, 6
In Order: 4, 2, 1, 7, 5, 8, 3, 6
Post Order: 4, 2, 7, 8, 5, 6, 3, 1
How can I deserialize this tree using its Pre Order and In Order or Post Order and In Order?
Thanks

So the claim is that given a pre-order and in-order traversal of the tree we can uniquely construct the tree.
In your example
Preorder : 1, 2, 4, 3, 5, 7, 8, 6
In Order : 4, 2, 1, 7, 5, 8, 3, 6
Now the root node in the tree will be the first node in the pre-order traversal. Moreover, all the numbers appearing before a value in the inorder traversal will lie in the left subtree rooted at that node. And similarly all values appearing after a value will lie in the right sub-tree. So continuing in this recursive fashion will deserialize the tree. You can read more about it here http://www.geeksforgeeks.org/construct-tree-from-given-inorder-and-preorder-traversal/
Similarly, you can deserialize the tree given its postorder and inorder traversal. Here the last value appearing in the post order traversal will be the root of the tree. You can read about the deserialization from post-order and in-order from here http://www.programcreek.com/2013/01/construct-binary-tree-from-inorder-and-postorder-traversal/

Related

Is my postorder traversal of this graph correct?

I am trying to implement an algorithm that requires a post-order traversal. Here is my graph (taken from here, pg. 8):
When I try to do a postorder traversal of this, the order I get is:
[3, 2, 1, 5, 4, 6]
The problem with this order is that the algorithm won't work in this order. This is the code I am using to get it (pseudocode):
function PostOrder(root, out_list) {
root.visited = true
for child in root.Children {
if not child.visited {
PostOrder(child, out_list)
}
}
out_list.append(root)
}
Is the postorder correct?
Yes, the post order traversal of your algorithm is correct. The expected output is indeed as you provided it.
Your confusion may come from the fact that the graph is not a binary tree, and not even a tree. It is a directed graph.
In general postorder means that you first perform a postorder traversal on the node behind the first outgoing edge, then on the node behind its next outgoing edge, ...etc, and only after all outgoing edges have been traversed, the node itself is output.
Since at node 1 you are not at the end yet, and still can go to 2, and from there to 3, you need to follow those edges before outputting anything. And only then backtrack.
For reference, here is your algorithm implemented in python:
def postorder(root, out_list, children, visited):
visited[root] = True
for child in children[root]:
if not visited[child]:
postorder(child, out_list, children, visited)
out_list.append(root)
children = [
[], # dummy for node 0
[2], # 1
[1,3], # 2
[2], # 3
[2,3], # 4
[1], # 5
[5,4] # 6
]
nodes = []
postorder(6, nodes, children, [False] * len(children))
print(nodes) # [3, 2, 1, 5, 4, 6]
I think you got confused with the postorder traversal of binary trees.
Postorder traversal in graph is different.
Post Ordering in Graphs – If we list the vertices in the order in which they are last visited by DFS traversal then the ordering is called PostOrder.
Assuming your root is node is 6, the order mentioned gives the correct answer.
Checkout the following example on how the post order traversal list is generated:
Pass 1:
List:[]
6 -> 5 -> 1 -> 2 -> 3 (Now Node 3 has no adjacent nodes which are unvisited)
List: [3]
Pass 2:
6 -> 5 -> 1 -> 2
Node 2 has has no adjacent nodes which are unvisited.
List: [3, 2]
Pass 3:
6 -> 5 -> 1
Node 1 has has no adjacent nodes which are unvisited.
List: [3, 2, 1]
Pass 4:
6 -> 5
Node 5 has has no adjacent nodes which are unvisited.
List: [3, 2, 1, 5]
Pass 5:
6 -> 4
Node 4 has has no adjacent nodes which are unvisited.
List: [3, 2, 1, 5, 4]
Pass 6:
Node 6 has has no adjacent nodes which are unvisited.
List: [3, 2, 1, 5, 4, 6]
Important Notes:
As we are using DFS, there can be multiple paths possible depending upon the order of the nodes in the adjacency list.
Possible are the correct orders:
[3, 2, 1, 5, 4, 6]
[1, 3, 2, 4, 5, 6]
[3, 1, 2, 4, 5, 6]
[1, 2, 3, 4, 5, 6]

How to find a list of unique shortest paths in a list of paths?

Imagine I have a tree graph as shown below, where a node can have multiple children, and so on.. (a node can have only 1 parent). If I have a list of paths along that graph, how can I find a subset of those paths that are unique and shortest?
Example Input (List of paths):
[1, 2, 3]
[1, 2]
[1, 7]
[1, 8, 9, 10]
Expected Output:
[1, 2]
[1, 7]
[1, 8, 9, 10]
The [1, 2, 3] path is ignored because it is longer than [1, 2], while the [1, 8, 9, 10] path is kept because it is unique.
First, sort the input paths by length. Maintain a set of leaf nodes. This will contain the last node of each valid path. After adding a leaf node, we'll forbid any path which includes that leaf node. When you add a path, check each of its members against the set of leaf nodes. If you get a match then the path is invalid, otherwise it's valid and you should add it's final element to the set of leaf nodes.
This is O(n log n) in the number of lists and linear in the number of elements across all lists.
Try to build a tree using these path. For each path, try to traverse from first node of the path to the last node of the path by setting edge to consecutive node.Mark last node of the path as leaf node after traversing each path. If you find any node marked as a leaf when traversing a path, you will stop traversing. Also remove the child of the node that marked as a leaf node. Every path from root node to leaf node in the final tree will be you answer. See figure below for more clarification:
Complexity will be sum of all path's length.

Creating A Binary Tree From Two Traversal Output

This is homework, but for some reason it would not allow me to add the homework tag.
We were assigned a lab for data structures in which the last question asked us to find the binary tree that would produce the following output from the given traversal methods:
LRN: 12, 9, 4, 7, 1, 14, 8, 13, 10, 15, 11, 2, 5, 16, 6, 3
and
LNR: 12, 3, 4, 9, 8, 1, 7, 14, 6, 13, 10, 16, 5, 15, 2, 11
I have identified the following about the tree:
The root node is 3. The root nodes left child and only left child of the tree is 12. The root nodes right child is 6. The furthest right node is 5.
Unfortunately I am stuck as to how to proceed. Any hints would be greatly appreciated.
From the post-order(LRN), we know that last element is the root. We can find the root in in-order(LNR). Then we can identify the left and right sub-trees of the root from in-order.
Using the length of left sub-tree, we can identify left and right sub-trees in post-order array. Recursively, we can build up the tree.
Check this link.

How many level order BST sequences are possible given a preOrder and inOrder sequence?

When I am trying to print level Order of BST, this question prompted me.
Here is a
Pre-Order Sequence: 4, 1, 2, 3, 5, 6, 7, 8
In_order Sequence : 1, 2, 3, 4, 5, 6, 7, 8
A level order sequence for a BST with above pre_order and In_order is
[4, 2, 6, 1, 3, 5, 7, 8]
However, for the same Pre-order an In-order sequence this level order sequence seems possible. [4, 1, 5, 2, 6, 3, 7, 8]. I don't know how. I am trying to figure this out.
I am unable to construct BST in paper (drawing) that satisfies all the pre_order, In-order and level order sequences.
If you have in-order traversal together with one of pre/post-order, that is enough to reconstruct a binary tree. Moreover, in case of BST (binary search tree), post-order or pre-order alone is sufficient.
In your case, reconstructing a BST from pre-order 4, 1, 2, 3, 5, 6, 7, 8 gives the following BST:
4
/ \
1 5
\ \
2 6
\ \
3 7
\
8
which gives, again unique, level-order traversal [4,1,5,2,6,3,7,8].
See also:
Reconstructing binary trees from tree traversals
Following combination will generate unique binary tree(which can be BST).
Inorder and Preorder.
Inorder and Postorder.
Inorder and Level-order.
So in your case inorder & pre order are given which will generate unique binary tree which is BST in your case so level order will be unique for that tree.
Pre-Order Sequence: 4, 1, 2, 3, 5, 6, 7, 8
In_order Sequence : 1, 2, 3, 4, 5, 6, 7, 8
SO tree will be
level 0- 4
level 1- 1,5
level 2- 2,6
level 3- 3,7
level 4- 8
Level order is
4,1,5,2,6,3,7,8
in sort there will always unique level order traversal

Binary Search Tree - Postorder logic

I am looking at implementing code to work out binary search tree.
Before I do this I was wanting to verify my input data in postorder and preorder.
I am having trouble working out what the following numbers would be in postorder and preorder
I have the following numbers 4, 3, 14 ,8 ,1, 15, 9, 5, 13, 10, 2, 7, 6, 12, 11, that I
am intending to put into an empty binary tree in that order.
The order I arrived at for the numbers in POSTORDER is
2, 1, 6, 3, 7, 11, 12, 10, 9, 8, 13, 15, 14, 4. Have I got this right?
I was wondering if anyone here would be able to kindly verify if the postorder sequence I came up with is indeed the correct sequence for my input i.e doing left subtree, right subtree and
then root.
The order I got for pre order (Visit root, do left subtree, do right subtree) is
4, 3, 1, 2, 5, 6, 14 , 8, 7, 9, 10, 12, 11, 15, 13. I can't be certain I got this right.
Very grateful for any verification.
Many Thanks
You haven't specified the structure of your tree (is it balanced?) nor the order in which you are inputting the data originally. If you do that we can verify your results

Resources