0% found this document useful (0 votes)
2 views5 pages

dslab__8

The document contains a C program that implements a binary search tree (BST) with functionalities to insert, delete, and traverse nodes in in-order and pre-order. It includes a function to find elements within a specified range. The main function demonstrates these operations by inserting nodes, deleting a node, and displaying the tree's traversal results.

Uploaded by

chetanyadav25678
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)
2 views5 pages

dslab__8

The document contains a C program that implements a binary search tree (BST) with functionalities to insert, delete, and traverse nodes in in-order and pre-order. It includes a function to find elements within a specified range. The main function demonstrates these operations by inserting nodes, deleting a node, and displaying the tree's traversal results.

Uploaded by

chetanyadav25678
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/ 5

printf(“Name-Chetan Yadav”);

printf(“Roll No. – 23BCS069”);

#include <stdio.h>

#include <stdlib.h>

struct Node

int data;

struct Node *left, *right;

};

struct Node *createNode(int key)

struct Node *node = (struct Node *)malloc(sizeof(struct Node));

node->data = key;

node->left = node->right = NULL;

return node;

struct Node *insert(struct Node *root, int key)

if (root == NULL)

return createNode(key);

if (key < root->data)

root->left = insert(root->left, key);

else if (key > root->data)

root->right = insert(root->right, key);

return root;

}
void inOrder(struct Node *root)

if (root != NULL)

inOrder(root->left);

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

inOrder(root->right);

void preOrder(struct Node *root)

if (root != NULL)

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

preOrder(root->left);

preOrder(root->right);

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->data)

root->left = deleteNode(root->left, key);

else if (key > root->data)

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->data = temp->data;

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

return root;

void range(struct Node *root, int min, int max)

if (root != NULL)
{

range(root->left, min, max);

if (root->data >= min && root->data <= max)

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

range(root->right, min, max);

int main()

struct Node *root = NULL;

// root = insert(root,25);

// insert(root,20);

// insert(root,30);

// insert(root,22);

// insert(root,9);

// insert(root,2);

// insert(root,11);

// insert(root,27);

// insert(root,35);

// insert(root,33);

// insert(root,45);

// insert(root,50);

// insert(root,40);

// printf("InOrder Traversal:");

// inOrder(root);

// printf("\nPreOrder Traversal :");

// preOrder(root);
// deleteNode(root,35);

root = insert(root, 7);

insert(root, 4);

insert(root, 12);

insert(root, 3);

insert(root, 6);

insert(root, 8);

insert(root, 1);

insert(root, 5);

insert(root, 10);

printf("InOrder Traversal:");

inOrder(root);

printf("\nPreOrder Traversal :");

preOrder(root);

deleteNode(root, 12);

printf("\nInOrder Traversal:");

inOrder(root);

printf("\nPreOrder Traversal :");

preOrder(root);

printf("\nElements between min and max is: ");

range(root, 4, 10);

You might also like