Guest User

Untitled

a guest
Jun 24th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. def enqueue( heap, key, value ):
  2. """
  3. enqueue: SBBTreeHeap Orderable Any -> NoneType
  4. Adds the key/value pair to the heap.
  5. Raises TypeError if heap is not an instance of SBBTreeHeap.
  6. Must be an O(log n) operation.
  7. """
  8.  
  9. if ( empty( heap ) ):
  10. heap = SBBTreeHeap
  11. heap.root = TreeNode ( key, value, None )
  12. return heap
  13. else:
  14. if ( heap.root.lchild == None and heap.root.rchild == None ):
  15. heap.root.lchild = TreeNode ( key, value, heap.root )
  16. heap.root.lchild.parent = heap.root
  17. heap.root.size += 1
  18. siftUp ( heap.root.lchild )
  19. return heap
  20. if ( heap.root.lchild != None ):
  21. heap.root.rchild = TreeNode ( key, value, heap.root )
  22. heap.root.rchild.parent = heap.root
  23. heap.root.size += 1
  24. siftUp ( heap.root.rchild )
  25. return heap
  26. if ( heap.root.lchild != None and heap.root.rchild != None ):
  27. if ( heap.root.lchild.size >= heap.root.rchild.size ):
  28. enqueue( heap.root.rchild, key, value )
  29. heap.root.size += 1
  30. else:
  31. enqueue( heap.root.lchild, key, value )
  32. heap.root.size += 1
  33. return heap
Add Comment
Please, Sign In to add comment