Binary Search Trees:
● A binary search tree or BST is a tree data structure in which each node has a (at
most)maximum of 2 children(not more than 2).
● All nodes of the left subtree are less than the root node.
● All nodes of the right subtree are greater than the root node.
● Binary search trees are used in most of the applications of computer science
domain
like searching, sorting, insertion, and deletion etc.
In short, all BSTs are binary trees, but not all binary trees are BSTs due to the
ordering rules in BSTs.
Q. Create the binary search tree using the following data elements.
43, 10, 79, 90, 12, 54, 11, 9, 50
● Insert 43 into the tree as the root of the tree.
● Read the next element, if it is less than the root node element, insert it as the root of
the
left sub-tree.
● Otherwise, insert it as the root of the right of the right subtree.
● The process of creating BST by using the given elements, is shown in the image on
the
next page.
1
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a tree node
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
// Function to create a new node
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (!newNode) {
printf("Memory error\n");
2
return NULL;
}
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
// Function to insert a node into the BST
Node* insert(Node* root, int data) {
// If the tree is empty, create a root node
if (root == NULL) {
root = createNode(data);
return root;
}
// Recursively insert data into the left or right subtree
if (data < root->data) {
root->left = insert(root->left, data);
} else if (data > root->data) {
root->right = insert(root->right, data);
}
// Return the modified root node
return root;
}
// Function for inorder traversal
void inorderTraversal(Node* root) {
3
if (root) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}
// Function for preorder traversal
void preorderTraversal(Node* root) {
if (root) {
printf("%d ", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
}
// Function for postorder traversal
void postorderTraversal(Node* root) {
if (root) {
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->data);
}
}
// Function to search for a node in the BST
Node* search(Node* root, int data) {
if (root == NULL || root->data == data) {
4
return root;
}
if (data < root->data) {
return search(root->left, data);
}
else {
return search(root->right, data);
}
}
int main() {
Node* root = NULL;
// Insert nodes into the BST
root = insert(root, 50);
root = insert(root, 30);
root = insert(root, 20);
root = insert(root, 40);
root = insert(root, 70);
root = insert(root, 60);
root = insert(root, 80);
printf("Inorder traversal: ");
inorderTraversal(root);
printf("\n");
printf("Preorder traversal: ");
5
preorderTraversal(root);
printf("\n");
printf("Postorder traversal: ");
postorderTraversal(root);
printf("\n");
int searchData = 60;
Node* searchedNode = search(root, searchData);
if (searchedNode) {
printf("Node %d found in the BST\n", searchData);
} else {
printf("Node %d not found in the BST\n", searchData);
}
return 0;
}