0% found this document useful (0 votes)
11 views8 pages

RA2011047010113 - Lab Assignmnet UNIT 3

The document discusses implementing level order traversal and lowest common ancestor in binary trees. It provides algorithms and code to print the level order traversal of a binary tree using a queue. It also provides an algorithm and code to find the lowest common ancestor of two nodes in a binary tree by finding paths from the root to each node and returning the first mismatch. The document also provides an algorithm and code to construct a balanced binary search tree from a sorted array by recursively taking the middle element as the root.

Uploaded by

gecene
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views8 pages

RA2011047010113 - Lab Assignmnet UNIT 3

The document discusses implementing level order traversal and lowest common ancestor in binary trees. It provides algorithms and code to print the level order traversal of a binary tree using a queue. It also provides an algorithm and code to find the lowest common ancestor of two nodes in a binary tree by finding paths from the root to each node and returning the first mismatch. The document also provides an algorithm and code to construct a balanced binary search tree from a sorted array by recursively taking the middle element as the root.

Uploaded by

gecene
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Q1- Write a program to implement level order

traversal of a binary tree

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

struct node** createQueue(int*, int*);


void enQueue(struct node**, int*, struct node*);
struct node* deQueue(struct node**, int*);

void printLevelOrder(struct node* root)


{
int rear, front;
struct node** queue = createQueue(&front, &rear);
struct node* temp_node = root;
while (temp_node) {
printf("%d ", temp_node->data);

if (temp_node->left)
enQueue(queue, &rear, temp_node->left);

if (temp_node->right)
enQueue(queue, &rear, temp_node->right);

temp_node = deQueue(queue, &front);


}
}

struct node** createQueue(int* front, int* rear)


{
struct node** queue = (struct node**)malloc(
sizeof(struct node*) * MAX_Q_SIZE);

*front = *rear = 0;
return queue;
}

void enQueue(struct node** queue, int* rear,


struct node* new_node)
{
queue[*rear] = new_node;
(*rear)++;
}

struct node* deQueue(struct node** queue, int* front)


{
(*front)++;
return queue[*front - 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;
}

Input-
Tree is constructed with 1,2,3,4,5

Output-
Level Order traversal of binary tree is
12345
Executed screenshot -

Q2-Write a program to implement Lowest


Common Ancestor in the binary tree

AIM: To print the lowest common ancestor in the binary tree.

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

Sample Output: LCA(4, 5) = 2nLCA(4, 6) = 1nLCA(3, 4) =


1nLCA(2, 4) = 2

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 -

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy