Week 12
Week 12
CSO102
WEEK-12 (DRAFT)
BINARY SEARCH TREE:
The left subtree of a node contains only nodes with keys lesser than
the node’s key.
The right subtree of a node contains only nodes with keys greater
than the node’s key.
The left and right subtree each must also be a binary search tree.
BINARY SEARCH TREE:
BINARY SEARCH TREE: Node Structure
struct Node {
int key;
struct Node *left_child;
struct Node *right_child;
};
BINARY SEARCH TREE: Operations
1. Searching
2. Insertion
3. Deletion
4. Traversal
1. Pre-order traversal: It will traverse the root first, then the left child
and at last the right child.
2. Post-order traversal: It will traverse the left subtree first, then root
and then the right subtree.
3. Inorder traversal: It will traverse the left subtree, then root and then
the right subtree.
BINARY SEARCH TREE: Operations
1. Searching
2. Insertion
3. Deletion
4. Traversal
1. Pre-order traversal: It will traverse the root first, then the left child
and at last the right child.
2. Post-order traversal: It will traverse the left subtree first, then root
and then the right subtree.
3. Inorder traversal: It will traverse the left subtree, then root and then
the right subtree.
BINARY SEARCH TREE: Searching
if (root == NULL)
return NULL;
if (num == root->key)
return root->key;
if (num < root->key)
return Binary_search(root->left)
if (num > root->key)
return Binary_search(root->right)
BINARY SEARCH TREE: Insertion
if (Node == NULL)
return create_node(key)
if (key < Node->key)
Node->left_child = insert(Node->left_child, key);
else if (key > Node->key)
Node->right_child = insert(Node->right_child, key);
return Node;
BINARY SEARCH TREE: Deletion
Case 1: Deletion of a leaf node
Case 2: Deletion of an internal node having one child
Case 3: Deletion of an internal node having two child
nodes
BINARY SEARCH TREE: Sorting
1. Construct BST
2. Print inorder traversal sequence
Time Complexity: ?
BINARY HEAP
A Binary Heap is a Complete Binary Tree/ Almost Complete
Binary Tree which is used to store data efficiently to get the
max or min element based on its structure.
Min Heap
Max Heap
STEPS
1. First find out the Minimum among the Violated Node, left,
and right child of Violated Node.
2. If the Minimum among them is the left child, then swap the
Violated Node value with the Left child value and recursively
call the function on the left Child.
3. If the Minimum among them is the right child, then swap
the Violated Node value with the right child value and
recursively call the function right Child.
4. Recursive call stops when the heap property is not
violated.
MIN_HEAPIFY( ):
void heapify(int arr[], int i)
{
int smallest = i; // The node which will be heapified
int l = 2 * i ; // left child node
int r = 2 * i + 1; // right child node
// Check if left child is smaller than its parent
if (l <= n && arr[l] < arr[smallest])
smallest = l;
// Check if right child is smaller than smallest
if (r < =n && arr[r] < arr[smallest])
smallest = r;
// If smallest is not parent
if (smallest != i) {
swap(arr[i], arr[smallest]);
2. At this point, the maximum element is stored at the root of the heap.
Replace it with the last item of the heap followed by reducing the size of the heap by 1.
Finally, heapify the root of the tree.
heapify(arr, N, i);
// Heap sort
for (int i = N - 1; i >= 1; i--) {
swap(&arr[0], &arr[i]);
extractMin()
decreaseKey()
insert()
delete()
MIN HEAP OPERATIONS
getMin(): It returns the root element of Min Heap. The time Complexity of this operation
is O(1). In case of a maxheap it would be getMax().
MIN HEAP OPERATIONS
extractMin(): Removes the minimum element from MinHeap. The time Complexity of
this Operation is O(log N) as this operation needs to maintain the heap property (by
calling heapify()) after removing the root.
MIN HEAP OPERATIONS
decreaseKey(): Decreases the value of the key. The time complexity of this operation is
O(log N). If the decreased key value of a node is greater than the parent of the node,
then we don’t need to do anything. Otherwise, we need to traverse up to fix the violated
heap property.
MIN HEAP OPERATIONS
insert(): Inserting a new key takes O(log N) time. We add a new key at the end of the
tree. If the new key is greater than its parent, then we don’t need to do anything.
Otherwise, we need to traverse up to fix the violated heap property.
MIN HEAP OPERATIONS
delete(): Deleting a key also takes O(log N) time. We replace the key to be deleted with
the minimum infinite by calling decreaseKey(). After decreaseKey(), the minus infinite
value must reach root, so we call extractMin() to remove the key.
Priority queue implementation using heap:
Naive Approach:
Suppose we have N elements and we have to insert these elements in the priority
queue. We can use list and can insert elements in O(N) time and can sort them to
maintain a priority queue in O(N logN ) time.
Priority queue implementation using heap:
Efficient Approach:
We can use heaps to implement the priority queue. It will take O(log N) time to insert and
delete each element in the priority queue.
Based on heap structure, priority queue also has two types max- priority queue and min -
priority queue.
Max Priority Queue is based on the structure of max heap and can perform following
operations:
Maximum :
Extract_Maximum :
Increase Value :
Insert Value :