You are on page 1of 9

Question 1

“Header.h” :
#pragma once

struct node {
int data;
struct node* left;
struct node* right;
};
struct node* createnode(int data);
void insert(struct node* root, int key);
void smallest_node(struct node* root);
void largest_node(struct node* root);
struct node* left_subtree(struct node* root);
struct node* deletenode(struct node* root, int node);
void preorder(struct node* root);
void postorder(struct node* root);
void inorder(struct node* root);
struct node* search(struct node* root, int key);
int Nodes_count(struct node* root);
void LeafNodeCount(node* root, int& Count);
int treeHeight(struct node* root);
int nodeLevel(struct node* root, int data, int level);

“20F-0315_3A.cpp/source.cpp” :
#pragma once

#include <Windows.h>
#include<iostream>
#include"Header.h"
using namespace std;
int node_counter = 0;

//--------Create Node
struct node* createnode(int data) {
struct node* tree_node = (struct node*)malloc(sizeof(struct node));
tree_node->data = data;
tree_node->left = NULL;
tree_node->right = NULL;
return tree_node;
}

//-----------Insertion
void insert(struct node* root, int key) {
struct node* prev = NULL;
while (root != NULL)
{
prev = root;
if (key == root->data)
return;
else if (key < root->data)
root = root->left;
else
root = root->right;
}
struct node* ptr = createnode(key);
if (key < prev->data)
prev->left = ptr;
else
prev->right = ptr;
}

//--------Smallest and Largest


void smallest_node(struct node* root)
{
while (root != NULL && root->left != NULL)
{
root = root->left;
}
cout << "Smallest value in the BST is : " << root->data;
}
void largest_node(struct node* root)
{
while (root != NULL && root->right != NULL)
{
root = root->right;
}
cout << "Largest value in the BST is :" << root->data;
}

//-------------Deletion
struct node* left_subtree(struct node* root) {
root = root->left;
while (root->right != NULL) {
root = root->right;
}
return root;
}
struct node* deletenode(struct node* root, int node) {
struct node* left_node;
if (root == NULL) {
return NULL;
}
if (root->left == NULL && root->right == NULL) {
delete(root);
return NULL;
}
if (node < root->data) {
root->left = deletenode(root->left, node);
}
else if (node > root->data) {
root->right = deletenode(root->right, node);
}
else {

left_node = left_subtree(root);
root->data = left_node->data;
root->left = deletenode(root->left, left_node->data);
}
return root;
}

//---------Inorder & Perorder & Postorder


