0% found this document useful (0 votes)
15 views4 pages

Notes 20220620182551

The document contains a C program that implements a binary search tree with functionalities such as insertion, searching for a key, counting leaf nodes, calculating the height of the tree, counting right nodes, inorder traversal, and deleting a node. It includes a menu-driven interface for user interaction to perform these operations. The program uses dynamic memory allocation for tree nodes and provides error handling for memory allocation failures.

Uploaded by

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

Notes 20220620182551

The document contains a C program that implements a binary search tree with functionalities such as insertion, searching for a key, counting leaf nodes, calculating the height of the tree, counting right nodes, inorder traversal, and deleting a node. It includes a menu-driven interface for user interaction to perform these operations. The program uses dynamic memory allocation for tree nodes and provides error handling for memory allocation failures.

Uploaded by

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

06.

20 18:25
/******************************************************************************

Welcome to GDB Online.


GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <stdio.h>
#include<stdlib.h>

// IQRA HASSAN KHAN 25 ECE


typedef struct tree
{
struct tree *left;
int info;
struct tree *right;

}treetype;

treetype*create(int num)
{
treetype *p=NULL;
p=(treetype*)malloc(sizeof(treetype));
if(p==NULL)
printf("not enough memory\n");
else
{
p->info=num;
p->left=NULL;
p->right=NULL;
}
return p;
}

void insert(treetype**root,treetype*temp)
{
if((temp)->info<(*root)->info)
{
if((*root)->left==NULL)
(*root)->left=temp;
else
insert(&(*root)->left,temp);

}
else
{
if((*root)->right==NULL)
(*root)->right=temp;
else
insert(&(*root)->right,temp);

}
}
//inorder traversal
void inorder(treetype**root)
{
if((*root)!=NULL)
{
inorder(&(*root)->left);
printf("%d",(*root)->info);
inorder(&(*root)->right);
}
}
// right count
int rightNode(treetype *root){
if(root!=NULL)
{
return 1+rightNode(root->right);

}
}

//leaf count
int countleaf(treetype *root){
if(root==NULL)
return 0;
else if(root->right==NULL && root->left==NULL)
return 1;
else

return countleaf(root->left)+rightNode(root->right);
}
//search key

void search(treetype *root,int key)


{
if(root==NULL)
return ;
else if(root->info==key)
printf("key found\n");
else if(key<root->info)
search(root->left,key);
else
search(root->right,key);
}

//height of tree
int heightnode(treetype *root)
{
if (root == NULL)
return 0;
else {
/* compute the depth of each subtree */
int lDepth = heightnode(root->left);
int rDepth = heightnode(root->right);

/* use the larger one */


if (lDepth > rDepth)
return (lDepth + 1);
else
return (rDepth + 1);
}
}
//delete node
treetype* minValueNode(treetype* node)
{
treetype* current = node;

/* loop down to find the leftmost leaf */


while (current && current->left != NULL)
current = current->left;

return current;
}

/* Given a binary search tree


and a key, this function
deletes the key and
returns the new root */
treetype* deleteNode(treetype* root, int key)
{
// base case
if (root == NULL)
return root;

// If the key to be deleted


// is smaller than the root's
// key, then it lies in left subtree
if (key < root->info)
root->left = deleteNode(root->left, key);

// If the key to be deleted


// is greater than the root's
// key, then it lies in right subtree
else if (key > root->info)
root->right = deleteNode(root->right, key);

// if key is same as root's key,


// then This is the node
// to be deleted
else {
// node with only one child or no child
if (root->left == NULL) {
treetype* temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL) {
treetype* temp = root->left;
free(root);
return temp;
}

// node with two children:


// Get the inorder successor
// (smallest in the right subtree)
treetype* temp = minValueNode(root->right);

// Copy the inorder


// successor's content to this node
root->info = temp->info;

// Delete the inorder successor


root->right = deleteNode(root->right, temp->info);
}
return root;
}

void main()
{
int num,c=0,h=0,r=0,choice,key;
treetype*root=NULL,*temp;
int ch;
do
{
printf("\n menu \n");
printf("1-create 2-search a key 3-total leaf nodes 4-height of tree 5-right
tree count 6-inorder traversal 7-delete node\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("enter element to be entered\n");
scanf("%d",&num);
temp=create(num);
if(root==NULL)
root=temp;
else
insert(&root,temp);
break;
case 2: printf("enter a key\n");
scanf("%d",&key);
search(root,key);
break;
case 3: c=countleaf(root);
printf("total leaf nodes are %d\n",c);
break;
case 4: h=heightnode(root);
printf("height of tree is %d\n",h);
break;
case 5: r=rightNode(root);
printf("right node count is%d\n",r);
break;
case 6: printf("inorder traversal is\n");
inorder(&root);
break;
case 7:printf("enter key to be delted\n");
scanf("%d",&key);
root=deleteNode(root,key);
break;

}
printf("PRESS 0 TO EXIT\n");
scanf("%d",&ch);
}while(ch!=0);

You might also like