Heap as array: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
No edit summary |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
[[Category:Videos]] | |||
{{#ev:youtube|https://www.youtube.com/watch?v=j-r4YOPFp7E|500|right||frame}} | |||
[[Category:Data Structures]] | [[Category:Data Structures]] | ||
==General information== | ==General information== | ||
Line 11: | Line 13: | ||
## a component named <math>ID</math>, which is a [[Numbers#natural numbers|natural number]] and ranges from <math>1,...,N_\text{max}</math>. | ## a component named <math>ID</math>, which is a [[Numbers#natural numbers|natural number]] and ranges from <math>1,...,N_\text{max}</math>. | ||
# There is an array <math>TheHeap</math> with index range <math>1,...,N_\text{max}</math> and component type heap item. The '''currently stored items''' are <math>TheHeap[1],...,TheHeap[N]</math>. | # There is an array <math>TheHeap</math> with index range <math>1,...,N_\text{max}</math> and component type heap item. The '''currently stored items''' are <math>TheHeap[1],...,TheHeap[N]</math>. | ||
# There is an [[Index handler|index handler]] <math>IH</math> with maximum size <math>N_\text{max}</math> and value type <math>\{1,\ldots,N_\text{max}\}</math>. For each used index <math>i\in\{1,...,N_\text{max}\}</math>, the associated value is the position of one of the <math>N</math> heap items in <math>TheHeap</math>. As long as this heap item is stored, <math>i</math> is permanently associated with this heap item (and can hence be used to locate and access this heap item at any time). The correspondence between the heap items and these numbers <math>i</math> is one-to-one. | |||
# There is an [[ | |||
# '''Heap property:''' For each <math>i\in\{2,...,N\}</math>, it is <math>TheHeap[i].key\ge TheHeap[\lfloor i/2 \rfloor ].key.</math>. | # '''Heap property:''' For each <math>i\in\{2,...,N\}</math>, it is <math>TheHeap[i].key\ge TheHeap[\lfloor i/2 \rfloor ].key.</math>. | ||
The heap property may be viewed as follows: Imagine a [[Directed Tree|binary tree]] of <math>N</math> nodes such that each height level except for the last one is full, and the last one is filled from left to right. The nodes have pairwise different indexes from <math>\{ 1,...,N \}</math>: | |||
The heap property may be viewed as follows: Imagine a [[binary tree]] of <math>N</math> nodes such that each height level except for the last one is full, and the last one is filled from left to right. The nodes have pairwise different indexes from <math>\{ 1,...,N \}</math>: | |||
# The root is indexed <math>1</math>. | # The root is indexed <math>1</math>. | ||
# For a node with <math>i</math>, the left child (if existing) has index <math>2*i</math>, and the right child (if existing) has index <math>2*i+1</math>. | # For a node with <math>i</math>, the left child (if existing) has index <math>2*i</math>, and the right child (if existing) has index <math>2*i+1</math>. |
Latest revision as of 23:12, 19 June 2015
General information
Abstract Data Structure: Bounded priority queue
Implementation Invariant:
- For each object of "Heap as array", there is
- a specific maximum number of items
, which is constant throughout the object's lifetime. - a current number of items
, which is dynamically changing, but it is at any time.
- a specific maximum number of items
- There is an internal type heap item with two components:
- a component named
of type - a component named
, which is a natural number and ranges from .
- a component named
- There is an array
with index range and component type heap item. The currently stored items are . - There is an index handler
with maximum size and value type . For each used index , the associated value is the position of one of the heap items in . As long as this heap item is stored, is permanently associated with this heap item (and can hence be used to locate and access this heap item at any time). The correspondence between the heap items and these numbers is one-to-one. - Heap property: For each
, it is .
The heap property may be viewed as follows: Imagine a binary tree of
- The root is indexed
. - For a node with
, the left child (if existing) has index , and the right child (if existing) has index .
Now associate the node with index
In particular, the minimum key is associated with the root.
Remark
The implementations of the methods Bounded priority queue: number and Bounded priority queue: find minimum are trivial and, hence, left out here.