0% found this document useful (0 votes)
31 views6 pages

Ex No 7

The document describes a program to implement binary search and binary tree traversal. It includes code for a binary search function that takes an array, size, and target value, and returns the index of the target if found. It also includes code for creating and traversing a binary tree using inorder, preorder, and postorder functions. The program was run and output the correct results, verifying that binary search and binary tree traversal were implemented.

Uploaded by

HARIKRISHNAN R
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)
31 views6 pages

Ex No 7

The document describes a program to implement binary search and binary tree traversal. It includes code for a binary search function that takes an array, size, and target value, and returns the index of the target if found. It also includes code for creating and traversing a binary tree using inorder, preorder, and postorder functions. The program was run and output the correct results, verifying that binary search and binary tree traversal were implemented.

Uploaded by

HARIKRISHNAN R
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/ 6

EX NO :7

BINARY SEARCH
DATE

AIM
To implement the program to demonstrate binary search

BINARY SEARCH

#include <stdio.h>
int binary_search(int arr[], int size, int target) {
int left = 0, right = size - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return 0;
}
int main() {
int array[21] = {1, 2, 3, 5, 7, 9, 12, 14, 16, 18, 21, 23, 25, 27, 29, 31, 34, 36, 38, 40, 42};
int loop;
for(loop = 0; loop < 21; loop++){
printf("%d", array[loop]);
}
int target;
printf("\n");
printf("enter the target element");
scanf("%d",&target);

71762308202
int size = sizeof(array) / sizeof(array[0]);
int result = binary_search(array, size, target);
if (result != -1) {
printf("the element %d found at index %d.\n", target, result);
} else {
printf("the element %d not found in the given array.\n", target);
}
return 0;
}

OUTPUT

RESULT
Hence the binary search code is implemented and verified

71762308202
EX NO :8
Binary Tree Traversal
DATE

Aim
To implement the Inorder Postorder and Preorder Traversal of binary tree
Program
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *lchild, *rchild;
} node;
node* createNode(node *parent, int data) {
node* newNode = (node*) malloc(sizeof(node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
exit(EXIT_FAILURE);
}
newNode->data = data;
newNode->lchild = NULL;
newNode->rchild = NULL;
if(parent == NULL) {
return newNode;
}
else if(data < parent->data) {
parent->lchild = createNode(parent->lchild, data);
}
else if(data > parent->data) {
parent->rchild = createNode(parent->rchild, data);
}
return parent;
}
void inOrder(node *root) {

71762308202
if(root != NULL) {
inOrder(root->lchild);
printf("%d | ", root->data);
inOrder(root->rchild);
}
}
void preOrder(node *root) {
if(root != NULL) {
printf("%d | ", root->data);
preOrder(root->lchild);
preOrder(root->rchild);
}
}
void postOrder(node *root) {
if(root != NULL) {
postOrder(root->lchild);
postOrder(root->rchild);
printf("%d | ", root->data);
}
}
void freeTree(node* root) {
if(root != NULL) {
freeTree(root->lchild);
freeTree(root->rchild);
free(root);
}
}
int main() {
node *root = NULL;
int scn, data;
while (1) {
printf("1. Add Node\n");
printf("2. Inorder Traversal\n");
printf("3. Preorder Traversal\n");

71762308202
printf("4. Postorder Traversal\n");
printf("0. Exit\n");
printf("Enter your Choice: ");
scanf("%d", &scn);
switch(scn) {
case 0:
freeTree(root);
exit(EXIT_SUCCESS);
break;
case 1:
printf("Enter Data: ");
scanf("%d", &data);
root = createNode(root, data);
printf("Node is added .\n");
break;
case 2:
printf("Inorder Traversal: ");
inOrder(root);
printf("\n");
break;
case 3:
printf("Preorder Traversal: ");
preOrder(root);
printf("\n");
break;
case 4:
printf("Postorder Traversal: ");
postOrder(root);
printf("\n");
break;
default:
printf("Invalid Choice. Try again.\n");
break;
}

71762308202
}
return 0;
}
OUTPUT

RESULT:
Thus the program to implement the Binary Tree Traversal is created and verified

71762308202

You might also like