1
CSE326:DataStructures Lecture#4 HeapsmorePriorityQs
BartNiswonger SummerQuarter2001
Today’sOutline
- Returnquizzes
- ThingsBartDidn’tFinishonFriday
(insert&d-Heaps)
- LeftistHeaps
- SkewHeaps
- ComparingHeaps
CSE326:DataStructures Lecture#4 HeapsmorePriorityQs BartNiswonger - - PDF document
CSE326:DataStructures Lecture#4 HeapsmorePriorityQs BartNiswonger SummerQuarter2001 TodaysOutline Returnquizzes ThingsBartDidntFinishonFriday (insert&d-Heaps)
1
CSE326:DataStructures Lecture#4 HeapsmorePriorityQs
BartNiswonger SummerQuarter2001
Today’sOutline
(insert&d-Heaps)
2
PriorityQueueADT
– create – destroy – insert – deleteMin – is_empty
thequeue,x andy,ifx hasalowerpriority value thany,x willbedeletedbeforey
F(7) E(5) D(100) A(4) B(6)
insert deleteMin
G(9) C(3) 20 14 12 9 11 8 10 6 7 5 4 2 2 4 5 7 6 10 8 11 9 12 14 20 12
1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12
NiftyStorageTrick
– child: – parent: – root: – nextfree:
3
DeleteMin
20 14 12 9 11 8 10 6 7 5 4 ? 2 20 14 12 9 11 8 10 6 7 5 4 2 pqueue.deleteMin()
Insert
20 14 12 9 11 8 10 6 7 5 4 2 20 14 12 9 11 8 10 6 7 5 4 2 pqueue.insert(3) ?
4
PercolateUp
20 14 12 9 11 8 10 6 7 5 4 2 ? 20 14 12 9 11 8 ? 6 7 5 4 2 10 20 14 12 9 11 8 5 6 7 ? 4 2 10 20 14 12 9 11 8 5 6 7 3 4 2 10 3 3 3
InsertCode
voidinsert(Objecto){ assert(!isFull()); size++; newPos= percolateUp(size,o); Heap[newPos]=o; }
intpercolateUp(inthole, Objectval){ while(hole>1&& val<Heap[hole/2]) Heap[hole]=Heap[hole/2]; hole/=2; } returnhole; }
runtime:
5
OtherPriorityQueue Operations
– giventhepositionofanobjectinthequeue, reduceitspriorityvalue
– giventhepositionofananobjectinthequeue, increaseitspriorityvalue
– giventhepositionofanobjectinthequeue, removeit
– givenasetofitems,buildaheap
DecreaseKey,IncreaseKey, andRemove
voiddecreaseKey(int obj){ assert(size>=obj); temp=Heap[obj]; newPos= percolateUp(obj,temp); Heap[newPos]=temp; } voidincreaseKey(intobj){ assert(size>=obj); temp=Heap[obj]; newPos=percolateDown(obj,temp); Heap[newPos]=temp; }
voidremove(intobj){ assert(size>= obj); percolateUp(obj, NEG_INF_VAL); deleteMin(); }
6
BuildHeap
Floyd’sMethod.Thankyou,Floyd.
5 11 3 10 6 9 4 8 1 7 2 12 pretendit’saheapandfixtheheap-orderproperty! 2 7 1 8 4 9 6 10 3 11 5 12
Build(this)Heap
6 7 1 8 4 9 2 10 3 11 5 12 6 7 10 8 4 9 2 1 3 11 5 12 11 7 10 8 4 9 6 1 3 2 5 12 11 7 10 8 4 9 6 5 3 2 1 12
7
Finally…
11 7 10 8 12 9 6 5 4 2 3 1 runtime:
ThinkingaboutHeaps
– findingachild/parentindexisamultiply/divideby two – operationsjumpwidelythroughtheheap – eachoperationlooksatonlytwonewnodes – insertsareatleastascommonasdeleteMins
– divisionandmultiplicationbypowersoftwoare fast – lookingatonenewpieceofdatasucksinacache line – withhuge datasets,diskaccessesdominate
8 4 9 6 5 4 2 3 1 8 10 12 7 11
Solution:d-Heaps
array
– optimizeperformancebasedon #ofinserts/removes – chooseapoweroftwofor efficiency – fitonesetofchildreninacache line – fitonesetofchildrenona memorypage/diskblock
3 7 2 8 5 121110 6 9 1 12
OneMoreOperation
9
Merge
Giventwoheaps,mergethemintoone heap
– firstattempt:inserteachelementofthe smallerheapintothelarger. runtime: – secondattempt:concatenateheaps’arrays andrunbuildHeap. runtime:
HowaboutO(logn)time?
Idea:HangaNewTree
12 13 10 6 11 5 2
+
10 14 4 9 1
=
14 12 13 10 6 4 9 11 5 1 2 ? 10 Now,just percolatedown!
10
Idea:HangaNewTree
12 13 10 6 11 5 2
+
10 6 11 5 2
=
12 13
Problem?
12 13 12 13
LeftistHeaps
– almostallnodesareontheleft – allthemergingworkisontheright
makeitsothatalltheworkyou havetodoinmaintainingaheap isinonesmallpart
11 thenullpathlength(npl) ofanodeisthenumber
RandomDefinition: NullPathLength
node)=0
1 1 1 2 anotherwayoflookingatit: nplistheheightofcomplete subtreerootedatthisnode
LeftistHeapProperties
– parent’spriorityvalueis tochildrens’ priority values – result:minimumelementisattheroot
– nullpathlengthofleftsubtree is nplofright subtree – result:treeisatleastas“heavy”ontheleftas theright
Areleftisttreescomplete?Balanced?
12
Leftisttreeexamples
NOT leftist leftist 1 1 1 2 1 1 2 1 1 leftist everysubtreeofaleftist treeisleftist,comrade!
RightPathinaLeftistTreeisShort
r,thetreehasatleast2r-1 nodes
Basis:r=1.Treehasatleast
Inductivestep:assumetrueforr’<r.Therightsubtreehasa rightpathofatleastr- 1nodes,soithasatleast2r- 1 - 1 nodes.Theleftsubtreemustalsohavearightpathofatleast r- 1 (otherwise,thereisanullpathofr- 3,lessthanthe rightsubtree).Again,thelefthas2r- 1 - 1 nodes.Alltoldthen, thereareatleast: 2r- 1 - 1+2r- 1 - 1+1=2r - 1
pathofatmostlogn nodes
1 1 2 1
13
ToDo
– Tuesday– 10:50am– Sieg322
14
ComingUp