You are on page 1of 22

ASSIGNMENT 3:

Part 1:

1)Check if two binary search trees are identical or not ( C language):

#include <stdio.h>

#include <stdlib.h>

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;

void inorder(struct Node* root)

if (root == NULL)

return;

inorder(root->left);
printf("%d " ,root->data);

inorder(root->right);

int isIdentical(struct Node* root1,struct Node* root2)

if (root1 == NULL && root2 == NULL)

return 1;

else if (root1 == NULL || root2 == NULL)

return 0;

else {

if (root1->data == root2->data && isIdentical(root1->left, root2->left)

&& isIdentical(root1->right, root2->right))

return 1;

else

return 0;

int main()

struct Node* root1 = newNode(5);

struct Node* root2 = newNode(5);

root1->left = newNode(3);

root1->right = newNode(8);

root1->left->left = newNode(2);

root1->left->right = newNode(4);

root2->left = newNode(3);
root2->right = newNode(8);

root2->left->left = newNode(2);

root2->left->right = newNode(4);

if (isIdentical(root1, root2))

printf( "Both BSTs are identical");

else

printf("BSTs are not identical") ;

return 0;

2) level order traversal of binary tree ( C language):

#include <stdio.h>

#include <stdlib.h>

struct node {

int data;

struct node *left, *right;

};

void printCurrentLevel(struct node* root, int level);

int height(struct node* node);

struct node* newNode(int data);

void printLevelOrder(struct node* root)


{

int h = height(root);

int i;

for (i = 1; i <= h; i++)

printCurrentLevel(root, i);

void printCurrentLevel(struct node* root, int level)

if (root == NULL)

return;

if (level == 1)

printf("%d ", root->data);

else if (level > 1) {

printCurrentLevel(root->left, level - 1);

printCurrentLevel(root->right, level - 1);

int height(struct node* node)

if (node == NULL)

return 0;

else {

int lheight = height(node->left);

int rheight = height(node->right);

if (lheight > rheight)

return (lheight + 1);

else

return (rheight + 1);


}

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 = newNode(1);

root->left = newNode(2);

root->right = newNode(3);

root->left->left = newNode(4);

root->left->right = newNode(5);

printf("Level Order traversal of binary tree is \n");

printLevelOrder(root);

return 0;

}
3) find next node in same level for given node in a binary tree (C++ language):

#include <iostream>

#include <list>

using namespace std;

struct Node

int data;

Node *left, *right;

Node(int data)

this->data = data;

this->left = this->right = nullptr;

};

Node* findRightNode(Node* root, Node* node)

