Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def enqueue( heap, key, value ):
- """
- enqueue: SBBTreeHeap Orderable Any -> NoneType
- Adds the key/value pair to the heap.
- Raises TypeError if heap is not an instance of SBBTreeHeap.
- Must be an O(log n) operation.
- """
- if ( empty( heap ) ):
- heap = SBBTreeHeap
- heap.root = TreeNode ( key, value, None )
- return heap
- else:
- if ( heap.root.lchild == None and heap.root.rchild == None ):
- heap.root.lchild = TreeNode ( key, value, heap.root )
- heap.root.lchild.parent = heap.root
- heap.root.size += 1
- siftUp ( heap.root.lchild )
- return heap
- if ( heap.root.lchild != None ):
- heap.root.rchild = TreeNode ( key, value, heap.root )
- heap.root.rchild.parent = heap.root
- heap.root.size += 1
- siftUp ( heap.root.rchild )
- return heap
- if ( heap.root.lchild != None and heap.root.rchild != None ):
- if ( heap.root.lchild.size >= heap.root.rchild.size ):
- enqueue( heap.root.rchild, key, value )
- heap.root.size += 1
- else:
- enqueue( heap.root.lchild, key, value )
- heap.root.size += 1
- return heap
Add Comment
Please, Sign In to add comment