BST Lecture 1
03 January 2023 07:57
What is Binary Search Tree ?
A binary search tree (BST) is a binary tree data structure which has the following
properties.
• The left subtree of a node contains only nodes with data less than the
node’s data.
• The right subtree of a node contains only nodes with data greater than the
node’s data.
• Both the left and right subtrees must also be binary search trees.
LST < root < RST
Both ST must be a Binary Search Tree
The four basic operations of BST:
a. Searching - O(log(n))
b. Insertion - O(log(n))
c. Deletion - O(log(n))
d. Traversals - O(n)
How operations are O(log(n)) ??
we eliminate half of the array based on the condition
DSA Lectures Page 1
a. Searching - O(log(n))
b. Insertion - O(log(n))
c. Deletion - O(log(n))
d. Traversals - O(n)
Construct BST from sorted array
[Link]
classNode{
Int data;
Node left,right;
Node(int d){
data=d;
left=right=null;
}
}
DSA Lectures Page 2
NodesortedArrayToBST(intarr[], int start, int end){
if(start>end){
Return null;
}
Int mid=(start+end)/2;
Nodenode= newNode(arr[mid]);
[Link]= sortedArrayToBST(arr,start,mid-1);
[Link]= sortedArrayToBST(arr,mid+1,end);
Return node;
}
NodesortedArrayToBST(intarr[], int start, int end){
if(start>end){
Return null;
}
Int mid=(start+end)/2;
Nodenode= newNode(arr[mid]);
[Link]= sortedArrayToBST(arr,start,mid-1);
[Link]= sortedArrayToBST(arr,mid+1,end);
Return node;
}
DSA Lectures Page 3
Basic operations in BST
You are given a partially written BST class. You are required to complete the body
size, sum, max, min and find function.
[Link]
bb2e-05dc9f6dc057
size - return the number of nodes in BST
sum - return the sum of nodes in BST
max - return the value of the node with the greatest value in BST
min - return the value of the node with the smallest value in BST
find - return true if there is a node in the tree equal to "data"
If(null ) return 0;
Return 1
+ size([Link])
+ size([Link]);
if(node == null)return false;
if([Link] == target) return true;
if([Link]> target) return find([Link], target);
else return find([Link], target);
Insert a node in a BST -
[Link]
a49ae6c08134
I think the ans is in driver code itself
DSA Lectures Page 4
I think the ans is in driver code itself
if(root == null) return new Node(data);
if([Link] == data) return root;
if(data < [Link])
[Link] = insertNode([Link], data);
else
[Link] = insertNode([Link], data);
return root;
Delete Node in BST
[Link]
51bd72c5-4874-4108-95b0-65900d4e5ef8
When node to be delete has
Zero Children
One Child
DSA Lectures Page 5
When node to be delete has
Zero Children
One Child
Two Child
public Node deleteNode(Node root, int val){
// WRITE YOUR CODE HERE
// find the node to be deleted and then we
if(root == null) return null;
if(val>[Link]){
[Link] = deleteNode([Link], val);
}
else if(val < [Link]){
[Link] = deleteNode([Link], val);
}
else {
//actual deleteion
if([Link] == null)
return [Link];
else if([Link] == null)
return [Link];
else{
[Link] = minValue([Link]);
[Link] = deleteNode([Link], [Link]);
}
}
return root;
}
Validate BST
Given a binary tree with N number of nodes, check if that input tree is BST (Binary
Search Tree) or not. If yes, print true, print false otherwise.
[Link]
a4fd-914a6a9669ff
DSA Lectures Page 6
Target Sum Pair In BST
You are a given a root node of partially constructed BST. You are also given a
value target , your task is to print all the nodes that add up to form the target value
[Link]
bd73-81ada8c52c67
DSA Lectures Page 7