priority queue implementation creating heaps
play

Priority Queue implementation Creating Heaps - PowerPoint PPT Presentation

Priority Queue implementation Creating Heaps " PriorityQueue


  1. Priority Queue implementation Creating Heaps � "��������������� ������������������������������������������� � PriorityQueue �������������������������������������� ���������������!����������0���������������# � ������������������������������������������������������� � ��������������������������#�������1������ � ����� ��������������!�"������ �����������# � ����������� � 2���!�������������3��������!��'����1���������������(��������� � $������!���������������!�������������������� �������� ���� ����������������������������14����������������� � ����!��!�������������������������������������!���������� � ���������%������������������������������ compareTo � "�������������������������� ����� ���������� ���������� • Comparable ������������������� this ������������%���� �������� • Comparator ��������������������������������%���� � �����#����������������������5����'�1�����������������(����� � &����������������������#� compareTo() ���� compare() ����������� �� ��!���'��������������������( � ��������������%�����'��������������������������������( � ��������������5�������������������������������� � )�������*������+����������!����,��--��. ������� ���� ��� ������� ���� ��/ Array-based heap Thinking about heaps ������7�����5�����8 ��������� ������������������������� � � 6 10 7 1713 9 21 19 25 6 ��!�����!��������1�� � )��������� � � / 6 : ; < = � > �� 10 7 ������������������1�9 � ������������1������������� � � ����������#�������1� 2*k 17 13 9 � ?��5�������� 21 � ��!��������#�����1� 2*k+1 ��������������1������������ 6 � 19 25 ��1�������������������� 10 7 �������������������5������ $'�(����������� � � 17 13 9 21 ����������!������������ ������������������������� � ������������������������� 25 ������������� � 19 �������������������������� @��������� � � 6 10 7 1713 9 21 19 25 ���������������������� � � � / 6 : ; < = � > �� ����������������������� � �������� ������� ���� ��6 ������� ���� ��:

  2. Adding values to heap Adding values, details (pseudocode) ����������������������������� void add(Object elt) � 6 6 ��������5������������ �� ��!��� { 10 7 10 7 // add elt to heap in myList ����������������5�� 17 13 9 17 13 9 21 21 myList.add(elt); � ������5������� ������������� 19 25 19 25 int loc = myList.size(); insert 8 6 � ��5��5�����7��8 ������� 6 10 7 while (1 < loc && ����� 10 7 17 13 elt < myList[loc/2]) 9 21 17 13 9 21 { 19 25 8 19 myList[loc] = myList[loc/2]; ����!������������������������ 6 25 8 � bubble 8 up loc = loc/2; // go to parent 10 7 ��������������5������� } 17 8 9 21 � ��������������������������� // what’s true here? 19 25 13 6 10 7 1713 9 21 19 25 � ������������������������� 6 myList.set(loc,elt); 8 7 � � / 6 : ; < = � > �� } 10 ArrayList myList 17 9 �������������������������!� 21 � 13 ���A������������'������3�����( 19 25 ������� ���� ��; ������� ���� ��< Removing minimal element Text Compression ������������������������� � 6 � B���������5����������� 10 7 ����!���������4��������� 17 13 9 21 "������������������������� � 19 25 25 � 7����8 ����������5���������� 10 7 � �����������������5�������� 17 13 9 21 C�������5��!��������������� � 19 �������� ����������������������� 7 � ��5������������'����������� 10 25 ��(���������������������� 17 13 9 21 � B����#������!� � �����������������7�� � 19 � $�����#������!� � ′ ′ 7 ′ ′ ������!8� 10 9 � ������� � ?������������������������ 17 13 25 21 � � �������������������������� � ′ ′ ′ ′ � )������������ 19 ������� ���� ��= ������� ���� ���

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend