Week 9 Lab A

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

NAME-HARSH BATHLA

ENROLLEMENT NO –LATG10090
1.Write a program to construct a k-ary tree of any order greater than 2 having at least 10 nodes. Now
traverse the tree using BFS & DFS.
DFS:
#include<iostream>
using namespace std;
class Node {
public:
int data;
Node *children[4];
Node(int val)
{
data = val;
}
};
void inorder(Node *node) {
if (node == NULL)
return;
for (int i = 0; i < 3; i++)
inorder(node->children[i]);
cout<< node->data << " ";
inorder(node->children[3]);
}
void preorder(Node *node)
{
if (node == NULL)
return;
cout<< node->data << " ";
for (int i = 0; i < 3; i++)
preorder(node->children[i]);
preorder(node->children[3]);
}
void postorder(Node *node)
{
if (node == NULL)
return;
for (int i = 0; i < 3; i++)
postorder(node->children[i]);
postorder(node->children[3]);
cout<< node->data << " ";
}

int main() {
Node* root = new Node(1);
root->children[0] = new Node(2);
root->children[1] = new Node(3);
root->children[2] = new Node(4);
root->children[0]->children[0] = new Node(5);
root->children[0]->children[1] = new Node(6);
root->children[0]->children[2] = new Node(7);
root->children[0]->children[0]->children[0] = new Node(8);
root->children[0]->children[0]->children[1] = new Node(9);
root->children[0]->children[0]->children[2] = new Node(10);
inorder(root);
cout<<endl;
postorder(root);
cout<<endl;
preorder(root);
return 0;
}

BFS:
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int key;
vector<Node *>child;
};
Node *newNode(int key)
{
Node *temp = new Node;
temp->key = key;
return temp;
}
void LevelOrderTraversal(Node * root)
{
if (root==NULL)
return;
queue<Node *> q;
q.push(root);
while (!q.empty())
{
int n = q.size();
while (n > 0)
{
Node * p = q.front();
q.pop();
cout << p->key << " ";

for (int i=0; i<p->child.size(); i++)


q.push(p->child[i]);
n--;
}

cout << endl;


}
}
int main()
{
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;
}

2.Write a program to check the depth of a given K-ary tree.


#include <bits/stdc++.h>
using namespace std;

int findHeight(int* parent, int n)


{
int res = 0;
for (int i = 0; i < n; i++) {
int p = i, current = 1;
while (parent[p] != -1) {
current++;
p = parent[p];
}

res = max(res, current);


}
return res;
}
int main()
{
int parent[] = { -1,0,1,1, 6, 0, 0, 2, 7 };
int n = sizeof(parent) / sizeof(parent[0]);
int height = findHeight(parent, n);
cout << "Height of the given tree is: " << height << endl;
return 0;
}

3.You are given a k-ary tree of order greater than 2 having at least 10 nodes. Each node has an
integer key. Device a way to check whether the sum of the keys of all the nodes is even or not.
#include <bits/stdc++.h>
using namespace std;

struct Node {
int key;
Node* left, *right;
};

Node* newNode(int key)


{
Node* node = new Node;
node->key = key;
node->left = node->right = NULL;
return (node);
}
int addBT(Node* root)
{
if (root == NULL)
return 0;
return (root->key + addBT(root->left) + addBT(root->right));
}
int main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
root->right->left->right = newNode(8);

int sum = addBT(root);


cout << "Sum of all the elements is: " << sum << endl;
if(sum % 2==0)
{
cout<<"even";
}
else
{
cout<<"odd";
}
return 0;
}

4.You are given a k-ary tree of order greater than 2 having at least 10 nodes. Each node has an
integer key. Device a way to print the count of nodes having odd keys and even keys.

#include<bits/stdc++.h>

using namespace std;

int evensum=0;int oddsum=0;int rec_ctr=0;

class Node

public :
int data;

struct Node **children;

int length;

Node()

length = 0;

data = 0;

Node(int n, int data_)

children = new Node*();

length = n;

data = data_;

};

void calcOddEvenNode(Node *node)

if (node == NULL)

return;

int total = node->length;

for (int i = 0; i < total - 1; i++)

calcOddEvenNode(node->children[i]);

if(node->data%2==0)
evensum++;

else

oddsum++;

calcOddEvenNode(node->children[total - 1]);

if(evensum+oddsum==7 && rec_ctr==0)

cout<<"Number of even nodes in the tree is "<<evensum<<"\n";

cout<<"Number of odd nodes in the tree is "<<oddsum<<"\n";

rec_ctr++;

int main()

int n = 3;

Node* root = new Node(n, 1);

root->children[0] = new Node(n, 2);

root->children[1] = new Node(n, 3);

root->children[2] = new Node(n, 4);

root->children[0]->children[0] = new Node(n, 5);

root->children[0]->children[1] = new Node(n, 6);

root->children[0]->children[2] = new Node(n, 7);

calcOddEvenNode(root);

return 0;

You might also like