if (root == nullptr) {

return nullptr;

list<Node*> queue;

queue.push_back(root);

Node* front = nullptr;


while (!queue.empty())

int size = queue.size();

while (size--)

front = queue.front();

queue.pop_front();

if (front == node)

if (size == 0) {

return nullptr;

return queue.front();

if (front->left) {

queue.push_back(front->left);

if (front->right) {

queue.push_back(front->right);

return nullptr;

}
int main()

Node* root = new Node(1);

root->left = new Node(2);

root->right = new Node(3);

root->left->left = new Node(4);

root->left->right = new Node(5);

root->right->left = new Node(6);

root->right->left->left = new Node(7);

root->right->left->right = new Node(8);

Node* right = findRightNode(root, root->left->right);

if (right) {

cout << "Right node is " << right->data;

else {

cout << "Right node doesn't exist";

return 0;

4) Determine if a given binary tree is a sub tree of another binary tree or not (C language):

#include <stdbool.h>
#include <stdio.h>

#include <stdlib.h>

struct node {

int data;

struct node* left;

struct node* right;

};

bool areIdentical(struct node* root1, struct node* root2)

if (root1 == NULL && root2 == NULL)

return true;

if (root1 == NULL || root2 == NULL)

return false;

return (root1->data == root2->data

&& areIdentical(root1->left, root2->left)

&& areIdentical(root1->right, root2->right));

bool isSubtree(struct node* T, struct node* S)

if (S == NULL)

return true;

if (T == NULL)

return false;

if (areIdentical(T, S))
return true;

return isSubtree(T->left, S) || isSubtree(T->right, S);

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* T = newNode(26);

T->right = newNode(3);

T->right->right = newNode(3);

T->left = newNode(10);

T->left->left = newNode(4);

T->left->left->right = newNode(30);

T->left->right = newNode(6);

struct node* S = newNode(10);

S->right = newNode(6);

S->left = newNode(4);

S->left->right = newNode(30);
if (isSubtree(T, S))

printf("Tree 2 is subtree of Tree 1");

else

printf("Tree 2 is not a subtree of Tree 1");

getchar();

return 0;

5) Check if given binary tree has symmetric structure or not (C++ language):

#include<bits/stdc++.h>

using namespace std;

// A Binary Tree Node

struct Node

int key;

struct Node* left, *right;

};

// Utility function to create new Node

Node *newNode(int key)

Node *temp = new Node;

temp->key = key;

temp->left = temp->right = NULL;


return (temp);

// Returns true if a tree is symmetric

// i.e. mirror image of itself

bool isSymmetric(struct Node* root)

if(root == NULL)

return true;

// If it is a single tree node, then

// it is a symmetric tree.

if(!root->left && !root->right)

return true;

queue <Node*> q;

// Add root to queue two times so that

// it can be checked if either one child

// alone is NULL or not.

q.push(root);

q.push(root);

// To store two nodes for checking their

// symmetry.

Node* leftNode, *rightNode;

while(!q.empty()){

// Remove first two nodes to check

// their symmetry.
leftNode = q.front();

q.pop();

rightNode = q.front();

q.pop();

// if both left and right nodes

// exist, but have different

// values--> inequality, return false

if(leftNode->key != rightNode->key){

return false;

// Push left child of left subtree node

// and right child of right subtree

// node in queue.

if(leftNode->left && rightNode->right){

q.push(leftNode->left);

q.push(rightNode->right);

// If only one child is present alone

// and other is NULL, then tree

// is not symmetric.

else if (leftNode->left || rightNode->right)

return false;

// Push right child of left subtree node

// and left child of right subtree node

// in queue.

if(leftNode->right && rightNode->left){


q.push(leftNode->right);

q.push(rightNode->left);

// If only one child is present alone

// and other is NULL, then tree

// is not symmetric.

else if(leftNode->right || rightNode->left)

return false;

return true;

// Driver program

int main()

// Let us construct the Tree shown in

// the above figure

Node *root = newNode(1);

root->left = newNode(2);

root->right = newNode(2);

root->left->left = newNode(3);

root->left->right = newNode(4);

root->right->left = newNode(4);

root->right->right = newNode(3);

if(isSymmetric(root))

cout << "The given tree is Symmetric";

else

cout << "The given tree is not Symmetric";


return 0;

6) Convert binary tree to its mirror(C language):

#include <stdio.h>

#include <stdlib.h>

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);

}
void mirror(struct Node* node)

if (node == NULL)

return;

else {

struct Node* temp;

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);

printf("%d ", 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);

printf("Inorder traversal of the constructed"

" tree is \n");

inOrder(root);

mirror(root);

printf("\nInorder traversal of the mirror tree"

" is \n");

inOrder(root);

return 0;

7) Check if binary tree can be converted to another by doing any no. of swaps of left and right child
(C++ language):

#include <iostream>

using namespace std;

struct Node

int data;

Node *left, *right;


Node(int data)

this->data = data;

this->left = this->right = nullptr;

};

bool equal(Node* X, Node* Y)

if (X == Y) {

return true;

return (X && Y) && (X->data == Y->data) &&

((equal(X->left, Y->left) && equal(X->right, Y->right)) ||

(equal(X->right, Y->left) && equal(X->left, Y->right)));

int main()

Node* X = nullptr;

X = new Node(6);

X->left = new Node(3);

X->right = new Node(8);

X->left->left = new Node(1);

X->left->right = new Node(7);

X->right->left = new Node(4);

X->right->right = new Node(2);


X->right->left->left = new Node(1);

X->right->left->right = new Node(7);

X->right->right->right = new Node(3);

Node* Y = nullptr;

Y = new Node(6);

Y->left = new Node(8);

Y->right = new Node(3);

Y->left->left = new Node(2);

Y->left->right = new Node(4);

Y->right->left = new Node(7);

Y->right->right = new Node(1);

Y->left->left->left = new Node(3);

Y->left->right->left = new Node(1);

Y->left->right->right = new Node(7);

if (equal(X, Y)) {

cout << "Binary tree can be converted";

else {

cout << "Binary tree cannot be converted";

return 0;

}
8) Find lowest common ancestor of two nodes in a binary tree (C++ language):
#include <bits/stdc++.h>

using namespace std;

// A Binary Tree node

struct Node {

int key;

struct Node *left, *right;

};

Node* newNode(int k)

Node* temp = new Node;

temp->key = k;

temp->left = temp->right = NULL;

return temp;

bool findPath(Node* root, vector<int>& path, int k)

// base case

if (root == NULL)

return false;

path.push_back(root->key);

if (root->key == k)

return true;
if ((root->left && findPath(root->left, path, k))

|| (root->right && findPath(root->right, path, k)))

return true;

path.pop_back();

return false;

int findLCA(Node* root, int n1, int n2)

vector<int> path1, path2;

if (!findPath(root, path1, n1)

|| !findPath(root, path2, n2))

return -1;

int i;

for (i = 0; i < path1.size() && i < path2.size(); i++)

if (path1[i] != path2[i])

break;

return path1[i - 1];

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);

cout << "LCA(4, 5) = " << findLCA(root, 4, 5);

cout << "\nLCA(4, 6) = " << findLCA(root, 4, 6);

cout << "\nLCA(3, 4) = " << findLCA(root, 3, 4);

cout << "\nLCA(2, 4) = " << findLCA(root, 2, 4);

return 0;

Part 2:

You might also like