void preorder(struct node* root) {
if (root != NULL) {
cout << root->data << " ";
preorder(root->left);
preorder(root->right);
}
}
void postorder(struct node* root) {
if (root != NULL) {
postorder(root->left);
postorder(root->right);
cout << root->data << " ";
}
}
void inorder(struct node* root) {
if (root != NULL) {
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
}

//-------Searching
struct node* search(struct node* root, int key) {
if (root == NULL) {
return NULL;
}
if (root->data == key) {
return root;
}
else if (root->data > key) {
return search(root->left, key);
}
else
return search(root->right, key);
}

//--- Node Counter


int Nodes_count(struct node* root)
{
if (root == NULL)
return 0;
if (root->left != NULL)
{
node_counter = node_counter + 1;
node_counter = Nodes_count(root->left);
}
if (root->right != NULL)
{
node_counter = node_counter + 1;
node_counter = Nodes_count(root->right);
}
return node_counter;
}

//--- Leaf Node Counter


void LeafNodeCount(node* root, int& Count)
{
if (root == NULL) {
return;
}
LeafNodeCount(root->left, Count);
LeafNodeCount(root->right, Count);
if (root->left == NULL && root->right == NULL) {
Count++;
}

//---Height
int treeHeight(struct node* root) {
int leftsubtree_height, rightsubtree_height;

if (root == NULL)
return false;

leftsubtree_height = treeHeight(root->left);
rightsubtree_height = treeHeight(root->right);
if (leftsubtree_height >= rightsubtree_height)
return leftsubtree_height + 1;
else
return rightsubtree_height + 1;

}
//--- Node Level
int nodeLevel(struct node* root, int data, int level) {
int subtree_level;

if (root == NULL)
return false;
if (root->data == data)
return level;

subtree_level = nodeLevel(root->left, data, level + 1);


if (subtree_level != 0)
return subtree_level;
subtree_level = nodeLevel(root->right, data, level + 1);
return subtree_level;
}

“main.cpp” :
#pragma once
#include<iostream>
#include"Header.h"
using namespace std;
int main() {
int choice;
int key;
int nodeCount = 0;
int count = 0;
int Count = 0;
int height;

struct node* root = createnode(65);


insert(root, 55);
insert(root, 22);
insert(root, 44);
insert(root, 61);
insert(root, 19);
insert(root, 90);
insert(root, 10);
insert(root, 78);
insert(root, 52);
//---------------------

while (true) {
cout << "\n";
cout << "\n\t\t\t\t*******BST MANUAL*******" << endl;
cout << "\t__Enter__\n";
cout << "\n1. For Insertion of a Node\n";
cout << "2. For Deletion of a Node\n";
cout << "3. For Searching a Node\n";
cout << "4. To Get Largest Value in the BST\n";
cout << "5. To Get Smallest Value in the BST\n";
cout << "6. For Inorder Traversal\n";
cout << "7. For Preorder Traversal\n";
cout << "8. For Postorder Traversal\n";
cout << "9. For Getting the Height of the Tree\n";
cout << "10. For Getting No. of Nodes in the BST\n";
cout << "11. For Getting the No. of Leaf Nodes\n";
cout << "12. For Getting the Level of a Specific Node\n";
cout << "13. To Exit\n";

cin >> choice;


switch (choice)
{
case 1: {
cout << "Enter the Node you want to Insert\n";
cin >> key;
insert(root, key);
}
break;
case 2: {
cout << "Enter the Node you wannt to Delete\n";
cin >> key;
deletenode(root, key);
}
break;
case 3: {
cout << "Enter the Node you want to Search\n";
cin >> key;
struct node* n = search(root, key);
if (n != NULL) {
cout << "Element Found :" << n->data << "\n";
}
else
cout << "Element not Found" << endl;
}
break;
case 4: {
largest_node(root);
}
break;
case 5: {
smallest_node(root);
}
break;
case 6: {
cout << "The Inorder Traversal of BST : ";
inorder(root);
}
break;
case 7: {
cout << "The Preorder Traversal of BST : ";
preorder(root);
}
break;
case 8: {
cout << "The Postorder Traversal of BST : ";
postorder(root);
}
break;
case 9: {
cout << "The Height of the Tree is :" << treeHeight(root);
}
break;
case 10: {
cout << "Total No. of Nodes in the BST : " << Nodes_count(root) <<
endl;
}
break;
case 11: {
LeafNodeCount(root, Count);
cout << endl;
cout << "Number of Leaf Nodes are/is : " << Count;
}
break;
case 12: {
cout << "Enter the Node whose Level you want to Know\n";
cin >> key;
cout << "The Noode is present at Level : " << nodeLevel(root, key,
1);
}
break;
case 13: {
exit(0);
}
break;
default:
break;
}
//system("cls");
}

system("pause>0");
return 0;
}

Question 2
#include<iostream>
using namespace std;

struct node {
int data;
struct node* left;
struct node* right;
};

struct node* createnode(int data) {


struct node* tree_node = (struct node*)malloc(sizeof(struct node));
tree_node->data = data;
tree_node->left = NULL;
tree_node->right = NULL;
return tree_node;
}

void preorder(struct node* root) {


if (root != NULL) {
cout << root->data << " ";
preorder(root->left);
preorder(root->right);
}
}
void postorder(struct node* root) {
if (root != NULL) {
postorder(root->left);
postorder(root->right);
cout << root->data << " ";
}
}
void inorder(struct node* root) {
if (root != NULL) {
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
}

int main() {

struct node* root = createnode(5);


struct node* node_1 = createnode(3);
struct node* node_2 = createnode(6);
struct node* node_3 = createnode(1);
struct node* node_4 = createnode(4);

root->left = node_1;
root->right = node_2;

node_1->left = node_3;
node_1->right = node_4;

cout << "Pre Order :";


preorder(root);
cout << "\n";

cout << "Post Order :";


postorder(root);
cout << "\n";

cout << "In Order :";


inorder(root);
cout << "\n";

system("pause>0");
return 0;
}

You might also like