Given an N-ary tree, the task is to print the N-ary tree graphically.
Graphical Representation of Tree: A representation of tree in which the root is printed in a line and the children nodes are printed in subsequenct lines with some amount of indentation.
Input: 0 / | \ / | \ 1 2 3 / \ / | \ 4 5 6 7 8 | 9 Output: 0 +--- 1 | +--- 4 | +--- 5 +--- 2 +--- 3 +--- 6 +--- 7 | +--- 9 +--- 8
Approach: The idea is to traverse the N-ary Tree using DFS Traversal to traverse the nodes and explore its children nodes until all the nodes are visited and then similarly, traverse the sibling nodes.
The step-by-step algorithm for the above approach is described below –
- Intialize a variable to store the current depth of the node, for root node the depth is 0.
- Declare a boolean array to store the current exploring depths and initially mark all of them to False.
- If the current node is a root node that is the depth of the node is 0, then simply print the data of the node.
- Otherwise, Iterate over a loop from 1 to the current depth of node and print, ‘|’ and three spaces for each of the exploring depth and for non-exploring depth print three spaces only.
- Print the current value of the node and move the output pointer to the next line.
- If the current node is the last node of that depth then mark that depth as non-exploring.
- Similarly, explore all the child nodes with the recursive call.
Below is the implementation of the above approcah:
0 +--- 1 | +--- 4 | +--- 5 +--- 2 +--- 3 +--- 6 +--- 7 | +--- 9 +--- 8
- Time Complexity: In the above-given approach, there is recusive call to explore all the vertices which takes O(V) time. Therefore, the time complexity for this approach will be O(V).
- Auxiliary Space Complexity: In the above-given approach, there is extra space used to store the exploring depths. Therefore, the auxiliary space complexity for the above approach will be O(V)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Given a binary tree, print out all of its root-to-leaf paths one per line.
- Given a binary tree, print all root-to-leaf paths
- Print Ancestors of a given node in Binary Tree
- Print Left View of a Binary Tree
- Print a Binary Tree in Vertical Order | Set 1
- Print Right View of a Binary Tree
- Print a Binary Tree in Vertical Order | Set 2 (Map based Method)
- Print nodes between two given level numbers of a binary tree
- Print Nodes in Top View of Binary Tree
- Print cousins of a given node in Binary Tree
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Print leftmost and rightmost nodes of a Binary Tree
- Print Binary Tree in 2-Dimensions
- Print extreme nodes of each level of Binary Tree in alternate order
- Print all nodes in a binary tree having K leaves
- Print all the paths from root, with a specified sum in Binary tree
- Print all k-sum paths in a binary tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet