1 / 8

Fundamentals of Python: From First Programs Through Data Structures

Learn about the array-based representation of binary trees, its practicality, efficiency, and implementation for complete binary trees and heaps. Discover how heaps are implemented using arrays.

hazelmurphy
Download Presentation

Fundamentals of Python: From First Programs Through Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Fundamentals of Python:From First Programs Through Data Structures Chapter 18 Hierarchical Collections: Trees

  2. An Array Implementation of Binary Trees • An array-based implementation of a binary tree is difficult to define and practical only in some cases • For complete binary trees, there is an elegant and efficient array-based representation • Elements are stored by level • The array representation of a binary tree is pretty rare and is used mainly to implement a heap Fundamentals of Python: From First Programs Through Data Structures

  3. An Array Implementation of Binary Trees (continued) Fundamentals of Python: From First Programs Through Data Structures

  4. An Array Implementation of Binary Trees (continued) Fundamentals of Python: From First Programs Through Data Structures

  5. An Array Implementation of Binary Trees (continued) Fundamentals of Python: From First Programs Through Data Structures

  6. Implementing Heaps Fundamentals of Python: From First Programs Through Data Structures

  7. Implementing Heaps (continued) • At most, log2n comparisons must be made to walk up the tree from the bottom, so add is O(log n) • Method may trigger a doubling in the array size • O(n), but amortized over all additions, it is O(1) Fundamentals of Python: From First Programs Through Data Structures

  8. Using a Heap to Implement a Priority Queue • In Ch15, we implemented a priority queue with a sorted linked list; alternatively, we can use a heap Fundamentals of Python: From First Programs Through Data Structures

More Related