2007-11-05 12:48:22 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								USING:  heaps.private  help.markup  help.syntax  kernel  math  assocs  ;
  
						 
					
						
							
								
									
										
										
										
											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:"  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $subsection min-heap }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $subsection min-heap? }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $subsection <min-heap> }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"Max-heaps sort their elements so that the maximum element is first:"  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $subsection min-heap }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $subsection min-heap? }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $subsection <min-heap> }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"Both obey a protocol."  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								$nl
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"Queries:"  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $subsection heap-empty? }
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:10:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $subsection heap-length }
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 11:01:11 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $subsection heap-peek }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"Insertion:"  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $subsection heap-push }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $subsection heap-push-all }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"Removal:"  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $subsection heap-pop* }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $subsection heap-pop } ;
 
							 
						 
					
						
							
								
									
										
										
										
											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 } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $description "Create a new "  { $link min-heap } "."  }
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:10:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $see-also <max-heap> } ;
 
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 02:42:37 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  <max-heap>  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $values { "max-heap"  max-heap } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $description "Create a new "  { $link max-heap } "."  }
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:10:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $see-also <min-heap> } ;
 
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 02:42:37 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  heap-push  
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:48:22 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $values { "key"  "a comparable object"  } { "value"  object  } { "heap"  heap } }
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:35:44 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $description "Push an pair onto a heap.  The key must be comparable with all other keys by the "  { $link <=> } " generic word."  }
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:48:22 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $side-effects "heap"  }
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:10:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $see-also heap-push-all heap-pop } ;
 
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 02:42:37 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  heap-push-all  
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:48:22 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $values { "assoc"  assoc  } { "heap"  heap } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $description "Push every key/value pair of an assoc onto a heap."  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $side-effects "heap"  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $see-also heap-push heap-pop } ;
 
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 02:42:37 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  heap-peek  
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:48:22 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $values { "heap"  heap } { "key"  object  } { "value"  object  } }
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:35:44 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $description "Outputs the first element in the heap, leaving it in the heap."  }
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:10:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $see-also heap-pop heap-pop* } ;
 
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 02:42:37 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  heap-pop*  
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:48:22 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $values { "heap"  heap } }
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:10:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $description "Removes the first element from the heap."  }
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:48:22 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $side-effects "heap"  }
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:10:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $see-also heap-pop heap-push heap-peek } ;
 
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 02:42:37 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  heap-pop  
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:48:22 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $values { "heap"  heap } { "key"  object  } { "value"  object  } }
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:35:44 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $description "Outputs the first element in the heap and removes it from the heap."  }
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:48:22 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $side-effects "heap"  }
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:10:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $see-also heap-pop* heap-push heap-peek } ;
 
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 02:42:37 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  heap-empty?  
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:48:22 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $values { "heap"  heap } { "?"  "a boolean"  } }
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:10:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $description "Tests if a "  { $link heap } " has no nodes."  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $see-also heap-length heap-peek } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  heap-length  
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:48:22 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $values { "heap"  heap } { "n"  integer  } }
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:10:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $description "Returns the number of key/value pairs in the heap."  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $see-also heap-empty? } ;