Vision Gate 2023 Pds Class 21 Binary Search Tree1701874364337
Vision Gate 2023 Pds Class 21 Binary Search Tree1701874364337
✓ Pen, Notebook and Calculator are must while attending the online class.
✓ Hold chat while attending the class. We will allow you to ask and put your questions in the comment
box.
Traversal
In – Order Traversal
Binary Search Tree
❑ Example : BST
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) {
int main() {
struct node* root = createNode(1);
insertLeft(root, 12);
insertRight(root, 9);
insertLeft(root->left, 5);
insertRight(root->left, 6);
BST - Operations
❑ Construct a BST with elements 45, 15, 79, 90, 10, 55, 12, 20, 50 inserted in the same
sequence.
Binary Search Tree
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
❑ 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
❑ 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
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
A 127 B 128
C 255 D 256
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