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

Heap

Uploaded by

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

Heap

Uploaded by

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

2023 | EtCPC

EtCPC - 3rd Tutorial Session

Trees and
Heaps
Presenter :- Anwar Misbah
Topics to be covered

Binary Search Tree (BST)

Heap Implementation

Disjoint-set data structure


Binary Search Tree(BST)

The BST is one way to organize data in a tree structure. In each subtree
rooted at x, the following BST property holds: Items on the left subtree of
x are smaller than x and items on the right subtree of x are greater than
(or equal to) x.
In the worst case, only O(log n) operations are
required in a root-to-leaf scan . However, this only
holds if the BST is balanced.

The BST property ensures that searching, insertion,


and deletion of data can be done in O(log n) time,
where n is the number of nodes in the tree.

Therefore, if we are searching for a specific value


in the tree, we can eliminate half of the remaining
nodes in each step of the search, leading to a
worst-case time complexity of O(log n).
Suppose we want to construct a binary search tree
with the following values: 5, 3, 8, 1, 6, 9.
First, we create a root node with the value 5. Next, we
insert the value 3. Since 3 is less than 5, it becomes the
left child of the root
5

3
We then insert the value 8. Since 8 is greater than 5, it
becomes the right child of the root:

3 8
Next, we insert the value 1. Since 1 is less than 5 and 3, it
becomes the left child of 3

3 8

1
We then insert the value 6. Since 6 is greater than 5 and
less than 8, it becomes the right child of 3

3 8

1 6
Finally, we insert the value 9. Since 9 is greater than 5
and 8, it becomes the right child of 8

3 8

1 6 9
To search for a value in the tree, we start at the
root and compare the value we are searching
for with the value of the current node.

If the value is less than the current node, we


move to its left child. If the value is greater than
the current node, we move to its right child.

We continue this process until we find the value


we are searching for or reach a leaf node (a
node with no children).
Heap

The heap is another way to organize data in a tree.

The (Binary) Heap is also a binary tree like the BST, except that it must be a complete tree.

Complete binary trees can be stored efficiently in a compact 1-indexed array of size n + 1,
which is often preferred to an explicit tree representation.

A heap is a binary tree where each node is either greater than or equal to (max heap) or less than or
equal to (min heap) its children.

The root node of the heap is either the minimum or maximum element of the heap, depending on
whether it is a min heap or max heap.
There are two main types of heaps: max heaps and min heaps.

Max heap
In a max heap, the root node is the maximum element in the heap, and each
parent node is greater than or equal to its children.

Let's say we have the following set of elements: { 5, 8, 3, 10, 2, 7 }

We can begin by adding the first element to an empty heap in order to generate a
max heap. Then, in order to retain the maximum heap property, we put each
additional member into the heap and "bubble up" the element to its proper
location.
Insert 5: The heap is empty, so we insert 5 as the root node.

5
Then, Insert 8 We insert 8 as the left child of the root. Since 8 is greater
than 5, we swap the two elements.

5
Then Insert 3: We insert 3 as the left child of 8. Since 8 is still greater than 3,
we do not need to swap any elements. However, 3 is less than its parent, so
we need to bubble it up further.

5 3
Insert 10 We insert 10 as the right child of 8. Since 10 is greater than both its
parent and grandparent, we swap it with 8 and then with 5.

10

5 8

3 2
Insert 2: We insert 2 as the left child of 5. Since 5 is greater than 2, we do not
need to swap any elements. However, 2 is less than its parent, so we need
to bubble it up further.

10

5 8

3 2

7
Insert 7: We insert 7 as the right child of 2. Since 7 is greater than its parent,
we swap it with 2 and then with 5.

10

7 8

3 2

5 The resulting max heap is: { 10, 7, 8, 3, 2, 5 }


Min heap
In a min heap, the root node is the minimum element in the heap, and
each parent node is less than or equal to its children.

Let's say we have the following set of elements: { 5, 8, 3, 10, 2, 7 }

To create a min heap, we can follow a similar process as max heap, but
maintain the min heap property instead.
Insert 5: The heap is empty, so we insert 5 as the root node.

5
Then, Insert 8: We insert 8 as the left child of the root. Since 5 is less than 8,
we do not need to swap any elements.

8
Then Insert 3: We insert 3 as the left child of 8. Since 3 is less than both its
parent and grandparent, we swap it with 8 and then with 5.

8 5
Insert 10: We insert 10 as the right child of 8. Since 8 is less than 10, we do not
need to swap any elements.

8 5

10
Insert 2: We insert 2 as the left child of 8. Since 2 is less than both its parent
and grandparent, we swap it with 8 and then with 3.

8 5

10 3
Insert 7: We insert 7 as the right child of 3. Since 3 is less than 7, we do not
need to swap any elements.

8 5

10 3

The resulting min heap is: { 2, 8, 5, 10, 3, 7 }


7
Disjoint Set( Union Find )
The Union-Find Disjoint Set (UFDS) is a data
structure to model a collection of disjoint sets
with the ability to efficiently determine whether
two items belong to the same set and to unite
two disjoint sets into one larger set.

A disjoint-set data structure is a data structure


that keeps track of a set of elements
partitioned into a number of disjoint (non
overlapping) subsets.

A union-find algorithm is an algorithm that


performs two useful operations on such a data
structure:
Find: Determine which subset a particular element is in. This can be
used for determining if two elements are in the same subset.

Union: Join two subsets into a single subset.

unionSet(0, 1) sets p[0] to 1 and rank[1] to 1.

unionSet(2, 3) sets p[2] to 3 and rank[3] to 1.

unionSet(3, 4) sets p[4] to 3.


C++ implementation is shown below
vector<int> p(N), rank(N); for (int i = 0; i < N; i++) p[i] = i; }
int findSet(int i) { return (p[i] == i) ? i : (p[i] = findSet(p[i])); }
bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
void unionSet(int i, int j) {
if (!isSameSet(i, j)) { // if from different set
int x = findSet(i), y = findSet(j);
if (rank[x] > rank[y]) p[y] = x; // rank keeps the tree short
else {
p[x] = y;
if (rank[x] == rank[y]) rank[y]++;
}
}
}
2023 | EtCPC

Thank You
Questions ?

You might also like