Programming with
Data Structures and Algorithms

Brown heaps

A heap is a data structure that holds comparable values and supports the following operations:

For simplicity, in this assignment you should use heaps of numbers.

Heaps are most commonly represented as binary trees. Unlike binary search trees, though, heaps impose no ordering constraint on the elements in the left and right children of a node. Instead, heaps maintain the invariant that the value at each node is less than the values of either of its children. Thus the minimum element in a heap is always its root.

One way to optimize the worst-case time complexity of the heap operations is to keep the heap balanced. A tree is balanced if the depths of any two leaves differ by at most one. In order to maintain balance in our heap implementation, we will impose a stronger condition: at each node, the number of nodes in the left subheap of a node must be the same or one greater than the number of nodes in the right subheap. We will call heaps with this invariant Brown heaps.

Part 1

Implement the three heap operations listed above for Brown heaps.

Use the following struct to represent Brown heap nodes:

(define-struct bhnode (value left right))

A Brown heap is either:

All three operations can be performed in O(log n) time or better. We will be grading on efficiency.

For testing purposes, we need you to use the function names and data definition above exactly as written.

Part 2

Analyze the time complexity of your implementations of the heap operations.

Note: It is important to make sure your analysis correctly describes your implementation. You will receive full credit on this section only for an accurate analysis of your code, even if your code is suboptimal.

What to turn in: