SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Downloaden Sie, um offline zu lesen
What is a Heap Data Structure?
• A Heap data structure is a binary tree with the following properties :

  1. It is a complete binary tree; that is, each level of the tree is completely filled, except possibly
     the bottom level. At this level, it is filled from left to right.

  2. It satisfies the heap-order property: The data item stored in each node is greater
     than or equal to the data items stored in its children.

• Examples:
(a)                                (b)                                      (c)
              9                                9                                          9


          8           4                   8              4                            6           4

      6       2   3                   6              2       3                    8       2   3


• In the above examples only (a) is a heap. (b) is not a heap as it is not complete and (c) is complete
  but does not satisfy the second property defined for heaps.

                                   Programming and Data Structures                                    1
Heap as an Array
•   We could implement heaps using a linked list like structure, like we did with
    binary trees but in this instance it is actually easier to implement heaps using
    arrays.

•   We simply number the nodes in the heap from top to bottom, numbering the nodes
    on each level from left to right and store the ith node in the ith location of the
    array. (of course remembering that array indexing in Java starts at zero).
                              1


                     2                          3



              4                   5     6                7




                              Programming and Data Structures                          2
Heap as an Array (2)
• An array A that represents a heap is an array with two attributes
   – length, the number of elements in the array
   – heap-size, the number of heap elements stored in the array
• Viewed as a binary tree and as an array :

                                                          1
                                                              16
                                        2                                           3
                                            14                                 10
                          4                               5            6                     7
                              8                               7            9             3
                 8                      9        10
                     2              4                 1

                     1        2    3        4     5       6        7   8       9    10
                     16       14   10       8     7       9        3   2       4    1


• The root of the tree is stored at A[0], its left-child at A[1], its right child at A[2]
etc.
                                            Programming and Data Structures                      3
Accessing the Heap Values
• Given the index i of a node, the indices of its parent Parent(i), left-child
  LeftChild(i) and right child RightChild(i) can be computed simply :

    Parent(i)                LeftChild(i)                     RightChild(i)
      return i/2               return 2i                       return 2i+1

• Heaps must also satisfy the heap property for every node, i, other than the
  root.
      A[Parent(i)] ≥ A[i]

• Therefore, the largest element in a heap is stored at the root, and the
  subtrees rooted at a node contain smaller values than does the node itself.



                            Programming and Data Structures                      4
Heap Operations
• The height of a node in a tree is the number of
  edges on the longest simple downward path from
  the node to a leaf. (i.e. maximum depth from that
  node – Remember?)

• The height of an n-element heap based on a
  binary tree is lg n

• The basic operations on heaps run in time at
  most proportional to the height of the tree and
  thus take O(lg n) time.
                 Programming and Data Structures      5
Maintaining the Heap Property
• One of the more basic heap operations is converting a complete binary
  tree to a heap.

• Such an operation is called “Heapify”.

• Its inputs are an array A and an index i into the array.

• When Heapify is called, it is assumed that the binary trees rooted at
  LeftChild(i) and RightChild(i) are heaps, but that A[i] may be smaller
  than its children, thus violating the 2nd heap property.

• The function of Heapify is to let the value at A[i] “float down” in the
  heap so that the subtree rooted at index i becomes a heap.

• The action required from Heapify is as follows:
                          Programming and Data Structures                   6
Example of Heapify
                                                      1
                                                       16                                          • Subtree to be heapified is marked by the shaded
                             2                                                     3
                         i       4                    5                        10              7
                                                                                                     triangle.
        4                                                             6
                14                                        7               9                    3   • Node i if smaller than its children is swapped with
8                                9        10                                                         the greater of its two children – in this case the left
    2                    8                        1                                                  child with value 14 is swapped for value 4 in node I.

                                               1
                                                  16                                               • If the next subtree down (again marked with the
                         2                                                     3
                             14                                           10                         shaded triangle region) is not a heap, we “percolate”
            4                                      5              6                    7             down to the next subtree level and find the larger
        i   4                                         7           9                    3             of the two children again to swap with current root
8                            9       10                                                              node.
    2                8                     1

                                              1
                                               16                                                   • We keep “percolating” down until either the
                         2                                                     3
                         14                                               10                          subtree conforms to the heap properties or a leaf
    4                                         5               6                            7          has been reached.
            8                                     7               9                    3
8                        9           10
2                    4                    1
                                                                          Programming and Data Structures                                         7
The Heapify Algorithm
Begin
Heapify(A[], i)          // Heapify takes in our heap and index of current root node of subtree to be heapified
{
   left = LeftChild(i);                       // index of left child
   right = RightChild(i);                     // index of right child

   if left ≤ A.heap-size AND A[left] > A[i]      { // if still within bounds AND left child
              largest = left;                      // greather than parent – remember largest as left child
   }
   else {
           largest = i;                               // else parent still has largest value for now
   }

   if right ≤ A.heap-size AND A[right] > A[largest] { // if still within bounds AND right child
           largest = right                           // greather than parent – remember largest as right child
   }
   if largest NOT EQUAL i {                      // if parent does not hold largest value
          swap A[i] and A[largest]               // swap parent with child that does have largest value
          Heapify(A, largest)                    // Percolate down and Heapify next subtree
   }
}
End

                                     Programming and Data Structures                                      8
The Heapify Algorithm
• At each step, the index of the largest of the elements A[i], A[Left(i)],
  and A[Right(i)] is stored in the variable largest.

• If A[i] is largest, then the subtree rooted at node i is a heap and the
  procedure ends.

• Otherwise, one of the two children has the largest element, and A[i] is
  swapped with A[largest], which causes node i and its children to satisfy
  the heap property.

• The node largest, however, now has the original value A[i], and thus
  the subtree rooted at largest may violate the heap property.

• Therefore, Heapify must be called recursively on that subtree.


                          Programming and Data Structures                    9
Analysis of Heapify
• The running time of Heapify on a subtree of size n rooted at a given
  node i is the O(1) time to fix up the relationships among the elements
  A[i], A[Left(i)] and A[Right(i)], plus the time to run Heapify on a
  subtree rooted at one of the children of node i.

• The subtrees can have size of at most 2n/3, and the running time of
  Heapify can therefore be described by the recurrence,
                T(n) ≤ T(2n/3) + O(1)


• Using either the iteration method or the master’s theorem we can find
  that the solution to this works out as:
                  T(n) = O(log n)



                        Programming and Data Structures                 10
Building a Heap
•   For the general case of converting a complete binary tree to a heap, we begin at the last
    node that is not a leaf, apply the “percolate down” routine to convert the subtree rooted at
    this current root node to a heap.

•   We then move onto the preceding node and “percolate down” that subtree.

•   We continue on in this manner, working up the tree until we reach the root of the given tree.

•   We use the Heapify procedure here in a bottom up manner. This means also means that to
    convert an array A[1..n], where n = A.length, into a heap we can apply the same procedure
    as described above.

•   Now, since the subarray A[(n/2 + 1)..n] are all leaves of the tree, each is a 1-element heap.

•   The procedure “Build-Heap” goes through the remaining nodes and runs Heapify on each.

•   The order of processing guarantees that the subtrees rooted at children of node i are heaps
    before Heapify is run at that node.


                                 Programming and Data Structures                              11
Build Heap Algorithm
Begin:
Build-Heap (A[])      {                      // takes in an array to be heapified
  A.heap-size = A.length;                    // heap size is number of elements in the array
  for i = A.length/2 downto 1 // starting at first subtree up to root full tree
  {
     Heapify(A, i);             // Heapify the current subtree
  }
}
End

•   Each call to Heapify costs O(lg n) time, and there are O(n) such calls.
•   Thus, the running time is at most O(n lg n)
•   A more complex analysis, however, gives us a tighter upper bound of O(n).
•   Hence, we can build a heap from an unordered array in linear time.

•   Lets have a look at a visual example of this operation (have a look at the next slide).
•   The current subtrees that are being worked on are marked inside a shaded triangle.
•   Any swapping that goes on is marked with a thicker edge.

                              Programming and Data Structures                                 12
Example of Build Heap
                  A            4            1        3        2            1 6      9           1 0               1 4               8               7

                                            1                                                                                       1
                                                4                                                                                           4
                       2                                           3                                                 2                                               3
                               1                              3                                                           1                                      3
              4                          5           6                          7                  4                                    5           6                         7
                   2                    i 16             9                 10                   i 2                                      16                 9                10
      8                    9 10                                                         8                             9 10
          14           8                7                                                   14                   8                  7
 (a)                                                                                                                                                                             (b)
                                        1                                                                                          1
                                            4                                                                     2                     4
                       2 1                                     3                                                                                                     3
                                                         3                                                   i
                                            5        6 i                 7                                            1
                                                                                                                                    5               6
                                                                                                                                                                10
                                                                                                                                                                                  7
              4 14                          16        9                10                       4                                   16                  9                    3
                                                                                                    14
  8                      9 10 7                                                         8                         9 10 7
       2               8                                                                    2                8
(c)                                                                                                                                                                              (d)
                                                 1                                                                                 1
                                        i 4                                                                                        16
                                                               3                                         2                                                       3
                           216                               10                                                  14                                         10
                                        5            6                 7                                                           5            6                    7
              4                             7            9                 3                4 8                                     7               9                    3
                  14                                                                                                      10
  8                        9       10                                               8                        9                 1
          2            8                1                                               2                4
                                                                                                                                                                                 (f)
(e)
                                                             Programming and Data Structures                                                                             13
Heap Practical Example :Priority Queue
• Many applications require that we process records with keys/priorities
  in order, but not necessarily in full sorted order. (e.g. CPU process
  scheduling).

• Items in a priority queue are not processed strictly in order of entry
  into the queue

• Items can be placed on the queue at any time, but processing always
  takes the item with the largest key/ priority

• The priority queue is a generalised queue Abstract Data Structure
  (ADT)

• In fact, the priority queue is a proper generalisation of the stact and the
  queue ADTs, as we can implement either with priority queues, using
  appropriate priorities
                          Programming and Data Structures                  14
Priority Queue - Definition
•   A priority queue is a data structure of items with keys that support two basic
    operations : insert a new item, and delete the item with the largest key.

       6 5 5 3 2 1
       6 6 5 5 3 2 1            Add item with priority 6
       6 6 5 5 4 3 2 1          Add item with priority 4
       6 5 5 4 3 2 1             Remove item with highest priority

•   Applications of priority queues include:
     – Simulation Systems, where events need to be processed chronologically.
     – Job scheduling in computer systems.

•   The Heap Data Structure is an efficient structure for implementing a simple
    priority queue.

•   In practice, priority queues are more complex than the simple definition above.
                             Programming and Data Structures                         15
Priority Queue
• One of the reasons that priority queue implementations are so useful is
  their flexibility

• For that reason any implementation will need to support some of the
  following operations
   – Construct a priority queue from N given items
   – Insert a new item
   – Delete the maximum item
   – Change the priority of an arbitrary specified item
   – Delete an arbitrary specified item

• We take “maximum” to mean “any record with the largest key value”.



                        Programming and Data Structures                 16
Other Heap Operations
•   Other important heap operations include :
    - Heap Insert (inserting an element into a heap)
    - Heap Extract Max/Min (extracting an Max/Min element from a heap)
    - Heap Delete (remove an element from a heap)
    - And of course…. HeapSort - O(N lg N) sorting algorithm
      + very basic idea :
           * swap and remove root value with final position element value
           * decrement size of heap and Heapify from position one (i.e. new root) again
             to restore new heap
           * Keep doing this while there is more nodes. This eventually gives us a
             sorted list.

•   Unfortunately, due to time constraints, we cannot examine these operations in any
    more detail.


                              Programming and Data Structures                     17

Weitere ähnliche Inhalte

Was ist angesagt?

Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeManishPrajapati78
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure shameen khan
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithmsGanesh Solanke
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithmShubham Dwivedi
 
Data Structure and Algorithms Hashing
Data Structure and Algorithms HashingData Structure and Algorithms Hashing
Data Structure and Algorithms HashingManishPrajapati78
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSGokul Hari
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & DeletionAfaq Mansoor Khan
 
Hash table
Hash tableHash table
Hash tableRajendran
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort AlgorithmLemia Algmri
 
