{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

notes108

# notes108 - h = | log(n 1 | h = O(log n Operation with heaps...

This preview shows pages 1–8. Sign up to view the full content.

Heaps - More efficient implementation of priority queues Insert element (k,e) O(log(n) ) Remove element () O(log(n) ) - A heap is a binary tree that stores key element pairs - It needs to satisfy two properties Order properties Key(parent) <= key(child) Structural property All levels are full except last one. In the last level all nodes are shifted to the left(left filled). This is called “Complete binary tree” Heap non heap It is not a heap because it is not left filled or because it break order property. Height of a heap What is the height of the heap? Two extremes Last level almost empty

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
n = 4 h = 3 n >= 2^n-1 4 >= 2^(3-1) 4 >= 4 Last level is full h n =7 h =3 n= node n <= 2^n – 1 7 <= 2^3 –1 7 <= 7 upper bound 2^(n-1) <= n <= 2^n-1 2^(n-1) <= n+1 <= 2^n log(2^(n-1)) <= log(n+1) <= log(2^n) n-1(log(2)) <= log(n+1) <= log(2) _ _ h-1 <= log(n+1) <= h h = | log (n+1) | h = O(log n) Operation with heaps Heaps insertion

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: h = | log (n+1) | h = O(log n) Operation with heaps Heaps insertion Original Insert element(2,e) 1 Insert in next available position 2. Fix heap bottom up (up-heap) swap parent –child keys are out of order from new node to root. #swaps <= n-1 #swaps = O(log n) insert elemento (k,e) takes O(log n) remove min original e = remove min element() 1. removing min element leaves a hole replace with last key of heap 2. Fix the heap top-down (down heap) Swap keys of parent and child if they are out of order starting from root to the bottom of replaced node Return s 2 Remove Min Element Again Return 3 #of comparison and swaps is O(log n) remove min Element() is O(log n) We may use a heap to do sorting of sequences Phase 1 for each element in S-remove element is S-Insert element in H(heap) Insert element in heap O(log n) n times = O(n log n) Phase 2 For each element in Heap-remove min Element from H-Insert H into s Remove Min Element in heap O(log n) N times = O(n log n) Phase 1 Phase 2 O(n log n) + O(n log n) = O(n log n) Sorting using a heap takes O(n log n) This sorting algorithm is called “heap sort” Implementation of a heap using an array Parents is in I Left child is in 2i Right child is in 2i+1...
View Full Document

{[ snackBarMessage ]}

### Page1 / 8

notes108 - h = | log(n 1 | h = O(log n Operation with heaps...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online