0% found this document useful (0 votes)
13 views28 pages

Vision Gate 2023 Pds Class 21 Binary Search Tree1701874364337

Uploaded by

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

Vision Gate 2023 Pds Class 21 Binary Search Tree1701874364337

Uploaded by

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

PROGRAMMING & DATA STRUCTURES

Binary Search Tree


Guidelines to Attend Live Class

✓ Pen, Notebook and Calculator are must while attending the online class.

✓ Regularity and Punctuality is necessary.

✓ Hold chat while attending the class. We will allow you to ask and put your questions in the comment
box.

✓ Attempt Quizzes Daily as per the schedule.

✓ Strictly follow the day-wise study plan.


Binary Search Tree

 Last Class – Quick Revision


Binary Search Tree

 Traversal

❑ The order of visiting/accessing nodes in a Binary Tree is called as Traversal.


❑ 2 types of Traversals:
a) Breadth first traversal (Level order traversal)
b) Depth first traversal
• In order
• Pre order
• Post order
Binary Search Tree

 Breadth – First Traversal


Binary Search Tree

 In – Order Traversal
Binary Search Tree

 Pre – Order Traversal


Binary Search Tree

 Post – Order Traversal


Binary Search Tree

 BST – In Order Traversal

❑ Example : BST

In orer Traversal of BST is always in the ascending order of values.


40, 52, 55, 57, 68, 70, 71, 73, 75, 78, 82, 89, 92, 94, 95.
Binary Search Tree

 Practice Problem - 1
Binary Search Tree

 Practice Problem - 2
Binary Search Tree
#include <stdio.h>
#include <stdlib.h>

 Program in ‘C’
struct node {
int item;
struct node* left;
struct node* right;
};

// Inorder traversal
void inorderTraversal(struct node* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ->", root->item);
inorderTraversal(root->right);
}

// preorderTraversal traversal
void preorderTraversal(struct node* root) {
if (root == NULL) return;
printf("%d ->", root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
Binary Search Tree
// postorderTraversal traversal
void postorderTraversal(struct node* root) {

 Program in ‘C’ if (root == NULL) return;


postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ->", root->item);
}

int main() {
struct node* root = createNode(1);
insertLeft(root, 12);
insertRight(root, 9);

insertLeft(root->left, 5);
insertRight(root->left, 6);

printf("Inorder traversal \n");


inorderTraversal(root);

printf("\nPreorder traversal \n");


preorderTraversal(root);

printf("\nPostorder traversal \n");


postorderTraversal(root);
}
Binary Search Tree

 Binary Search Tree

• In a Binary search tree, the value of left node must


be smaller than the parent node, and the value of
right node must be greater than the parent node.
This rule is applied recursively to the left and right
subtrees of the root.
Binary Search Tree

 BST - Operations

The Operations that can be performed on BST include:


❑ Construction
❑ Insertion
❑ Deletion
❑ Search
Binary Search Tree

 BST – Construction / Insertion

❑ Construct a BST with elements 45, 15, 79, 90, 10, 55, 12, 20, 50 inserted in the same
sequence.
Binary Search Tree

 Deletion from BST

In a binary search tree, we must delete a node from the tree by keeping in mind that the
property of BST is not violated. To delete a node from BST, there are three possible situations
occur -
❑ The node to be deleted is the leaf node, or,
❑ The node to be deleted has only one child, and,
❑ The node to be deleted has two children
Binary Search Tree

 Deletion from BST – Leaf Node

❑ It is the simplest case to delete a node in BST. Here, we have to replace the leaf node
with NULL and simply free the allocated space.
Binary Search Tree

 Deletion from BST – Node With 1 Child

❑ In this case, we have to replace the target node with its child, and then delete the child
node. It means that after replacing the target node with its child node, the child node
will now contain the value to be deleted. So, we simply have to replace the child node
with NULL and free up the allocated space.
Binary Search Tree

 Deletion from BST – Node With 2 Children

This case of deleting a node in BST is a bit complex among other two cases. In such a case,
the steps to be followed are listed as follows -
❑ First, find the inorder successor of the node to be deleted.
❑ After that, replace that node with the inorder successor until the target node is placed
at the leaf of tree.
❑ And at last, replace the node with NULL and free up the allocated space.
Binary Search Tree

Example-1 The Minimum number of nodes with height 2n in a binary tree will be _______.

A 2n B 2n + 1

C 2n - 1 D 2n + 1
Binary Search Tree

The number of leaf nodes in a binary tree, if there are 16 nodes with 2 children
Example-2
is ____.

A 15 B 16

C 17 D 8
Binary Search Tree

The Number Of Nodes in a Perfect binary tree at level 8 will be _____.


Example-3
(NOTE: Level Numbering started from 1)

A 127 B 128

C 255 D 256
Binary Search Tree

Consider a Right Skewed Binary Tree with elements labelled A to E, while A


Example-4
being root and E being leaf node (In Alphabetical order, level wise). Then, For
such tree, Pick Correct statement from below:

A In Order Traversal == Level Order Traversal == Post Order Traversal

Pre Order Traversal == In Order Traversal == Level Order Traversal

C In Order Traversal == Post Order Traversal == Pre Order Traversal

D Post Order Traversal == Level Order Traversal == Pre Order Traversal


Binary Search Tree

Example-5 If The Elements 45, 32, 70, 62, 53, 12 and 16 are inserted into a BST in the same
sequence, Then The resultant BST is given as _____.

A 45, 32, 70, 16, 62, 12, 53 B 45, 32, 70, 62, 12, 16, 53

C 45, 32, 70, 12, 16, 62, 53 D 45, 32, 70, 12, 62, 16, 53
Binary Search Tree

For a Binary Tree, The In order Traversal Sequence is B,A,D,C,E,F,G,I,H,J and Pre
Example-6 Order Sequence is E,A,B,C,D,F,G,H,I,J. The Post Order Traversal Sequence is
________.

A B,D,C,A,,I,J,H,G,F,E B B,D,C,I,A,J,H,G,F,E

C B,D,C,A,,J,I,H,G,F,E D B,D,C,A,I,J,H,F,G,E
Binary Search Tree

The Post Order traversal sequence for the BST with Pre order sequence as
Example-7
20,10,5,15,12,17,16,19,32,27,37,34,40 is _______.

A 5, 12, 16, 19, 17, 15, 10, 27, 34, 40, 37, 32, 20

5, 12, 16, 19, 17, 15, 10, 34, 27, 40, 37, 32, 20

C 5, 12, 16, 19, 17, 15, 10, 27, 34, 37, 40, 32, 20

D 5, 12, 16, 19, 17, 15, 10, 27, 37, 40, 34, 32, 20

You might also like