Given a generic tree, perform a Level order traversal and print all of its nodes
Examples:
Input : 10
/ / \ \
2 34 56 100
/ \ | / | \
77 88 1 7 8 9
Output : 10
2 34 56 100
77 88 1 7 8 9
Input : 1
/ / \ \
2 3 4 5
/ \ | / | \
6 7 8 9 10 11
Output : 1
2 3 4 5
6 7 8 9 10 11
The approach to this problem is similar to Level Order traversal in a binary tree. We Start with pushing root node in a queue and for each node we pop it,print it and push all its child in the queue.
In case of a generic tree we store child nodes in a vector. Thus we put all elements of the vector in the queue.
C++
// CPP program to do level order traversal // of a generic tree #include <bits/stdc++.h> using namespace std; // Represents a node of an n-ary tree struct Node { int key; vector<Node *>child; }; // Utility function to create a new tree node Node *newNode(int key) { Node *temp = new Node; temp->key = key; return temp; } // Prints the n-ary tree level wise void LevelOrderTraversal(Node * root) { if (root==NULL) return; // Standard level order traversal code // using queue queue<Node *> q; // Create a queue q.push(root); // Enqueue root while (!q.empty()) { int n = q.size(); // If this node has children while (n > 0) { // Dequeue an item from queue and print it Node * p = q.front(); q.pop(); cout << p->key << " "; // Enqueue all children of the dequeued item for (int i=0; i<p->child.size(); i++) q.push(p->child[i]); n--; } cout << endl; // Print new line between two levels } } // Driver program int main() { /* Let us create below tree * 10 * / / \ \ * 2 34 56 100 * / \ | / | \ * 77 88 1 7 8 9 */ Node *root = newNode(10); (root->child).push_back(newNode(2)); (root->child).push_back(newNode(34)); (root->child).push_back(newNode(56)); (root->child).push_back(newNode(100)); (root->child[0]->child).push_back(newNode(77)); (root->child[0]->child).push_back(newNode(88)); (root->child[2]->child).push_back(newNode(1)); (root->child[3]->child).push_back(newNode(7)); (root->child[3]->child).push_back(newNode(8)); (root->child[3]->child).push_back(newNode(9)); cout << "Level order traversal Before Mirroring\n"; LevelOrderTraversal(root); return 0; } |
Java
// Java program to do level order traversal // of a generic tree import java.util.*; class GFG { // Represents a node of an n-ary tree static class Node { int key; Vector<Node >child = new Vector<>(); }; // Utility function to create a new tree node static Node newNode(int key) { Node temp = new Node(); temp.key = key; return temp; } // Prints the n-ary tree level wise static void LevelOrderTraversal(Node root) { if (root == null) return; // Standard level order traversal code // using queue Queue<Node > q = new LinkedList<>(); // Create a queue q.add(root); // Enqueue root while (!q.isEmpty()) { int n = q.size(); // If this node has children while (n > 0) { // Dequeue an item from queue // and print it Node p = q.peek(); q.remove(); System.out.print(p.key + " "); // Enqueue all children of // the dequeued item for (int i = 0; i < p.child.size(); i++) q.add(p.child.get(i)); n--; } // Print new line between two levels System.out.println(); } } // Driver Code public static void main(String[] args) { /* Let us create below tree * 10 * / / \ \ * 2 34 56 100 * / \ | / | \ * 77 88 1 7 8 9 */ Node root = newNode(10); (root.child).add(newNode(2)); (root.child).add(newNode(34)); (root.child).add(newNode(56)); (root.child).add(newNode(100)); (root.child.get(0).child).add(newNode(77)); (root.child.get(0).child).add(newNode(88)); (root.child.get(2).child).add(newNode(1)); (root.child.get(3).child).add(newNode(7)); (root.child.get(3).child).add(newNode(8)); (root.child.get(3).child).add(newNode(9)); System.out.println("Level order traversal " + "Before Mirroring "); LevelOrderTraversal(root); } } // This code is contributed by Rajput-Ji |
C#
// C# program to do level order traversal // of a generic tree using System; using System.Collections.Generic; class GFG { // Represents a node of an n-ary tree public class Node { public int key; public List<Node >child = new List<Node>(); }; // Utility function to create a new tree node static Node newNode(int key) { Node temp = new Node(); temp.key = key; return temp; } // Prints the n-ary tree level wise static void LevelOrderTraversal(Node root) { if (root == null) return; // Standard level order traversal code // using queue Queue<Node > q = new Queue<Node >(); // Create a queue q.Enqueue(root); // Enqueue root while (q.Count != 0) { int n = q.Count; // If this node has children while (n > 0) { // Dequeue an item from queue // and print it Node p = q.Peek(); q.Dequeue(); Console.Write(p.key + " "); // Enqueue all children of // the dequeued item for (int i = 0; i < p.child.Count; i++) q.Enqueue(p.child[i]); n--; } // Print new line between two levels Console.WriteLine(); } } // Driver Code public static void Main(String[] args) { /* Let us create below tree * 10 * / / \ \ * 2 34 56 100 * / \ | / | \ * 77 88 1 7 8 9 */ Node root = newNode(10); (root.child).Add(newNode(2)); (root.child).Add(newNode(34)); (root.child).Add(newNode(56)); (root.child).Add(newNode(100)); (root.child[0].child).Add(newNode(77)); (root.child[0].child).Add(newNode(88)); (root.child[2].child).Add(newNode(1)); (root.child[3].child).Add(newNode(7)); (root.child[3].child).Add(newNode(8)); (root.child[3].child).Add(newNode(9)); Console.WriteLine("Level order traversal " + "Before Mirroring "); LevelOrderTraversal(root); } } // This code is contributed by Rajput-Ji |
Output:
10 2 34 56 100 77 88 1 7 8 9
This article is contributed by Raghav Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
Recommended Posts:
- Deletion of a given node K in a Binary Tree using Level Order Traversal
- Level order traversal of Binary Tree using Morris Traversal
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Flatten Binary Tree in order of Level Order Traversal
- Insertion in n-ary tree in given order and Level order traversal
- Level Order Tree Traversal
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Boundary Level order traversal of a Binary Tree
- Zig Zag Level order traversal of a tree using single array
- Specific Level Order Traversal of Binary Tree
- Density of Binary Tree using Level Order Traversal
- Perfect Binary Tree Specific Level Order Traversal
- Perfect Binary Tree Specific Level Order Traversal | Set 2
- Calculate height of Binary Tree using Inorder and Level Order Traversal
- Check if the level order traversal of a Binary Tree results in a palindrome
- Build Binary Tree from BST such that it's level order traversal prints sorted data
- Check if the given array can represent Level Order Traversal of Binary Search Tree
- Level Order Successor of a node in Binary Tree
- Level Order Predecessor of a node in Binary Tree
Improved By : Rajput-Ji

