RA2011047010113 - Lab Assignmnet UNIT 3
RA2011047010113 - Lab Assignmnet UNIT 3
Aim -
: To print the level order traversal of a binary tree.
Algorithm/Procedure-
printLevelorder(tree)
1) Create an empty queue q
2) temp_node = root /*start from root*/
3) Loop while temp_node is not NULL
a) print temp_node->data.
b) Enqueue temp_node’s children
(first left then right children) to q
c) Dequeue a node from q.
Program -
#include <stdio.h>
#include <stdlib.h>
#define MAX_Q_SIZE 500
struct node {
int data;
struct node* left;
struct node* right;
};
if (temp_node->left)
enQueue(queue, &rear, temp_node->left);
if (temp_node->right)
enQueue(queue, &rear, temp_node->right);
*front = *rear = 0;
return queue;
}
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);
return 0;
}
Input-
Tree is constructed with 1,2,3,4,5
Output-
Level Order traversal of binary tree is
12345
Executed screenshot -
Procedure:
1.Find a path from the root to n1 and store it in a vector or array.
2. Find a path from the root to n2 and store it in another vector or array.
3. Traverse both paths till the values in arrays are the same. Return the common element just
before the mismatch.
Program:
#include <iostream>
#include <vector>
using namespace std;
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)
{
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;
}
Executed screenshot -
Q3.Write a program to construct balanced tree.
Aim -
To construct balanced tree and print preorder traversal of the bst.
Procedure -
1.Get the Middle of the array and make it root. 2) Recursively do same for left half and right half.
a) Get the middle of left half and make it left child of the root created in step 1. b) Get the middle
of right half and make it right child of the root created in step 1.
Program -
#include<stdio.h>
#include<stdlib.h>
struct TNode
{
int data;
struct TNode* left;
struct TNode* right;
};
struct TNode* newNode(int data);
struct TNode* sortedArrayToBST(int arr[], int start, int end)
{
if (start > end)
return NULL;
int mid = (start + end)/2;
struct TNode *root = newNode(arr[mid]);
root->left = sortedArrayToBST(arr, start, mid-1);
root->right = sortedArrayToBST(arr, mid+1, end);
return root;}
struct TNode* newNode(int data){
struct TNode* node = (struct TNode*)
malloc(sizeof(struct TNode));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;}
void preOrder(struct TNode* node){
if (node == NULL)
return;
printf("%d ", node->data);
preOrder(node->left);
preOrder(node->right);}
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7};
int n = sizeof(arr)/sizeof(arr[0]);
struct TNode *root = sortedArrayToBST(arr, 0, n-1);
printf("PreOrder Traversal of constructed BST:\n ");
preOrder(root);
return 0;
}
Sample Output -
Preorder traversal of constructed BST
4213657
Executed screenshot -