110 likes | 272 Views
Heaps. Full Binary Tree. A binary tree in which all the leaves are on the same level every nonleaf node has two children. Complete Binary Tree. A binary tree that is either full or full to the next-to-last level with leaves on last level as far left as possible. Heap. Two requirements
E N D
Full Binary Tree • A binary tree in which • all the leaves are on the same level • every nonleaf node has two children
Complete Binary Tree • A binary tree that is either • full or • full to the next-to-last level • with leaves on last level as far left as possible
Heap • Two requirements • Shape • a complete binary tree • Order • each of elements contains a value • less (greater) than or equal value of either child
Storing Tree in Array • Given parent is stored at position i • left child is in 2i +1 • right child is in 2i + 2 • parent of node i is at (i-1)/2
Delete 15, Restart and Insert 20 • left child = 2i +1 • right child is in 2i + 2 • parent i is at (i-1)/2
Demo • Priority queue may be implemented using heaps. • If we add or delete nodes, the heap would have to be adjusted. • http://www.ee.uwa.edu.au/~plsd210/ds/heaps.html#p_queue_anim
Heap Sort Demo • http://gaigs.cmsc.lawrence.edu/cmsc34/AVClient.html • http://students.ceid.upatras.gr/~perisian/data_structure/HeapSort/heap_applet.html
STL Heap Algorithms See HeapDemo.cpp
STL priority_queue Operations See priorityqdemo.cpp