Sparse matrix and its representation data structure
Sparse matrix and its representation data structureSparse matrix and its representation data structure
Sparse matrix and its representation data structureVardhil Patel
 
Insertion sort bubble sort selection sort
Insertion sort bubble sort  selection sortInsertion sort bubble sort  selection sort
Insertion sort bubble sort selection sortUmmar Hayat
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithmMohd Arif
 
Complexity analysis - The Big O Notation
Complexity analysis - The Big O NotationComplexity analysis - The Big O Notation
Complexity analysis - The Big O NotationJawad Khan
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sortMadhu Bala
 
Heap sort
Heap sortHeap sort
Heap sortMohd Arif
 

Was ist angesagt? (20)

Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
Data Structure and Algorithms Hashing
Data Structure and Algorithms HashingData Structure and Algorithms Hashing
Data Structure and Algorithms Hashing
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
Hashing PPT
Hashing PPTHashing PPT
Hashing PPT
 
Hash table
Hash tableHash table
Hash table
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Red black tree
Red black treeRed black tree
Red black tree
 
Sparse matrix and its representation data structure
Sparse matrix and its representation data structureSparse matrix and its representation data structure
Sparse matrix and its representation data structure
 
single linked list
single linked listsingle linked list
single linked list
 
Insertion sort bubble sort selection sort
Insertion sort bubble sort  selection sortInsertion sort bubble sort  selection sort
Insertion sort bubble sort selection sort
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Complexity analysis - The Big O Notation
Complexity analysis - The Big O NotationComplexity analysis - The Big O Notation
Complexity analysis - The Big O Notation
 
Linked List
Linked ListLinked List
Linked List
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
Heap sort
Heap sortHeap sort
Heap sort
 
Lecture24
Lecture24Lecture24
Lecture24
 

Ähnlich wie Heaps

DocValues aka. Column Stride Fields in Lucene 4.0 - By Willnauer Simon
DocValues aka. Column Stride Fields in Lucene 4.0 - By Willnauer SimonDocValues aka. Column Stride Fields in Lucene 4.0 - By Willnauer Simon
DocValues aka. Column Stride Fields in Lucene 4.0 - By Willnauer Simonlucenerevolution
 
Column Stride Fields aka. DocValues
Column Stride Fields aka. DocValuesColumn Stride Fields aka. DocValues
Column Stride Fields aka. DocValuesLucidworks (Archived)
 
Column Stride Fields aka. DocValues
Column Stride Fields aka. DocValues Column Stride Fields aka. DocValues
Column Stride Fields aka. DocValues Lucidworks (Archived)
 
Sienna 6 bst
Sienna 6 bstSienna 6 bst
Sienna 6 bstchidabdu
 
Unit 8
Unit 8Unit 8
Unit 8mrecedu
 
05 heap 20161110_jintaeks
05 heap 20161110_jintaeks05 heap 20161110_jintaeks
05 heap 20161110_jintaeksJinTaek Seo
 
RTree Spatial Indexing with MongoDB - MongoDC
RTree Spatial Indexing with MongoDB - MongoDC RTree Spatial Indexing with MongoDB - MongoDC
RTree Spatial Indexing with MongoDB - MongoDC Nicholas Knize, Ph.D., GISP
 
3.7 heap sort
3.7 heap sort3.7 heap sort
3.7 heap sortKrish_ver2
 
Technical aptitude questions
Technical aptitude questionsTechnical aptitude questions
Technical aptitude questionssadiqkhanpathan
 
HeapSort
HeapSortHeapSort
HeapSortTuqa Rmahi
 
Data Structures Aptitude
Data Structures AptitudeData Structures Aptitude
Data Structures AptitudeSriram Raj
 
Heap Data Structure
 Heap Data Structure Heap Data Structure
Heap Data StructureSaumya Som
 
The Back Propagation Learning Algorithm
The Back Propagation Learning AlgorithmThe Back Propagation Learning Algorithm
The Back Propagation Learning AlgorithmESCOM
 
Data structure-questions
Data structure-questionsData structure-questions
Data structure-questionsShekhar Chander
 
Week 8 (trees)
Week 8 (trees)Week 8 (trees)
Week 8 (trees)amna izzat
 

Ähnlich wie Heaps (20)

