DS Exp7
DS Exp7
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.
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