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

DSA-II UNIT-I Binary Search Tree

The document provides an overview of Binary Search Trees (BST), detailing their properties, node representation, insertion, searching, and deletion routines. It also discusses expression trees, including their construction from postfix expressions, and introduces the concept of Max Heaps. Additionally, it includes examples and routines for various operations related to BSTs and expression trees.

Uploaded by

Ms. Pavithra D
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)
3 views

DSA-II UNIT-I Binary Search Tree

The document provides an overview of Binary Search Trees (BST), detailing their properties, node representation, insertion, searching, and deletion routines. It also discusses expression trees, including their construction from postfix expressions, and introduces the concept of Max Heaps. Additionally, it includes examples and routines for various operations related to BSTs and expression trees.

Uploaded by

Ms. Pavithra D
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/ 30

UNIT-I

TREE ADT

BINARY SEARCH TREE


BST
🞆 It is a binary tree.
🞆 A Binary Search Tree (BST) is a tree in which
all the nodes follow the below-mentioned
properties
⚫ The value of the left sub-tree is less than the
value of its parent (root) node's key.
⚫ The value of the right sub-tree is greater than or
equal to the value of its parent (root) node's key.
🞆 Thus, BST divides all its sub-trees into two
segments; the left sub-tree and the right sub-
tree and can be defined as −
🞆 left_subtree (keys) < node (key)
<right_subtree (keys)
REPRESENTATION

🞆 BST is a collection of nodes arranged in a


way where they maintain BST properties.
Each node has a key and an associated
value. While searching, the desired key is
compared to the keys in BST and if found, the
associated value is retrieved.
NODE REPRESENTATION
struct treenode
{
int data;
struct tree *leftChild;
struct tree *rightChild;
}
EXAMPLE
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 lesser 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 sub-tree.
ROUTINE TO INSERT AN ELEMENT

🞆 To insert the element X into the tree.


🞆 Check with the root node T
🞆 If it is less than the root
⚫ Traverse the left subtree recursively until it
reaches the T→left equals to NULL. Then X is
placed in T → left.
🞆 If it is greater than the root
⚫ Traverse the right subtree recursively until it
reaches the T → right equals to NULL. Then X is
placed in T → right.
struct Node { struct Node* createNode(int
int data; value) {
struct Node* left; struct Node* newNode = (struct
Node*)malloc(sizeof(struct
struct Node* right;
Node));
};
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode; }
BST INSERTION
struct Node* insert(struct Node* root, int
value) {
if (root == NULL) {
return createNode(value); }
if (value < root->data) {
root->left = insert(root->left, value); }
else if (value > root->data) {
root->right = insert(root->right, value); }

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; };

// Stack structure for nodes


typedef struct Stack {
Node* data[100]; // Assuming max 100 elements int top;}
Stack;

//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;

for (int i = 0; postfix[i] != '\0'; i++) {


Node* newNode = createNode(postfix[i]);

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*.

You might also like