Skip to content

Commit

Permalink
Added levelorder,MaximumDepth and countofleafnodes program
Browse files Browse the repository at this point in the history
  • Loading branch information
kumar007ambi committed Oct 25, 2024
1 parent 13d6352 commit f0aea79
Show file tree
Hide file tree
Showing 3 changed files with 238 additions and 0 deletions.
96 changes: 96 additions & 0 deletions C++/Data-Structures/04-tree/levelOrder_traversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <bits/stdc++.h>
using namespace std;

struct Node
{
int data;
Node *left;
Node *right;
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};

// Function to insert a node into the binary tree
Node *insertNode(Node *root, int data)
{
if (!root)
{
return new Node(data);
}
queue<Node *> q;
q.push(root);
while (!q.empty())
{
Node *temp = q.front();
q.pop();

if (!temp->left)
{
temp->left = new Node(data);
break;
}
else
{
q.push(temp->left);
}

if (!temp->right)
{
temp->right = new Node(data);
break;
}
else
{
q.push(temp->right);
}
}
return root;
}

// Function to perform level order traversal
void levelOrderTraversal(Node *root)
{
if (!root)
return;
queue<Node *> q;
q.push(root);
while (!q.empty())
{
Node *temp = q.front();
q.pop();
cout << temp->data << " ";
if (temp->left)
q.push(temp->left);
if (temp->right)
q.push(temp->right);
}
cout << endl;
}

int main()
{
Node *root = nullptr;
int n, data;
cout << "Enter the number of nodes: ";
cin >> n;
cout << "Enter the node values:\n";
for (int i = 0; i < n; ++i)
{
cin >> data;
root = insertNode(root, data);
}
cout << "Level Order Traversal: ";
levelOrderTraversal(root);

return 0;
}


// Enter the number of nodes: 5
// Enter the node values:
// 1 2 3 4 5
// Level Order Traversal: 1 2 3 4 5
// 1
// / \
// 2 3
// / \
// 4 5
78 changes: 78 additions & 0 deletions C++/Data-Structures/04-tree/maximumDepth_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <bits/stdc++.h>
using namespace std;

struct TreeNode
{
int data;
TreeNode *left;
TreeNode *right;
TreeNode(int val) : data(val), left(NULL), right(NULL) {}
};
// Function to calculate maximum depth
int maxDepth(TreeNode *root)
{
if (root == NULL)
return 0;
else
{
int leftDepth = maxDepth(root->left);
int rightDepth = maxDepth(root->right);
return max(leftDepth, rightDepth) + 1;
}
}
// Function to insert nodes in level order
TreeNode *insertLevelOrder(int arr[], int n)
{
if (n == 0)
return NULL;
TreeNode *root = new TreeNode(arr[0]);
queue<TreeNode *> q;
q.push(root);
int i = 1;
while (i < n)
{
TreeNode *temp = q.front();
q.pop();
// Insert left child
if (i < n)
{
temp->left = new TreeNode(arr[i++]);
q.push(temp->left);
}
// Insert right child
if (i < n)
{
temp->right = new TreeNode(arr[i++]);
q.push(temp->right);
}
}
return root;
}

int main()
{
int n;
cout << "Enter number of nodes: ";
cin >> n;
int arr[n];
cout << "Enter node values in level order (use -1 for NULL nodes): ";
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
// Insert nodes in level order
TreeNode *root = insertLevelOrder(arr, n);
// Calculate maximum depth
int depth = maxDepth(root);
cout << "Maximum Depth of the Binary Tree: " << depth << endl;
return 0;
}

// Enter number of nodes: 7
// Enter node values in level order (use -1 for NULL nodes): 1 2 3 -1 -1 4 5
// Maximum Depth of the Binary Tree: 3
// 1
// / \
// 2 3
// / \
// 4 5
64 changes: 64 additions & 0 deletions C++/Data-Structures/04-tree/numberOfLeafNodes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <bits/stdc++.h>
using namespace std;

struct Node
{
int data;
Node *left;
Node *right;

Node(int val) : data(val), left(nullptr), right(nullptr) {}
};
// Function to insert nodes in the binary tree
Node *insertNode(Node *root, int val)
{
if (root == nullptr)
{
return new Node(val);
}
if (val < root->data)
{
root->left = insertNode(root->left, val);
}
else
{
root->right = insertNode(root->right, val);
}
return root;
}
// Function to count leaf nodes
int countLeafNodes(Node *root)
{
if (root == nullptr)
return 0;
if (root->left == nullptr && root->right == nullptr)
return 1;
return countLeafNodes(root->left) + countLeafNodes(root->right);
}

int main()
{
Node *root = nullptr;
int n, val;
cout << "Enter the number of nodes in the binary tree: ";
cin >> n;
cout << "Enter the values of nodes:\n";
for (int i = 0; i < n; i++)
{
cin >> val;
root = insertNode(root, val);
}
int leafCount = countLeafNodes(root);
cout << "Number of leaf nodes in the binary tree: " << leafCount << endl;
return 0;
}

// Enter the number of nodes in the binary tree: 7
// Enter the values of nodes:
// 4 2 6 1 3 5 7
// Number of leaf nodes in the binary tree: 4
// 4
// / \
// 2 6
// / \ / \
// 1 3 5 7

0 comments on commit f0aea79

Please sign in to comment.