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

BST Lecture 1 - Class Notes

The document discusses binary search trees (BSTs). It defines a BST as a binary tree where the left subtree of a node contains values less than the node's value, and the right subtree contains values greater than the node's value. It lists the basic BST operations as search, insertion, deletion, and traversal, and explains that search, insertion, and deletion operations have O(log n) time complexity because the tree is divided in half at each step. It then provides code examples for constructing a BST from a sorted array, inserting a node, deleting a node, and validating if a tree is a BST.

Uploaded by

vignesh waran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

BST Lecture 1 - Class Notes

The document discusses binary search trees (BSTs). It defines a BST as a binary tree where the left subtree of a node contains values less than the node's value, and the right subtree contains values greater than the node's value. It lists the basic BST operations as search, insertion, deletion, and traversal, and explains that search, insertion, and deletion operations have O(log n) time complexity because the tree is divided in half at each step. It then provides code examples for constructing a BST from a sorted array, inserting a node, deleting a node, and validating if a tree is a BST.

Uploaded by

vignesh waran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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


https://www.geeksforgeeks.org/sorted-array-to-balanced-bst/

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]);
node.left= sortedArrayToBST(arr,start,mid-1);
node.right= 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]);
node.left= sortedArrayToBST(arr,start,mid-1);
node.right= 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.
https://course.acciojob.com/idle?question=4be84bda-20ca-44bb-
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(root.left)
+ size(root.right);

if(node == null)return false;


if(node.data == target) return true;
if(node.data> target) return find(node.left, target);
else return find(node.right, target);

Insert a node in a BST -


https://course.acciojob.com/idle?question=e552caac-75e2-4f1e-8aee-
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(root.val == data) return root;
if(data < root.val)
root.left = insertNode(root.left, data);
else
root.right = insertNode(root.right, data);
return root;

Delete Node in BST


https://course.acciojob.com/idle?question=
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>root.val){
root.right = deleteNode(root.right, val);
}
else if(val < root.val){
root.left = deleteNode(root.left, val);
}
else {
//actual deleteion
if(root.left == null)
return root.right;
else if(root.right == null)
return root.left;
else{
root.val = minValue(root.right);
root.right = deleteNode(root.right, root.val);
}
}
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.
https://course.acciojob.com/idle?question=3b992182-31dd-4aaa-
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
https://course.acciojob.com/idle?question=0dd4bc90-7cd3-45f8-
bd73-81ada8c52c67

DSA Lectures Page 7

You might also like