Back to Claude Haiku 4.5

Agent Work: Homework2

Claude Haiku 4.5 · COMP 382: Reasoning about Algorithms

COMP 382: Reasoning about Algorithms

Homework 2

Total Points: 100

---

Problem 1: Persistent Dynamic Sets [25 points]

During the course of an algorithm, you sometimes find that you need to maintain past versions of a dynamic set as it is updated. We call such a set persistent. One way to implement a persistent set is to copy the entire set whenever it is modified, but this approach can slow down a program and also consume a lot of space. Sometimes, you can do much better.

Consider a persistent set S with the operations INSERT, DELETE, and SEARCH, which you implement using binary search trees.

Maintain a separate root for every version of the set. In order to insert a key (e.g., key 5) into the set, create a new node with that key. This node becomes the child of a new copy of its parent, and so on up to the root. Thus, you copy only part of the tree and share some of the nodes with the original tree.

Assume that each tree node has the attributes key, left, and right but no parent.

Part (a)

For a persistent binary search tree (not a red-black tree, just a binary search tree), identify the nodes that need to change to insert or delete a node.

Part (b)

Write a procedure PERSISTENT-TREE-INSERT(T, z) that, given a persistent binary search tree T and a node z to insert, returns a new persistent tree T' that is the result of inserting z into T. Assume that you have a procedure COPY-NODE(x) that makes a copy of node x, including all of its attributes.

Part (c)

If the height of the persistent binary search tree T is h, what are the time and space requirements of your implementation of PERSISTENT-TREE-INSERT? (The space requirement is proportional to the number of nodes that are copied.)

Part (d)

Suppose that you include the parent attribute in each node. In this case, the PERSISTENT-TREE-INSERT procedure needs to perform additional copying. Prove that PERSISTENT-TREE-INSERT then requires Ω(n) time and space, where n is the number of nodes in the tree.

Part (e)

Show how to use red-black trees to guarantee that the worst-case running time and space are O(log n) per insertion. You may assume that all keys are distinct.

---

Problem 2: Join Operation on Red-Black Trees [35 points]

The join operation takes two dynamic sets S₁ and S₂ and an element x such that for any x₁ ∈ S₁ and x₂ ∈ S₂, we have x₁.key ≤ x.key ≤ x₂.key. It returns a set S = S₁ ∪ {x} ∪ S₂.

In this problem, we investigate how to implement the join operation on red-black trees.

Part (a)

Given a red-black tree T, let us store its black-height as the new attribute T.bh. Argue that RB-INSERT can maintain the bh attribute without requiring extra storage in the nodes of the tree and without increasing the asymptotic running times. Show how to determine the black-height of each node visited while descending through T, using O(1) time per node visited.

Let T₁ and T₂ be red-black trees and x be a key value such that for any nodes x₁ in T₁ and x₂ in T₂, we have x₁.key ≤ x.key ≤ x₂.key. You will show how to implement the operation RB-JOIN(T₁, x, T₂), which destroys T₁ and T₂ and returns a red-black tree T = T₁ ∪ {x} ∪ T₂. Let n be the total number of nodes in T₁ and T₂.

Part (b)

Assume that T₁.bh ≥ T₂.bh. Describe an O(log n) time algorithm that finds a black node y in T₁ with the largest key from among those nodes whose black-tree height is T₂.bh.

Part (c)

Let Tᵧ be the subtree rooted at y. Describe how T = Tᵧ ∪ {x} ∪ T₂ can replace Tᵧ in O(1) time without destroying the binary-search-tree property.

Part (d)

What color should we make x so that red-black properties 1, 3, and 5 are maintained? Describe how to enforce properties 2 and 4 in O(log n) time.

Part (e)

Argue that no generality is lost by making the assumption in part (b). Describe the symmetric situation that arises when T₁.bh ≤ T₂.bh.

Part (f)

Show that the running time of RB-JOIN is O(log n).

---

Problem 3: Joining and Splitting 2-3-4 Trees [40 Points]

The join operation takes two dynamic sets S' and S'' and an element x such that x'.key < x.key < x''.key for any x' ∈ S' and x'' ∈ S''. It returns a set S = S' ∪ {x} ∪ S''.

The split operation is like an "inverse" join: given a dynamic set S and an element x ∈ S, it creates a set S' that consists of all elements in S − {x} whose keys are less than x.key and a set S'' that consists of all elements in S − {x} whose keys are greater than x.key.

In this problem, we investigate how to implement these operations on 2-3-4 trees (B-trees with t = 2). We assume for convenience that elements consist only of keys and that all key values are distinct.

Part (a)

Show how to maintain, for every node x of a 2-3-4 tree, the height of the subtree rooted at x as an attribute x.height. Make sure that your implementation does not affect the asymptotic running times of searching and insertion.

Part (b)

Show how to implement the join operation. Given two 2-3-4 trees T' and T'' and a key k, the join operation should run in O(1 + |h' − h''|) time, where h' and h'' are the heights of T' and T'', respectively.

Part (c)

Consider the simple path p from the root of a 2-3-4 tree T to a given key k, the set S' of keys in T that are less than k, and the set S'' of keys in T that are greater than k. Show that p breaks S' into a set of trees {T'₀, T'₁, ..., T'ₘ} and a set of keys {k'₁, k'₂, ..., k'ₘ} such that y < k'ᵢ < z for i = 1, 2, ..., m and any keys y ∈ T'ᵢ₋₁ and z ∈ T'ᵢ. What is the relationship between the heights of T'ᵢ₋₁ and T'ᵢ? Describe how p breaks S'' into sets of trees and keys.

Part (d)

Show how to implement the split operation on T. Use the join operation to assemble the keys in S' into a single 2-3-4 tree T' and the keys in S'' into a single 2-3-4 tree T''. The running time of the split operation should be O(log n), where n is the number of keys in T. (Hint: The costs for joining should telescope.)