prog
prog
1
2 3
4 5
TYPES OF HEAP
Min Heap- The value of a node must be less then or equal to the values of its children.
Max Heap - The value of a node must be grater than or equal to the values of its children.
Example:
1 Min Heap- The value of a 7
Max Heap - The value of a
2 3 node must be less than or 3 6 node must be grater than or
equal to the values of its equal to the values of its
4 5 children. 1 2 4 children.
TRY THIS!
Heapify is a proceess of adjusting the location of
a node in order to follow the heap property.
11
This is max heap Which nodes violates the heap property?
5 2
31
6 17 14 17 Max heap
This are min heap
1 21
9 10 12 18 9 8 12 18
3 2 8 7 3 2 1 7
To heapify an element, we have to find the maximum of its children and swap it with the current
element. Continue this process until the heap property is satisfied at every node
31 31 31 19
10 16 10 19 10 19 10 16
9 8 14 12 9 8 16 12 8 16 12
9 9 8 14 12
3 1 5 7 19 3 1 5 7 14 3 1 5 7 14 3 1 5 7
AVL Trees
Content
Here's what you'll find in this presentation:
1. Analysis of Tree-based Algorithms (BST)
2.Operations in BST
3. Binary Tree Operations
4.AVL tree rotations
The value of every node is GREATER THAN OR EQUAL TO the values of the nodes in its left subtree and is
LESS THAN OR EQUAL TO the values of the nodes in its right subtree.
Tree Operations in Binary Search Tree
• make_null()
6
•find() or search()
4 8 • find_min() and find_max()
2 5 10 9 •insert()
8 9 •delete()
1 3
Delete: 2
2 is less/greater 2 is less/greater
than 3? 2 is less/greater 2 is less/greater
6 6 than 6?
than 3? than 6?
3 8 6
3 8
2 3 8
4 7 2 7
4 1 will replace 2
1 2 4 7
1
1
is 2 less or IF THERE’S 4
greater than 3? ANOTHER NODE is 2 less or
greater than 3?
Find max
Find min
6
6
3 8
3 8
2 4 7
2 4 7
1
1
max always in right, so you
min always in left, so you
don’t need to go in left.
don’t need to go in right.