Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

Samantha Sinnerine

CSCI-313

C-9.29

An alternative implementation of the upheap method in the HeapPriorityQueue class


that uses recursion instead of a loop:

/**
* Moves the entry at index j higher, if necessary, to restore the heap property.
* Recursive implementation.
*/
protected void upheap(int j) {
if (j > 0) { // Base case: stop recursion if reaching the root
int p = parent(j);
if (compare(heap.get(j), heap.get(p)) < 0) { // Swap if child is smaller
than parent
swap(j, p);
upheap(p); // Recursive call on the parent's location
}
}
}

-----------------------------------------------------------------------------------
-----------------
C-9.31

When inserting a new node:


a. If the tree is empty, set the last node reference to the newly inserted node.
b. Otherwise, if the newly inserted node's position is to the right of the current
last node, update the last node reference to the newly inserted node.

When removing a node:


a. If the node being removed is the last node, update the last node reference to
the new last node:
i. If the last node is a left child, update the last node reference to its parent.
ii. If the last node is a right child, find the new last node by traversing up the
tree until finding a node that has a right sibling. Set the last node reference to
the leftmost node in the next level.
b. If the node being removed is not the last node, no changes are needed

-----------------------------------------------------------------------------------
-----------------
C-9.34

To compute all the entries in a heap H that have a key less than or equal to k, we
can use the following algorithm:

Create an empty list or array to store the entries with keys less than or equal to
k.
Start from the root of the heap H.
If the key of the current entry is less than or equal to k, add it to the list or
array.
Recursively traverse the left and right children of the current entry, repeating
steps 3 and 4 for each child.
Return the list or array containing the entries with keys less than or equal to k.
The algorithm visits each entry in the heap H exactly once and checks its key
against k. Therefore, the time complexity of the algorithm is proportional to the
number of entries returned.

-----------------------------------------------------------------------------------
-----------------
C-9.37

To combine two binary trees T1 and T2 into a binary tree T while maintaining the
heap-order property, we can use the following algorithm:

Create a new empty binary tree T.


Find the root node with the minimum key among the roots of T1 and T2. Let's assume
it is the root of T1.
Set the root of T as the root node with the minimum key from T1.
Recursively merge the remaining nodes of T1 and T2 into T by following these steps:
If the left child of the current node in T1 has a smaller key than the left
child of the current node in T2, merge the left subtree of T1 with the left subtree
of T2 and set it as the left child of the current node in T.
Otherwise, merge the left subtree of T2 with the left subtree of T1 and set
it as the left child of the current node in T.
Apply the same logic to merge the right subtrees of T1 and T2.
Continue recursively merging the children of the current nodes until reaching
the leaves of T1 and T2.
Return the merged binary tree T.

The time complexity of this algorithm is O(h1 + h2), where h1 and h2 are the
respective heights of T1 and T2. This is because merging the trees is done in a
depth-first manner, traversing the trees from the root to the leaves, which takes
time proportional to the heights of the trees.

You might also like