0% found this document useful (0 votes)
65 views8 pages

Binary Search Tree (BST)

This document defines functions to implement operations on a binary search tree (BST) including insertion, deletion, and traversal. It defines a node structure with left and right pointers and functions to create nodes, insert keys, find the minimum value node, delete nodes, and traverse the tree in-order, pre-order, and post-order. It also includes a main function that builds a sample BST and demonstrates the deletion and traversal functions.

Uploaded by

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

Binary Search Tree (BST)

This document defines functions to implement operations on a binary search tree (BST) including insertion, deletion, and traversal. It defines a node structure with left and right pointers and functions to create nodes, insert keys, find the minimum value node, delete nodes, and traverse the tree in-order, pre-order, and post-order. It also includes a main function that builds a sample BST and demonstrates the deletion and traversal functions.

Uploaded by

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

Binary Search Tree (BST)

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 PrintTree(struct node *root){
if (root == NULL) return;
printf("%d ", root->key);
PrintTree(root->left);
PrintTree(root->right);
}

void inordertraversal(struct node *root){


if (root == NULL) return;

inordertraversal(root->left);
printf("\n%d ", root->key);
inordertraversal(root->right);
}
void preordertraversal(struct node *root){
if (root == NULL) return;
printf("%d ", root->key);
preordertraversal(root->left);
preordertraversal(root->right);
}

void postordertraversal(struct node *root){


if (root == NULL) return;
postordertraversal(root->left);
postordertraversal(root->right);
printf("%d ", root->key);
}
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){

//case 1: No Child
if (root == NULL) return root; // if only left child present
// find left or right
if (key < root->key) else if (root->right == NULL){
root->left = deleteNode(root->left, key); struct node *temp = root->left;
else if (key > root->key) free(root);
root->right = deleteNode(root->right, key); return temp;
}
//case 2:
else{ // if two child present
// if only right child present struct node* temp = minValueNode(root->right);
if (root->left == NULL){ root->key = temp->key;
struct node *temp = root->right; root->right = deleteNode(root->right, temp->key);
free(root); }
return temp; return root;
} }
int main(){
struct node *root = NULL;
root = insert(root, 50);
root = insert(root, 30);
root = insert(root, 20);
root = insert(root, 40);
root = insert(root, 70); root = deleteNode(root, 20);
root = insert(root, 60); printf("Inorder traversal of the modified tree \n");
root = insert(root, 80); inordertraversal(root);
printf("Printing Tree\n"); printf("\nDelete 30\n");
PrintTree(root); root = deleteNode(root, 30);
printf("\n\nInorder traversal of the given tree \n"); printf("Inorder traversal of the modified tree \n");
inordertraversal(root); inordertraversal(root);
printf("\npreorder traversal of the given tree \n"); printf("\nDelete 50\n");
preordertraversal(root); root = deleteNode(root, 50);
printf("\npostorder traversal of the given tree \n"); printf("Inorder traversal of the modified tree \n");
postordertraversal(root); inordertraversal(root);
printf("\nDelete 20\n"); return 0;
}

You might also like