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

prog

Programming, Heap, AVL trees

Uploaded by

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

prog

Programming, Heap, AVL trees

Uploaded by

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

Heap is a tree with some special properties (complete binary trees).

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

Therefore it cannot be consider as a sample of a heap 9 10 12 18


Min heap
Heapifying Max Heap 3 2 8 7
31 31
1 21 10 21 Min heap should be a max heap!

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

Other operation in heap


Insert element
remove min/max element

Insert: 19 Delete max

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

BINARY SEARCH TREES


(BST) is a special search tree that satisfies the following property:
NOTE: A search tree is a tree data structure used for locating specific keys from within a set.

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

Find: 4 compare if so we go to right, if Insert: 5


4 is equal 4 is = to 4. So its a Is 5 is greater/
6 6 6
to 3 time to stop less than 6?
3 8 searching 3 8 3 8
2 4 7 2 2 7
4 7 4
1 1 compare if 5 is 1 5
greater than 3?
This will be the
then if next node
node 5
is greater/less
than?

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.

You might also like