2008-04-26 00:12:44 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								USING:  heaps.private  help.markup  help.syntax  kernel  math  assocs  
						 
					
						
							
								
									
										
										
										
											2008-10-01 19:10:57 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								math.order quotations ;
 
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 02:42:37 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								IN:  heaps  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								ARTICLE: "heaps"  "Heaps" 
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 11:01:11 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								"A heap is an implementation of a "  { $emphasis "priority queue"  } ", which is a structure that maintains a sorted set of elements. The key property is that insertion of an arbitrary element and removal of the first element (determined by order) is performed in O(log n) time."  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								$nl
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:10:26 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								"Heap elements are key/value pairs and are compared using the "  { $link <=> } " generic word on the first element of the pair."  
						 
					
						
							
								
									
										
										
										
											2007-11-05 11:01:11 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								$nl
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"There are two classes of heaps. Min-heaps sort their elements so that the minimum element is first:"  
						 
					
						
							
								
									
										
										
										
											2009-10-01 15:56:36 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $subsections
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    min-heap
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    min-heap?
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    <min-heap>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 11:01:11 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								"Max-heaps sort their elements so that the maximum element is first:"  
						 
					
						
							
								
									
										
										
										
											2009-10-01 15:56:36 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $subsections
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    max-heap
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    max-heap?
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    <max-heap>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 11:01:11 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								"Both obey a protocol."  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								$nl
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"Queries:"  
						 
					
						
							
								
									
										
										
										
											2009-10-01 15:56:36 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $subsections
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    heap-empty?
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    heap-size
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    heap-peek
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 11:01:11 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								"Insertion:"  
						 
					
						
							
								
									
										
										
										
											2009-10-01 15:56:36 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $subsections
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    heap-push
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    heap-push*
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    heap-push-all
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 11:01:11 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								"Removal:"  
						 
					
						
							
								
									
										
										
										
											2009-10-01 15:56:36 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $subsections
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    heap-pop*
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    heap-pop
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    heap-delete
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2008-10-01 19:10:57 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								"Processing heaps:"  
						 
					
						
							
								
									
										
										
										
											2009-10-01 15:56:36 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $subsections slurp-heap } ;
 
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 02:42:37 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 11:01:11 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								ABOUT: "heaps" 
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 02:42:37 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  <min-heap>  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $values { "min-heap"  min-heap } }
							 
						 
					
						
							
								
									
										
										
										
											2008-02-21 18:07:26 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{ $description "Create a new "  { $link min-heap } "."  } ;
 
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 02:42:37 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  <max-heap>  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $values { "max-heap"  max-heap } }
							 
						 
					
						
							
								
									
										
										
										
											2008-02-21 18:07:26 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{ $description "Create a new "  { $link max-heap } "."  } ;
 
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 02:42:37 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  heap-push  
						 
					
						
							
								
									
										
										
										
											2009-10-21 18:40:43 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $values { "value"  object  } { "key"  "a comparable object"  } { "heap"  "a heap"  } }
							 
						 
					
						
							
								
									
										
										
										
											2008-02-21 18:07:26 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{ $description "Push a pair onto a heap. The key must be comparable with all other keys by the "  { $link <=> } " generic word."  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $side-effects "heap"  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  heap-push*  
						 
					
						
							
								
									
										
										
										
											2009-10-21 18:40:43 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $values { "value"  object  } { "key"  "a comparable object"  } { "heap"  "a heap"  } { "entry"  entry } }
							 
						 
					
						
							
								
									
										
										
										
											2008-02-21 18:07:26 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{ $description "Push a pair onto a heap, and output an entry which may later be passed to "  { $link heap-delete } "."  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $side-effects "heap"  } ;
 
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 02:42:37 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  heap-push-all  
						 
					
						
							
								
									
										
										
										
											2008-02-21 20:12:37 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{ $values { "assoc"  assoc  } { "heap"  "a heap"  } }
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:48:22 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{ $description "Push every key/value pair of an assoc onto a heap."  }
							 
						 
					
						
							
								
									
										
										
										
											2008-02-21 18:07:26 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{ $side-effects "heap"  } ;
 
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 02:42:37 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  heap-peek  
						 
					
						
							
								
									
										
										
										
											2009-10-21 18:40:43 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $values { "heap"  "a heap"  } { "value"  object  } { "key"  object  } }
							 
						 
					
						
							
								
									
										
										
										
											2008-02-21 18:07:26 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{ $description "Output the first element in the heap, leaving it in the heap."  } ;
 
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 02:42:37 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  heap-pop*  
						 
					
						
							
								
									
										
										
										
											2008-02-21 20:12:37 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{ $values { "heap"  "a heap"  } }
							 
						 
					
						
							
								
									
										
										
										
											2008-02-21 18:07:26 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{ $description "Remove the first element from the heap."  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $side-effects "heap"  } ;
 
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 02:42:37 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  heap-pop  
						 
					
						
							
								
									
										
										
										
											2009-10-21 18:40:43 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $values { "heap"  "a heap"  } { "value"  object  } { "key"  object  } }
							 
						 
					
						
							
								
									
										
										
										
											2008-02-21 18:07:26 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{ $description "Output and remove the first element in the heap."  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $side-effects "heap"  } ;
 
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 02:42:37 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  heap-empty?  
						 
					
						
							
								
									
										
										
										
											2008-02-21 20:12:37 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{ $values { "heap"  "a heap"  } { "?"  "a boolean"  } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $description "Tests if a heap has no nodes."  } ;
 
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:10:26 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2008-02-21 18:07:26 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								HELP:  heap-size  
						 
					
						
							
								
									
										
										
										
											2008-02-21 20:12:37 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{ $values { "heap"  "a heap"  } { "n"  integer  } }
							 
						 
					
						
							
								
									
										
										
										
											2008-02-21 18:07:26 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{ $description "Returns the number of key/value pairs in the heap."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  heap-delete  
						 
					
						
							
								
									
										
										
										
											2008-03-11 20:51:58 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{ $values { "entry"  entry } { "heap"  "a heap"  } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $description "Remove the specified entry from the heap."  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $errors "Throws an error if the entry is from another heap or if it has already been deleted."  }
							 
						 
					
						
							
								
									
										
										
										
											2008-02-21 18:07:26 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{ $side-effects "heap"  } ;
 
							 
						 
					
						
							
								
									
										
										
										
											2008-10-01 19:10:57 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  slurp-heap  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $values
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								     { "heap"  "a heap"  } { "quot"  quotation } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $description "Removes values from a heap and processes them with the quotation until the heap is empty."  } ;