C Program: Binary Tree level-order traversal
7. Level-Order Traversal Variants
Write a C program that extends the binary tree program to perform a level-order traversal. Print the nodes at each level from top to bottom.
Sample Solution:
C Code:
Output:
Input a value to insert into the binary tree (enter 0 to stop): 78 Input a value to insert into the binary tree (enter 0 to stop): 52 Input a value to insert into the binary tree (enter 0 to stop): 48 Input a value to insert into the binary tree (enter 0 to stop): 28 Input a value to insert into the binary tree (enter 0 to stop): 22 Input a value to insert into the binary tree (enter 0 to stop): 8 Input a value to insert into the binary tree (enter 0 to stop): 6 Input a value to insert into the binary tree (enter 0 to stop): 0 In-order Traversal (Sorted Order) of the Binary Tree: 6 8 22 28 48 52 78 Level-order Traversal of the Binary Tree: 78 52 48 28 22 8 6
Explanation:
In the exercise above,
- Struct Definition:
- Defines a structure struct TreeNode representing a node in a binary tree. Each node has an integer data, a pointer to the left child (left), and a pointer to the right child (right).
- Function to Create a Node:
- createNode function allocates memory for a new node, initializes its data, and sets both left and right pointers to 'NULL'.
- Function to Insert a Node:
- insertNode function inserts a node into the binary tree while maintaining the binary search tree property. It recursively compares the value to be inserted with the current node's value and traverses left or right accordingly.
- In-Order Traversal Function:
- inOrderTraversal performs an in-order traversal of the binary tree, printing the elements in sorted order.
- Level-Order Traversal Function:
- levelOrderTraversal performs a level-order traversal of the binary tree. It uses a queue to keep track of nodes at each level and prints them from top to bottom.
- Free Tree Function:
- freeTree recursively frees the memory allocated for the binary tree nodes.
- Main Function:
- The main function creates an empty binary tree and allows users to input values to insert into the tree. It stops when the user enters 0. Afterward, it prints the in-order traversal (sorted order) and the level-order traversal of the binary tree.
Flowchart:
For more Practice: Solve these Related Problems:
- Write a C program to perform a spiral (zigzag) level-order traversal of a binary tree.
- Write a C program to print the level-order traversal of a binary tree in reverse order, starting from the bottom level.
- Write a C program to execute a level-order traversal that prints each level on a separate line with its level number.
- Write a C program to implement level-order traversal recursively by computing the tree height and printing nodes level by level.
C Programming Code Editor:
Previous: C Program: Binary Tree mirroring for a mirror image.
Next: C Program: Expression Tree from Postfix Expression and Evaluation.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.