0% found this document useful (0 votes)
5 views3 pages

Binary Tree 59

The document presents an assignment on Binary Search Trees by Sanket Bhimapa Koli, including a C program that implements the creation, insertion, deletion, and inorder traversal of a binary search tree. It defines a structure for the tree nodes and includes functions for inserting and deleting nodes, as well as displaying the tree's contents in order. The output demonstrates the tree's state before and after the deletion of a node.

Uploaded by

anishwarushe0725
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)
5 views3 pages

Binary Tree 59

The document presents an assignment on Binary Search Trees by Sanket Bhimapa Koli, including a C program that implements the creation, insertion, deletion, and inorder traversal of a binary search tree. It defines a structure for the tree nodes and includes functions for inserting and deleting nodes, as well as displaying the tree's contents in order. The output demonstrates the tree's state before and after the deletion of a node.

Uploaded by

anishwarushe0725
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/ 3

ASSIGNMENT NO 8

(BINARY SEARCH TREE)

NAME - SANKET BHIMAPA KOLI

ROLL NO-59

PRN - 2122000732

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

struct node {
int key;
struct node *left, *right;
};

struct node *newNode(int item) {


struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}

void inorder(struct node *root) {


if (root != NULL) {

inorder(root->left);

printf("%d -> ", root->key);

inorder(root->right);
}
}

struct node *insert(struct node *node, int key) {

if (node == NULL) return newNode(key);

if (key < node->key)


node->left = insert(node->left, key);
else
node->right = insert(node->right, key);

return node;
}

struct node *minValueNode(struct node *node) {


struct node *current = node;
while (current && current->left != NULL)
current = current->left;
return current;
}

struct node *deleteNode(struct node *root, int key) {


if (root == NULL) return root;

if (key < root->key)


root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);

else {
if (root->left == NULL) {
struct node *temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct node *temp = root->left;
free(root);
return temp;
}
struct node *temp = minValueNode(root->right);

root->key = temp->key;

root->right = deleteNode(root->right, temp->key);


}
return root;
}

int main() {
struct node *root = NULL;
root = insert(root, 8);
root = insert(root, 3);
root = insert(root, 1);
root = insert(root, 6);
root = insert(root, 7);
root = insert(root, 10);
root = insert(root, 14);
root = insert(root, 4);

printf("Inorder traversal: ");


inorder(root);

printf("\nAfter deleting 10\n");


root = deleteNode(root, 10);
printf("Inorder traversal: ");
inorder(root);
}

OUTOUT-
Inorder traversal: 1 -> 3 -> 4 -> 6 -> 7 -> 8 -> 10 -> 14
After deleting 10
Inorder traversal: 1 -> 3 -> 4 -> 6 -> 7 -> 8 -> 14

You might also like