DSA-II UNIT-I Binary Search Tree
DSA-II UNIT-I Binary Search Tree
TREE ADT
return root;}
ROUTINE TO INSERT AN ELEMENT
SearchTree insert(int X, Search Tree T)
{
if(T==NULL)
{
T=malloc(size of(Struct Treenode));
if(T!=NULL)
{
T → Element=X;
T → left=NULL;
T → right=NULL;
}
}
else
if(X<T → element)
T → left=Insert(X,T → left);
else
if(X>T → element)
T → right=Insert(X,T → right);
//else X is in the tree already
return T;
ROUTINE FOR FIND OPERATION
🞆 Check whether the root is NULL if so then
return NULL.
🞆 Otherwise , check the value X with the root
node value (i.e T → data)
⚫ If X is equal to T → data, return T
⚫ If X is less than T → data, Traverse the left of T
recursively.
⚫ If X is greater than T → data, Traverse the right of
T recursively.
ROUTINE FOR FIND OPERATION
Int find(int X, Search Tree T)
{
if(T==NULL)
return NULL;
if(X<T → Element)
return Find(X,T → left);
else
if(X>T → Element)
return Find(X,T → right);
else
return T;
}
RECURSIVE ROUTINE FOR FINDMIN
🞆 This operation returns the position of the
smallest element in the tree.
🞆 To perform findmin, start at the root node
and go left as long as there is a left child
RECURSIVE ROUTINE FOR FINDMIN
int findmin(Search Tree T)
{
if(T==NULL)
return NULL;
else if(T → left ==NULL)
return T;
else
return FindMin(T → left);
}
NON RECURSIVE ROUTINE FOR
FINDMIN
int findmin(Search Tree T)
{
if(T!=NULL)
while(T → Left!=NULL)
T=T → Left;
return T;
}
RECURSIVE ROUTINE FOR FINDMAX
int findmax(Search Tree T)
{
if(T==NULL)
return NULL;
else if(T → right ==NULL)
return T;
else
return FindMax(T → right);
}
NON RECURSIVE ROUTINE FOR
FINDMAX
int findmax(Search Tree T)
{
if(T!=NULL)
while(T → right!=NULL)
T=T → right;
return T;
}
DELETION ROUTINE
struct Node* deleteNode(struct Node* root, int
key) {
if (root == NULL) {
return root; // key not found }
// Traverse the tree to find the node to delete
if (key < root->data) {
root->left = deleteNode(root->left, key);
}
else if (key > root->data) {
root->right = deleteNode(root->right,
key); }
CONT..
else { // Node to be deleted found
// Case 1: Node with no child or one child
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; }
CONT..
// Case 2: Node with two children struct Node*
temp = findMin(root->right); // Find inorder
successor
root->data = temp->data; // Copy
successor's data to current node
root->right = deleteNode(root->right, temp-
>data); // Delete successor }
return root; }
EXPRESSION TREE
🞆 The expression tree is a binary tree in which
each internal node corresponds to the
operator and each leaf node corresponds to
the operand
🞆 example expression tree for 3 + ((5+9)*2)
CONSTRUCTION OF EXPRESSION
TREE FROM POSTFIX EXPRESSION
struct Node {
char data;
struct Node* left;
struct Node* right; };
//push operation
void push(Stack* stack, Node* node) {
stack->data[++(stack->top)] = node;}
//Pop operation
Node* pop(Stack* stack) { return stack->data[(stack->top)--];}
// Function to create a new tree node
Node* createNode(char data) {
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->left = node->right = NULL;
return node; }
Node* constructTree(char* postfix) {
Stack* stack = NULL;
stack.top = -1;
if (isalnum(postfix[i])) { // Operand
push(&stack, newNode);
{
else { // Operator
newNode->right = pop(&stack);
newNode->left = pop(&stack);
push(&stack, newNode); }
}
return pop(&stack); // Root of the expression tree }
int main() {
char postfix[] = "ab+cde+**"; // Example
postfix
Node* root =
constructExpressionTree(postfix);
return 0;
}
MAX AND MIN HEAP
🞆 A Max Heap is a complete binary tree in
which the value of each parent node is
greater than or equal to the values of its
children. This ensures that the maximum
value is always at the root of the tree.
🞆 Properties of a Max Heap:
1. Heap Order Property – The key at a parent
node is always greater than or equal to the
keys of its children.
2. Complete Binary Tree – Every level of the
tree is completely filled except possibly the
last level, which is filled from left to right.
EXAMPLE
Given the following sequence of numbers,
construct a Binary Search Tree (BST):
Input Sequence: 50, 30, 70, 20, 40, 60, 80
1. Draw the BST formed after inserting all
elements in the given order.
2. Perform an inorder traversal of the BST and
write the output.
3. Insert the value 25 into the BST and redraw
the updated tree.
4. Delete the node 30 from the BST and explain
how the tree is restructured.
Construct an Expression Tree from a postfix
expression: AB+C*.