Heapsort
HeapsortHeapsort
Heapsort
 
DocValues aka. Column Stride Fields in Lucene 4.0 - By Willnauer Simon
DocValues aka. Column Stride Fields in Lucene 4.0 - By Willnauer SimonDocValues aka. Column Stride Fields in Lucene 4.0 - By Willnauer Simon
DocValues aka. Column Stride Fields in Lucene 4.0 - By Willnauer Simon
 
Column Stride Fields aka. DocValues
Column Stride Fields aka. DocValuesColumn Stride Fields aka. DocValues
Column Stride Fields aka. DocValues
 
Column Stride Fields aka. DocValues
Column Stride Fields aka. DocValues Column Stride Fields aka. DocValues
Column Stride Fields aka. DocValues
 
Sienna 6 bst
Sienna 6 bstSienna 6 bst
Sienna 6 bst
 
Unit 8
Unit 8Unit 8
Unit 8
 
05 heap 20161110_jintaeks
05 heap 20161110_jintaeks05 heap 20161110_jintaeks
05 heap 20161110_jintaeks
 
Heapsort
HeapsortHeapsort
Heapsort
 
Heapsort
HeapsortHeapsort
Heapsort
 
RTree Spatial Indexing with MongoDB - MongoDC
RTree Spatial Indexing with MongoDB - MongoDC RTree Spatial Indexing with MongoDB - MongoDC
RTree Spatial Indexing with MongoDB - MongoDC
 
Heapsort
HeapsortHeapsort
Heapsort
 
3.7 heap sort
3.7 heap sort3.7 heap sort
3.7 heap sort
 
Heap sort
Heap sort Heap sort
Heap sort
 
Technical aptitude questions
Technical aptitude questionsTechnical aptitude questions
Technical aptitude questions
 
HeapSort
HeapSortHeapSort
HeapSort
 
Data Structures Aptitude
Data Structures AptitudeData Structures Aptitude
Data Structures Aptitude
 
Heap Data Structure
 Heap Data Structure Heap Data Structure
Heap Data Structure
 
The Back Propagation Learning Algorithm
The Back Propagation Learning AlgorithmThe Back Propagation Learning Algorithm
The Back Propagation Learning Algorithm
 
Data structure-questions
Data structure-questionsData structure-questions
Data structure-questions
 
Week 8 (trees)
Week 8 (trees)Week 8 (trees)
Week 8 (trees)
 

KĂźrzlich hochgeladen

SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 

KĂźrzlich hochgeladen (20)

SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 

