Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Print only Odd Numbered Levels of a Tree
In C++, to print the odd-numbered levels of a binary tree, the levels are numbered from the root as Level 1. The odd-numbered levels are Level 1, Level 3, and so on.
This program prints the nodes present at these odd levels. It uses level-order traversal to process the tree level by level and prints the nodes found at these levels.
Algorithm to Print only Odd Numbered Levels of a Tree
Following is the Algorithm to print odd numbered levels of a tree ?
- Create a structure for tree nodes with data and pointers to left and right children.
- Write a function to compute the height of the tree.
- Create a function to print all nodes at a specific level.
- Use a loop to traverse the tree and call the print function for odd levels only.
- Build the tree and call the function to print odd levels.
Pseudocode
Following is the Pseudocode to print odd numbered levels of a tree ?
Begin
Declare nod as a structure.
Declare d of integer datatype.
Declare a pointer l against struct nod.
Declare a pointer l against struct nod.
Call function struct nod* newNod(int d).
Declare struct nod* newNod(int d) function.
Declare a pointer node against struct node.
Initialize node = (struct nod*) malloc(sizeof(struct nod)).
node->d = d
node->l = NULL
node->r = NULL
return node.
Call function printLevel(struct nod* root, int lvl).
Declare function printLevel(struct nod* root, int lvl).
if (root == NULL) then
return
if (lvl == 1) then
print the values of root->d
else if (lvl > 1)
call printLevel(root->l, lvl - 1)
printLevel(root->r, lvl - 1)
Call function height(struct nod* node).
Declare function height(struct nod* node) to compute the height of tree.
if (node == NULL) then
return 0
else
int lhght = height(node->l);
int rhght = height(node->r);
if (lhght > rhght) then
return (lhght + 1)
else
return (rhght + 1)
Declare function printLevelOrder(struct nod* root).
declare h of the integer datatype.
initialize h = height(root).
declare i of the integer datatype.
for (i = 1; i <= h; i+=2)
call function printLevel(root, i).
insert values in the tree.
Print "Odd numbered Level Order traversal of binary tree is".
Call function printLevelOrder(root).
End
C++ Implementation
In this program, we demonstrate how to selectively traverse and print nodes at odd levels in a binary tree.
#include <iostream>
using namespace std;
// Define a structure for tree nodes
struct Node {
int data;
Node* left;
Node* right;
};
// Function to create a new node
Node* newNode(int data) {
Node* node = new Node();
node->data = data;
node->left = nullptr;
node->right = nullptr;
return node;
}
// Function to calculate the height of the tree
int height(Node* root) {
if (root == nullptr) return 0;
int leftHeight = height(root->left);
int rightHeight = height(root->right);
return max(leftHeight, rightHeight) + 1;
}
// Function to print nodes at a given level
void printLevel(Node* root, int level) {
if (root == nullptr) return;
if (level == 1) {
cout << root->data << " ";
} else if (level > 1) {
printLevel(root->left, level - 1);
printLevel(root->right, level - 1);
}
}
// Function to print nodes at odd levels
void printOddLevels(Node* root) {
int h = height(root);
for (int i = 1; i <= h; i += 2) { // Traverse only odd levels
printLevel(root, i);
}
}
// Driver code
int main() {
Node* root = newNode(7);
root->left = newNode(6);
root->right = newNode(4);
root->left->left = newNode(3);
root->left->right = newNode(5);
root->right->left = newNode(1);
root->right->right = newNode(2);
cout << "Odd-numbered Level Order traversal of binary tree = ";
printOddLevels(root);
return 0;
}
Output
For the given tree:
7
/ \
6 4
/ \ / \
3 5 1 2
Following is the output to the above program ?
Odd-numbered Level Order traversal of binary tree = 7 3 5 1 2
Advertisements