0% found this document useful (0 votes)
20 views

Binary Search Tree

This document contains code for binary search tree operations like insertion, traversal, finding height, leaf nodes, mirror image, and counting nodes. It defines a Node structure with left, right and data pointers, and includes functions to create and insert nodes, perform inorder, preorder and postorder traversals, find the tree height, display leaf nodes, mirror the tree, and count total nodes. Main inserts sample nodes and tests each operation.

Uploaded by

om.lohade23
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Binary Search Tree

This document contains code for binary search tree operations like insertion, traversal, finding height, leaf nodes, mirror image, and counting nodes. It defines a Node structure with left, right and data pointers, and includes functions to create and insert nodes, perform inorder, preorder and postorder traversals, find the tree height, display leaf nodes, mirror the tree, and count total nodes. Main inserts sample nodes and tests each operation.

Uploaded by

om.lohade23
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Title : Binary Search Tree

Name : Lohade Om Manoj


Roll No : 3 Seda IT C

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

struct Node {
int data;
struct Node* left;
struct Node* right;
};

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}

struct Node* insert(struct Node* root, int data) {


if (root == NULL) {
return createNode(data);
}

if (data < root->data) {


root->left = insert(root->left, data);
} else if (data > root->data) {
root->right = insert(root->right, data);
}

return root;
}

void inorderTraversal(struct Node* root) {


if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}

void preorderTraversal(struct Node* root) {


if (root == NULL) return;
printf("%d ", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
void postorderTraversal(struct Node* root) {
if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->data);
}

struct Node* mirrorImage(struct Node* root) {


if (root == NULL) return root;

struct Node* temp = root->left;


root->left = mirrorImage(root->right);
root->right = mirrorImage(temp);

return root;
}

int height(struct Node* root) {


if (root == NULL) return 0;
int leftHeight = height(root->left);
int rightHeight = height(root->right);
return 1 + (leftHeight > rightHeight ? leftHeight : rightHeight);
}

void displayLeafNodes(struct Node* root) {


if (root == NULL) return;
if (root->left == NULL && root->right == NULL) {
printf("%d ", root->data);
}
displayLeafNodes(root->left);
displayLeafNodes(root->right);
}

int countNodes(struct Node* root) {


if (root == NULL) return 0;
return 1 + countNodes(root->left) + countNodes(root->right);
}

int main() {
struct Node* root = NULL;

root = insert(root, 5);


root = insert(root, 9);
root = insert(root, 25);
root = insert(root, 39);
root = insert(root, 49);
root = insert(root, 21);
root = insert(root, 31);

printf("Inorder Traversal: ");


inorderTraversal(root);
printf("\n");

printf("Preorder Traversal: ");


preorderTraversal(root);
printf("\n");

printf("Postorder Traversal: ");


postorderTraversal(root);
printf("\n");

printf("Mirror Image: ");


root = mirrorImage(root);
inorderTraversal(root);
printf("\n");

printf("Height of the BST: %d\n", height(root));

printf("Leaf Nodes: ");


displayLeafNodes(root);
printf("\n");

printf("Number of Nodes: %d\n", countNodes(root));

return 0;
}

You might also like