You are given a set of links, e.g.
a ---> b b ---> c b ---> d a ---> e
Print the tree that would form when each pair of these links that has the same character as start and end point is joined together. You have to maintain fidelity w.r.t. the height of nodes, i.e. nodes at height n from root should be printed at same row or column. For set of links given above, tree printed should be –
-->a |-->b | |-->c | |-->d |-->e
Note that these links need not form a single tree; they could form, ahem, a forest. Consider the following links
a ---> b a ---> g b ---> c c ---> d d ---> e c ---> f z ---> y y ---> x x ---> w
The output would be following forest.
-->a |-->b | |-->c | | |-->d | | | |-->e | | |-->f |-->g -->z |-->y | |-->x | | |-->w
You can assume that given links can form a tree or forest of trees only, and there are no duplicates among links.
Solution: The idea is to maintain two arrays, one array for tree nodes and other for trees themselves (we call this array forest). An element of the node array contains the TreeNode object that corresponds to respective character. An element of the forest array contains Tree object that corresponds to respective root of tree.
It should be obvious that the crucial part is creating the forest here, once it is created, printing it out in required format is straightforward. To create the forest, following procedure is used –
Do following for each input link, 1. If start of link is not present in node array Create TreeNode objects for start character Add entries of start in both arrays. 2. If end of link is not present in node array Create TreeNode objects for start character Add entry of end in node array. 3. If end of link is present in node array. If end of link is present in forest array, then remove it from there. 4. Add an edge (in tree) between start and end nodes of link.
It should be clear that this procedure runs in linear time in number of nodes as well as of links – it makes only one pass over the links. It also requires linear space in terms of alphabet size.
Following is Java implementation of above algorithm. In the following implementation characters are assumed to be only lower case characters from ‘a’ to ‘z’.
------------ Forest 1 ---------------- -->a |-->b | |-->c | |-->d |-->e ------------ Forest 2 ---------------- -->a |-->b | |-->c | | |-->d | | | |-->e | | |-->f |-->g -->z |-->y | |-->x | | |-->w
In the above implementation, endpoints of input links are assumed to be from set of only 26 characters. Extend the implementation where endpoints are strings of any length.
This article is contributed by Ciphe. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Tree Isomorphism Problem
- The Great Tree-List Recursion Problem.
- Applications of Minimum Spanning Tree Problem
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Convert a given Binary tree to a tree that holds Logical AND property
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Sub-tree with minimum color difference in a 2-coloured tree
- Cartesian tree from inorder traversal | Segment Tree
- Check if the given binary tree has a sub-tree with equal no of 1's and 0's | Set 2
- Create a mirror tree from the given binary tree