Packagede.polygonal.ds
Classpublic class Heap
InheritanceHeap Inheritance Object
Implements Collection

A heap is a special kind of binary tree in which every node is greater than all of its children. The implementation is based on an arrayed binary tree. It can be used as an efficient priority queue.

See also

PriorityQueue


Public Properties
 PropertyDefined By
  front : *
[read-only] The heap's front item.
Heap
  _heap : Array
Heap
  maxSize : int
[read-only] The heap's maximum capacity.
Heap
  size : int
[read-only] The total number of items.
Heap
Public Methods
 MethodDefined By
  
Heap(size:int, compare:Function = null)
Initializes a new heap.
Heap
  
clear():void
Clears all items.
Heap
  
contains(obj:*):Boolean
Checks if a given item exists.
Heap
  
Dequeues and returns the front item.
Heap
  
dump():String
Prints out all elements (for debug/demo purposes).
Heap
  
enqueue(obj:*):Boolean
Enqueues some data.
Heap
  
Initializes an iterator object pointing to the first item in the collection.
Heap
  
isEmpty():Boolean
Checks if the collection is empty.
Heap
  
toArray():Array
Converts the collection into an array.
Heap
  
toString():String
Prints out a string representing the current object.
Heap
Property Detail
_heapproperty
public var _heap:Array

frontproperty 
front:*  [read-only]

The heap's front item.


Implementation
    public function get front():*
maxSizeproperty 
maxSize:int  [read-only]

The heap's maximum capacity.


Implementation
    public function get maxSize():int
sizeproperty 
size:int  [read-only]

The total number of items.


Implementation
    public function get size():int
Constructor Detail
Heap()Constructor
public function Heap(size:int, compare:Function = null)

Initializes a new heap.

Parameters
size:int — The heap's maximum capacity.
 
compare:Function (default = null) — A comparison function for sorting the heap's data. If no function is passed, the heap uses a function for comparing numbers.
Method Detail
clear()method
public function clear():void

Clears all items.

contains()method 
public function contains(obj:*):Boolean

Checks if a given item exists.

Parameters

obj:*

Returns
Boolean — True if the item is found, otherwise false.
dequeue()method 
public function dequeue():*

Dequeues and returns the front item.

Returns
* — The heap's front item or null if it is empty.
dump()method 
public function dump():String

Prints out all elements (for debug/demo purposes).

Returns
String — A human-readable representation of the structure.
enqueue()method 
public function enqueue(obj:*):Boolean

Enqueues some data.

Parameters

obj:* — The data to enqueue.

Returns
Boolean — False if the queue is full, otherwise true.
getIterator()method 
public function getIterator():Iterator

Initializes an iterator object pointing to the first item in the collection.

Returns
Iterator — An iterator object.
isEmpty()method 
public function isEmpty():Boolean

Checks if the collection is empty.

Returns
Boolean — True if empty, otherwise false.
toArray()method 
public function toArray():Array

Converts the collection into an array.

Returns
Array — An array.
toString()method 
public function toString():String

Prints out a string representing the current object.

Returns
String — A string representing the current object.