Professional Documents
Culture Documents
“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;
}
//-------------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;
}
//-------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);
}
//---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;
“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;
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";
system("pause>0");
return 0;
}
Question 2
#include<iostream>
using namespace std;
struct node {
int data;
struct node* left;
struct node* right;
};
int main() {
root->left = node_1;
root->right = node_2;
node_1->left = node_3;
node_1->right = node_4;
system("pause>0");
return 0;
}