SlideServe
Menu
Préférences
© SlideServe · v2026

Priority Queues

wilma-mcmahon avatar
56 diapositives · 760 Views · Nov 19, 2014

Priority Queues. Two kinds of priority queues: Min priority queue. Max priority queue. Complexity Of Operations. Sorted List Insert O(N) Delete O(1) Unsorted List Insert O(1) Delete O(N). Heaps. Two key properties Heap shape Complete binary tree Value at node

Priority Queues - slide 1
01 / 56Priority Queues
1 / 17

Télécharger la présentation

Priority Queues
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue.

  2. Complexity Of Operations Sorted List Insert O(N) Delete O(1) Unsorted List Insert O(1) Delete O(N)

  3. Heaps • Two key properties • Heap shape • Complete binary tree • Value at node • Min Heap: Smaller than or equal to values in subtrees • Max Heap: Larger than or equal to values in subtrees • Example heap • X  Y • X  Z X Y Z

  4. Heap & Non-heap Examples 5 5 8 5 45 6 45 5 2 2 6 22 6 22 6 22 25 8 45 25 8 45 25 Heaps Non-heaps

  5. 2 4 3 6 7 9 3 8 6 Min Heap With 9 Nodes Complete binary tree with 9 nodes that is also a min tree.

  6. 9 8 7 6 7 2 6 5 1 Max Heap With 9 Nodes Complete binary tree with 9 nodes that is also a max tree.

  7. Heap Height Since a heap is a complete binary tree, the height of an n node heap is log2 (n+1).

  8. 9 8 7 6 7 2 6 5 1 A Heap Is Efficiently Represented As An Array 9 8 7 6 7 2 6 5 1 0 1 2 3 4 5 6 7 8 9 10

  9. 9 8 7 6 7 2 6 5 1 7 Inserting An Element Into A Max Heap Complete binary tree with 10 nodes.

  10. Inserting An Element Into A Max Heap 9 8 7 6 7 2 6 New element is 5. 5 1 7 5

  11. Inserting An Element Into A Max Heap 9 8 7 6 2 6 7 New element is 20. 5 1 7 7

  12. Inserting An Element Into A Max Heap 9 8 7 6 2 6 New element is 20. 5 1 7 7 7

  13. Inserting An Element Into A Max Heap 9 7 6 8 2 6 New element is 20. 5 1 7 7 7

  14. Inserting An Element Into A Max Heap 20 9 7 6 8 2 6 New element is 20. 5 1 7 7 7

  15. Inserting An Element Into A Max Heap 20 9 7 6 8 2 6 Complete binary tree with 11 nodes. 5 1 7 7 7

  16. Inserting An Element Into A Max Heap 20 9 7 6 8 2 6 New element is 15. 5 1 7 7 7

  17. Inserting An Element Into A Max Heap 20 9 7 6 2 6 New element is 15. 8 5 1 7 7 8 7

  18. Inserting An Element Into A Max Heap 20 7 15 6 9 2 6 New element is 15. 8 5 1 7 7 8 7

  19. Heap Operations – Insert( X ) • Algorithm • Add X to end of tree • While (X > parent) • Swap X with parent // X bubbles up tree • Complexity • # of swaps proportional to height of tree • O( log(n) )

  20. Insertion Insert(int key) { n++; //heap size+1 i=n; while (key > heap[i/2] && i>1) { heap[i]=heap[i/2]; i=i/2; } heap[i]=key; }

  21. 20 7 15 6 9 2 6 8 5 1 7 7 8 7 Removing The Max Element Max element is in the root.

  22. Removing The Max Element 7 15 6 9 2 6 After max element is removed. 8 5 1 7 7 8 7

  23. Removing The Max Element 7 15 6 9 2 6 Heap with 10 nodes. 8 5 1 7 7 8 7 Reinsert 8 into the heap.

  24. Removing The Max Element 7 15 6 9 2 6 Reinsert 8 into the heap. 5 1 7 7 7

  25. Removing The Max Element 15 7 6 9 2 6 Reinsert 8 into the heap. 5 1 7 7 7

  26. Removing The Max Element 15 9 7 6 2 6 8 Reinsert 8 into the heap. 5 1 7 7 7

  27. Removing The Max Element 15 9 7 6 2 6 8 Max element is 15. 5 1 7 7 7

  28. Removing The Max Element 9 7 6 2 6 8 After max element is removed. 5 1 7 7 7

  29. Removing The Max Element 9 7 6 2 6 8 Heap with 9 nodes. 5 1 7 7 7

  30. Removing The Max Element 9 7 6 2 6 8 Reinsert 7. 5 1

  31. Removing The Max Element 9 7 6 2 6 8 Reinsert 7. 5 1

  32. Removing The Max Element 9 8 7 6 7 2 6 Reinsert 7. 5 1

  33. Deletion Delete() { key=heap[1]; data=heap[n--]; i=1; while ((j=2*i)<=n) { if (2*i+1<=n) { if (heap[2*i+1] > heap[2*i]) j=2*i+1; } if (heap[j] > data); { heap[i]=heap[j]; i=j; } else break; } heap[i]=data; return key; }

  34. 9 8 7 6 7 2 6 5 1 Complexity Of Remove Max Element Complexity is O(log n).

  35. Binary Search Tree 20 10 40 6 15 30 25 2 8 Only keys are shown.

  36. Definition Of Binary Search Tree A binary tree. Each node has a (key, value) pair. For every node x, all keys in the left subtree of x are smaller than that in x. For every node x, all keys in the right subtree of x are greater than that in x.

  37. Binary search trees Two binary search trees representing the same set: • Average depth of a node is O(log N); maximum depth of a node is O(N)

  38. 20 10 40 6 15 30 25 2 8 The Operation Insert() 35 Insert a pair whose key is 35.

  39. 20 10 40 6 15 30 25 35 2 8 The Operation Insert() 7 Insert a pair whose key is 7.

  40. The Operation Delete() • Four cases: • No element with delete key. • Element is in a leaf. • Element is in a degree 1 node. • Element is in a degree 2 node.

  41. 20 10 40 6 15 30 18 25 35 2 8 7 Delete From A Leaf Delete a leaf element. key = 7

  42. 20 10 40 6 15 30 18 25 35 2 8 7 Delete From A Degree 1 Node Delete from a degree 1 node. key = 40

  43. 20 10 40 6 15 30 18 25 35 2 8 7 Delete From A Degree 1 Node (contd.) Delete from a degree 1 node. key = 15

  44. 20 10 40 6 15 30 18 25 35 2 8 7 Delete From A Degree 2 Node Delete from a degree 2 node. key = 10

  45. Delete From A Degree 2 Node 20 10 40 6 15 30 18 25 35 2 8 7 Replace with largest key in left subtree (or smallest in right subtree).

  46. Delete From A Degree 2 Node 20 10 40 6 15 30 18 25 35 2 8 7 Replace with largest key in left subtree (or smallest in right subtree).

  47. Delete From A Degree 2 Node 20 8 40 6 15 30 18 25 35 2 8 7 Replace with largest key in left subtree (or smallest in right subtree).

  48. Delete From A Degree 2 Node 20 8 40 6 15 30 18 25 35 2 8 7 Largest key must be in a leaf or degree 1 node.

  49. 20 10 40 6 15 30 18 25 35 2 8 7 Another Delete From A Degree 2 Node Delete from a degree 2 node. key = 20

  50. Delete From A Degree 2 Node 20 10 40 6 15 30 18 25 35 2 8 7 Replace with largest in left subtree.