Heaps

  • 1. What is a Heap Data Structure? • A Heap data structure is a binary tree with the following properties : 1. It is a complete binary tree; that is, each level of the tree is completely filled, except possibly the bottom level. At this level, it is filled from left to right. 2. It satisfies the heap-order property: The data item stored in each node is greater than or equal to the data items stored in its children. • Examples: (a) (b) (c) 9 9 9 8 4 8 4 6 4 6 2 3 6 2 3 8 2 3 • In the above examples only (a) is a heap. (b) is not a heap as it is not complete and (c) is complete but does not satisfy the second property defined for heaps. Programming and Data Structures 1
  • 2. Heap as an Array • We could implement heaps using a linked list like structure, like we did with binary trees but in this instance it is actually easier to implement heaps using arrays. • We simply number the nodes in the heap from top to bottom, numbering the nodes on each level from left to right and store the ith node in the ith location of the array. (of course remembering that array indexing in Java starts at zero). 1 2 3 4 5 6 7 Programming and Data Structures 2
  • 3. Heap as an Array (2) • An array A that represents a heap is an array with two attributes – length, the number of elements in the array – heap-size, the number of heap elements stored in the array • Viewed as a binary tree and as an array : 1 16 2 3 14 10 4 5 6 7 8 7 9 3 8 9 10 2 4 1 1 2 3 4 5 6 7 8 9 10 16 14 10 8 7 9 3 2 4 1 • The root of the tree is stored at A[0], its left-child at A[1], its right child at A[2] etc. Programming and Data Structures 3
  • 4. Accessing the Heap Values • Given the index i of a node, the indices of its parent Parent(i), left-child LeftChild(i) and right child RightChild(i) can be computed simply : Parent(i) LeftChild(i) RightChild(i) return i/2 return 2i return 2i+1 • Heaps must also satisfy the heap property for every node, i, other than the root. A[Parent(i)] ≥ A[i] • Therefore, the largest element in a heap is stored at the root, and the subtrees rooted at a node contain smaller values than does the node itself. Programming and Data Structures 4
  • 5. Heap Operations • The height of a node in a tree is the number of edges on the longest simple downward path from the node to a leaf. (i.e. maximum depth from that node – Remember?) • The height of an n-element heap based on a binary tree is lg n • The basic operations on heaps run in time at most proportional to the height of the tree and thus take O(lg n) time. Programming and Data Structures 5
  • 6. Maintaining the Heap Property • One of the more basic heap operations is converting a complete binary tree to a heap. • Such an operation is called “Heapify”. • Its inputs are an array A and an index i into the array. • When Heapify is called, it is assumed that the binary trees rooted at LeftChild(i) and RightChild(i) are heaps, but that A[i] may be smaller than its children, thus violating the 2nd heap property. • The function of Heapify is to let the value at A[i] “float down” in the heap so that the subtree rooted at index i becomes a heap. • The action required from Heapify is as follows: Programming and Data Structures 6
  • 7. Example of Heapify 1 16 • Subtree to be heapified is marked by the shaded 2 3 i 4 5 10 7 triangle. 4 6 14 7 9 3 • Node i if smaller than its children is swapped with 8 9 10 the greater of its two children – in this case the left 2 8 1 child with value 14 is swapped for value 4 in node I. 1 16 • If the next subtree down (again marked with the 2 3 14 10 shaded triangle region) is not a heap, we “percolate” 4 5 6 7 down to the next subtree level and find the larger i 4 7 9 3 of the two children again to swap with current root 8 9 10 node. 2 8 1 1 16 • We keep “percolating” down until either the 2 3 14 10 subtree conforms to the heap properties or a leaf 4 5 6 7 has been reached. 8 7 9 3 8 9 10 2 4 1 Programming and Data Structures 7
  • 8. The Heapify Algorithm Begin Heapify(A[], i) // Heapify takes in our heap and index of current root node of subtree to be heapified { left = LeftChild(i); // index of left child right = RightChild(i); // index of right child if left ≤ A.heap-size AND A[left] > A[i] { // if still within bounds AND left child largest = left; // greather than parent – remember largest as left child } else { largest = i; // else parent still has largest value for now } if right ≤ A.heap-size AND A[right] > A[largest] { // if still within bounds AND right child largest = right // greather than parent – remember largest as right child } if largest NOT EQUAL i { // if parent does not hold largest value swap A[i] and A[largest] // swap parent with child that does have largest value Heapify(A, largest) // Percolate down and Heapify next subtree } } End Programming and Data Structures 8
  • 9. The Heapify Algorithm • At each step, the index of the largest of the elements A[i], A[Left(i)], and A[Right(i)] is stored in the variable largest. • If A[i] is largest, then the subtree rooted at node i is a heap and the procedure ends. • Otherwise, one of the two children has the largest element, and A[i] is swapped with A[largest], which causes node i and its children to satisfy the heap property. • The node largest, however, now has the original value A[i], and thus the subtree rooted at largest may violate the heap property. • Therefore, Heapify must be called recursively on that subtree. Programming and Data Structures 9
  • 10. Analysis of Heapify • The running time of Heapify on a subtree of size n rooted at a given node i is the O(1) time to fix up the relationships among the elements A[i], A[Left(i)] and A[Right(i)], plus the time to run Heapify on a subtree rooted at one of the children of node i. • The subtrees can have size of at most 2n/3, and the running time of Heapify can therefore be described by the recurrence, T(n) ≤ T(2n/3) + O(1) • Using either the iteration method or the master’s theorem we can find that the solution to this works out as: T(n) = O(log n) Programming and Data Structures 10
  • 11. Building a Heap • For the general case of converting a complete binary tree to a heap, we begin at the last node that is not a leaf, apply the “percolate down” routine to convert the subtree rooted at this current root node to a heap. • We then move onto the preceding node and “percolate down” that subtree. • We continue on in this manner, working up the tree until we reach the root of the given tree. • We use the Heapify procedure here in a bottom up manner. This means also means that to convert an array A[1..n], where n = A.length, into a heap we can apply the same procedure as described above. • Now, since the subarray A[(n/2 + 1)..n] are all leaves of the tree, each is a 1-element heap. • The procedure “Build-Heap” goes through the remaining nodes and runs Heapify on each. • The order of processing guarantees that the subtrees rooted at children of node i are heaps before Heapify is run at that node. Programming and Data Structures 11
  • 12. Build Heap Algorithm Begin: Build-Heap (A[]) { // takes in an array to be heapified A.heap-size = A.length; // heap size is number of elements in the array for i = A.length/2 downto 1 // starting at first subtree up to root full tree { Heapify(A, i); // Heapify the current subtree } } End • Each call to Heapify costs O(lg n) time, and there are O(n) such calls. • Thus, the running time is at most O(n lg n) • A more complex analysis, however, gives us a tighter upper bound of O(n). • Hence, we can build a heap from an unordered array in linear time. • Lets have a look at a visual example of this operation (have a look at the next slide). • The current subtrees that are being worked on are marked inside a shaded triangle. • Any swapping that goes on is marked with a thicker edge. Programming and Data Structures 12
  • 13. Example of Build Heap A 4 1 3 2 1 6 9 1 0 1 4 8 7 1 1 4 4 2 3 2 3 1 3 1 3 4 5 6 7 4 5 6 7 2 i 16 9 10 i 2 16 9 10 8 9 10 8 9 10 14 8 7 14 8 7 (a) (b) 1 1 4 2 4 2 1 3 3 3 i 5 6 i 7 1 5 6 10 7 4 14 16 9 10 4 16 9 3 14 8 9 10 7 8 9 10 7 2 8 2 8 (c) (d) 1 1 i 4 16 3 2 3 216 10 14 10 5 6 7 5 6 7 4 7 9 3 4 8 7 9 3 14 10 8 9 10 8 9 1 2 8 1 2 4 (f) (e) Programming and Data Structures 13
  • 14. Heap Practical Example :Priority Queue • Many applications require that we process records with keys/priorities in order, but not necessarily in full sorted order. (e.g. CPU process scheduling). • Items in a priority queue are not processed strictly in order of entry into the queue • Items can be placed on the queue at any time, but processing always takes the item with the largest key/ priority • The priority queue is a generalised queue Abstract Data Structure (ADT) • In fact, the priority queue is a proper generalisation of the stact and the queue ADTs, as we can implement either with priority queues, using appropriate priorities Programming and Data Structures 14
  • 15. Priority Queue - Definition • A priority queue is a data structure of items with keys that support two basic operations : insert a new item, and delete the item with the largest key. 6 5 5 3 2 1 6 6 5 5 3 2 1 Add item with priority 6 6 6 5 5 4 3 2 1 Add item with priority 4 6 5 5 4 3 2 1 Remove item with highest priority • Applications of priority queues include: – Simulation Systems, where events need to be processed chronologically. – Job scheduling in computer systems. • The Heap Data Structure is an efficient structure for implementing a simple priority queue. • In practice, priority queues are more complex than the simple definition above. Programming and Data Structures 15
  • 16. Priority Queue • One of the reasons that priority queue implementations are so useful is their flexibility • For that reason any implementation will need to support some of the following operations – Construct a priority queue from N given items – Insert a new item – Delete the maximum item – Change the priority of an arbitrary specified item – Delete an arbitrary specified item • We take “maximum” to mean “any record with the largest key value”. Programming and Data Structures 16
  • 17. Other Heap Operations • Other important heap operations include : - Heap Insert (inserting an element into a heap) - Heap Extract Max/Min (extracting an Max/Min element from a heap) - Heap Delete (remove an element from a heap) - And of course…. HeapSort - O(N lg N) sorting algorithm + very basic idea : * swap and remove root value with final position element value * decrement size of heap and Heapify from position one (i.e. new root) again to restore new heap * Keep doing this while there is more nodes. This eventually gives us a sorted list. • Unfortunately, due to time constraints, we cannot examine these operations in any more detail. Programming and Data Structures 17