(Prog 5)
(Prog 5)
Section- IT-1
Part 3
Program 1:
1) Construction of Binary tree and insertion of new node
in binary tree.
Code-
#include <iostream>
using namespace std;
class BST
{ int data; BST
*left, *right;
public:
BST();
BST(int);
BST* Insert(BST*, int);
void Inorder(BST*);
};
BST ::BST()
: data(0)
, left(NULL)
, right(NULL)
{
}
return root;
}
int main()
{
BST b, *root = NULL;
root = b.Insert(root, 40);
b.Insert(root, 30);
b.Insert(root, 20);
b.Insert(root, 50);
b.Insert(root, 70);
b.Insert(root, 60);
b.Insert(root, 10);
b.Insert(root, 15);
b.Inorder(root);
return 0; }
OUTPUT
2)
Find the no of nodes in the longest path
Code-
#include <bits/stdc++.h>
struct Node {
int data;
Node* left, *right;
};
Node* getNode(int data)
{
if (!root) {
}
sumOfLongRootToLeafPath(root->left, sum + root->data,
len + 1, maxLen, maxSum);
if (!root)
return 0;
return maxLen;
}
int main()
{
Node* root = getNode(4); root-
>left = getNode(2); root->right =
getNode(5); root->left->left =
getNode(7); root->left->right =
getNode(1); root->right->left =
getNode(2); root->right->right =
getNode(3); root->left->right->left =
getNode(6);
return 0;
}
OUTPUT
3)
Minimum data value found in the tree.
Code-
#include <bits/stdc++.h>
using namespace std;
struct node
{ int data; struct
node* left; struct
node* right;
};
struct node* newNode(int data)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data; node->left = NULL;
node->right = NULL;
return(node);
}
int main()
{
struct node* root = NULL;
root = insert(root, 14);
insert(root, 21);
insert(root, 16);
insert(root, 31);
insert(root, 60);
insert(root, 52);
return 0;
}
OUTPUT
struct Node
{ int
data;
Node *left, *right;
};
return node;
}
else {
root->right = insert(root->right, key);
}
return root;
}
parent = curr;
if (curr == nullptr)
{
cout << "Key Not found";
return;
}
if (parent == nullptr) {
cout << "The node with key " << key << " is root node";
}
else if (key < parent->data) {
cout << "The given key is the left node of the node with key "
<< parent->data;
}
else {
cout << "The given key is the right node of the node with key "
<< parent->data;
}
}
int main()
{
int keys[] = { 15, 10, 20, 8, 12, 16, 25 };
searchIterative(root, 25);
return 0;
}
OUTPUT
5) Change a tree so that the roles of the left and right
pointers are swapped at every node
Code-
#include<bits/stdc++.h>
using namespace std;
struct Node
{ int data; struct
Node* left; struct
Node* right;
};
struct Node* newNode(int data)
{
struct Node* node = (struct Node*)
malloc(sizeof(struct Node));
node->data = data; node->left = NULL;
node->right = NULL;
return(node);
}
mirror(node->left); mirror(node-
>right); temp = node->left;
node->left = node->right; node->right =
temp;
}
}
void inOrder(struct Node* node)
{ if (node ==
NULL) return;
inOrder(node->left);
cout << node->data << " ";
inOrder(node->right);
}
int main()
{
struct Node *root = newNode(1);
root->left = newNode(2); root-
>right = newNode(3); root->left-
>left = newNode(4); root->left-
>right = newNode(5); cout <<
"Inorder traversal of the constructed"
mirror(root);
cout << "\n Inorder traversal of the mirror tree"
<< " is \n";
inOrder(root);
return 0;
}
OUTPUT