0% found this document useful (0 votes)
18 views10 pages

DS Exp7

Uploaded by

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

DS Exp7

Uploaded by

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

EXPT 7 BINARY SEARCH TREE DATE:21/08/24

Aim:Write a C program to create a binary search tree and to perform insert,delete,se


arch and to find maximum and minimum number.

Theory:
One of the important uses of binary trees is in searching.
Binary Search Trees are binary trees that are specially organized for the
purpose of searching.
An element can be searched in average O(log n) times where N is the number
of nodes.
In binary search tree, a key is associated with each node.

We can define binary search trees recursively as-


A binary search tree is a binary tree that may be empty and if it is not empty then it s
atisfies the following properties-
1) All the keys in the left subtree of root are less than the key in the root.
2) All the keys in the right subtree of root are greater than the key in the root.
3) Left and right subtrees of root are also binary search trees.
• We have assumed that all the keys of binary search tree are distinct, although
there can be binary search tree with duplicates and in that case the
operations explained have to be changed.

PR:202311449
Program:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct tree{
int info;
struct tree *lchild;
struct tree *rchild;
};
struct tree *search(struct tree *ptr,int data){
if(ptr==NULL){
printf("Not found");
return NULL;
}
else if(data<ptr->info)
return search(ptr->lchild,data);
else if(data>ptr->info)
return search(ptr->rchild,data);
else
return ptr;
}
struct tree *insert(struct tree *ptr,int key){
if(ptr==NULL){
ptr=(struct tree *)malloc(sizeof(struct tree));
ptr->info=key;
ptr->lchild=ptr->rchild=NULL;
}
else if(key<ptr->info)
ptr->lchild=insert(ptr->lchild,key);
else if(key>ptr->info)
ptr->rchild=insert(ptr->rchild,key);
else
printf("Duplicate\n");
return ptr;
}
struct tree *min_rec(struct tree *ptr){
if(ptr==NULL)
return ptr;
if(ptr->lchild==NULL)
return ptr;
else
return min_rec(ptr->lchild);
}
struct tree *max_rec(struct tree *ptr){

PR:202311449
if(ptr==NULL)
return ptr;
if(ptr->rchild==NULL)
return ptr;
else
return max_rec(ptr->rchild);
}
struct tree *del(struct tree *root, int key) {
if (root == NULL) {
return root;
}
if (key < root->info) {
root->lchild = del (root->lchild, key);
} else if (key > root->info) {
root->rchild = del (root->rchild, key);
} else {
if (root->lchild == NULL) {
struct tree *temp = root->rchild;
free(root);
return temp;
} else if (root->rchild == NULL) {
struct tree *temp = root->lchild;
free(root);
return temp;
}
struct tree *successor = root->rchild;
while (successor->lchild != NULL) {
successor = successor->lchild;
}
root->info = successor->info;
root->rchild = del (root->rchild, successor->info);
}
return root;
}
void display(struct tree *ptr,int level){
if(ptr==NULL)
return;
if(level==1)
printf("%d ",ptr->info);
else if(level>1){
display(ptr->lchild,level-1);
display(ptr->rchild,level-1);
}
}

PR:202311449
void preorder(struct tree *root){
if(root!=NULL){
printf("%d ",root->info);
preorder(root->lchild);
preorder(root->rchild);
}
}
void inorder(struct tree *root){
if(root!=NULL){
inorder(root->lchild);
printf("%d ", root->info);
inorder(root->rchild);
}
}
void postorder(struct tree *root){
if(root != NULL){
postorder(root->lchild);
postorder(root->rchild);
printf("%d ", root->info);
}
}
int height(struct tree *ptr) {
if (ptr == NULL) return 0;
int h_left = height(ptr->lchild);
int h_right = height(ptr->rchild);
return (h_left > h_right ? h_left : h_right) + 1;
}
void levelOrder(struct tree *ptr){
int i,h=height(ptr),n;
for(i=1;i<=h;i++)
display(ptr,i);
}
void printStructuredTree(int arr[], int size, int levels) {
int n = 0;
for (int level = 0; level <= levels; level++) {
int nodesInLevel = pow(2, level);
int spaceBetweenNodes = pow(2, levels - level + 1) - 1;
int initialSpace = pow(2, levels - level) - 1;
for (int i = 0; i < initialSpace; i++)
printf(" ");
for (int i = 0; i < nodesInLevel && n < size; i++) {
if (arr[n] != -1) {
printf("%d", arr[n]);

PR:202311449
if (i < nodesInLevel - 1) {
for (int j = 0; j < spaceBetweenNodes; j++)
printf(" ");
}
} else {
printf(" ");
if (i < nodesInLevel - 1) {
for (int j = 0; j < spaceBetweenNodes; j++)
printf(" ");
}
}
n++;
}
printf("\n");
}
}
int *createArray(struct tree *root, int *ptr, int i) {
if (root == NULL) {
ptr[i] = -1;
} else {
ptr[i] = root->info;
createArray(root->lchild, ptr, i * 2 + 1);
createArray(root->rchild, ptr, i * 2 + 2);
}
return ptr;
}
int main(){
struct tree *root=NULL;
int numbers[] = {41, 70, 58, 49, 25, 12, 44, 20, 68, 91, 57, 42, 37, 28, 27};
for(int i=0;i<15;i++){
printf("Insert(%d)\n",numbers[i]);
root = insert(root, numbers[i]);
int maxNodes = pow(2, height(root) + 1) - 1;
int *arr = (int *)malloc(maxNodes * sizeof(int));
for (int i = 0; i < maxNodes; i++)
arr[i] = -1;
createArray(root, arr, 0);
printStructuredTree(arr, maxNodes, height(root));
printf("\n");
}
printf("Height:%d",height(root));
printf("\nLevel Order:");
levelOrder(root);
printf("\nPreorder:");

PR:202311449
preorder(root);
printf("\nPostorder:");
postorder(root);
printf("\nInorder:");
inorder(root);
struct tree *min=min_rec(root);
printf("\nThe minimum number is:%d ",min->info);
struct tree *max=max_rec(root);
printf("\nThe maximum number is:%d ",max->info);
for(int i=0;i<15;i++){
printf("Delete(%d)\n",numbers[i]);
root = del(root, numbers[i]);
int maxNodes = pow(2, height(root) + 1) - 1;
int *arr = (int *)malloc(maxNodes * sizeof(int));
for (int i = 0; i < maxNodes; i++)
arr[i] = -1;
createArray(root, arr, 0);
printStructuredTree(arr, maxNodes, height(root));
printf("\n");
}
printf(“NULL”);
}

PR:202311449
Output:

PR:202311449
PR:202311449
PR:202311449
Conclusion:The provided code implements a basic binary search tree (BST) in C wi
th functions for inserting, searching, deleting, and traversing the tree in various order
s (preorder, inorder, postorder, and level order). Additionally, it includes functionality
to find the minimum and maximum values in the tree, as well as to delete a node and
then display the updated tree structure.The overall program demonstrates efficient tr
ee operations and provides comprehensive tree manipulation functions, showing ho
w trees can be managed and traversed systematically in C.

PR:202311449

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