DSA 2: Data Structure Implementation
x.prev ≠ NIL
*Deleting from a doubly linked list* The procedure LIST-DELETE(L,x) removes an element x from a linked list L. It must be given a pointer to x, and it then "splices" x out of the list by updating pointers. Identify the missing piece of code.
i = A.heap_size
*Insert into a max-heap* Given an array A that has been max-heapified, and a key k, the the MAX_HEAP_INSERT(A,k) procedure will insert the key k into the array and preserve the max-heap property. Identify the missing piece of code.
x ≠ NIL
*Inserting into a Binary Search Tree* Given a binary search tree T and a new node z, the TREE_INSERT(T,z) procedure insert the new node into the tree, maintaining the Binary Search Tree Property. Identify the missing piece of code.
Q.tail = 1
*Inserting into a circular queue* This queue is implemented as an array Q[1..n]. The queue has an attribute Q.head that indexes, or points to its head, an attribute Q.tail that indexes or points to the next location in Q where a newly arriving element will be inserted into the queue, and an attribute Q.length which stores the total capacity of the queue. The ENQUEUE(Q,x) procedure inserts the element x in the queue. Identify the missing piece of code.
L.head = x
*Inserting into a doubly linked list* Given an element x whose key attribute has already been set, the LIST-INSERT(L,x) procedure "splices" x onto the front of the linked list. Identify the missing piece of code.
y.next ≠ NIL
*Inserting into a singly linked list* Given an element x whose key attribute has already been set, the LIST-INSERT(L,x) procedure "splices" x onto the back end of the linked list. Identify the missing piece of code.
T[j] == NIL
*Inserting into an open addressing hash table* Given a key k, the HASH_INSERT(T, k) procedure adds the key k to the hash table T. This hash table is implemented as an array T[1..m], and includes a hash function h(k, i) that, for every key k, the probe sequence {h(k,0),h(k,1),...,h(k,m-1)} is a permutation of {0,1...,m-1}. Identify the missing piece of code.
exchange A[i] with A[largest]
*Max-heapify an array* Given an array A and an index i, where the left subtree rooted at 2i and the right subtree rooted at 2i + 1 are max-heaps, the MAX_HEAPIFY(A,i) procedure lets the value at A[i] "float down" in the max-heap so that the subtree rooted at index i obeys the max-heap property. Identify the missing piece of code.
S.top = S.top - 1
*Popping off of a stack* This stack is implemented as an array that has an attribute S.top that indexes the most recently inserted element.The POP(S) procedure removes the element on the top of stack S, and returns it. Identify the missing piece of code.
S.top = S.top + 1
*Pushing onto a stack* Given an element x whose key attribute has already been set, the PUSH(S,x) procedure add the element x to the top of stack S. This stack is implemented as an array that has an attribute S.top that indexes the most recently inserted element. Identify the missing piece of code.
Q.head == Q.length
*Removing from a circular queue* This queue is implemented as an array Q[1..n]. The queue has an attribute Q.head that indexes, or points to its head, an attribute Q.tail that indexes or points to the next location in Q where a newly arriving element will be inserted into the queue, and an attribute Q.length which stores the total capacity of the queue. The DEQUEUE(Q) procedure removes and returns the element at the head of the queue. Identify the missing piece of code.
x ≠ NIL and k ≠ x.key
*Searching a Binary Search Tree* Given a root node x and a key k, the TREE_SEARCH(x,k) procedure iteratively searches the tree for the key K, returning NIL if k is not found. Identify the missing piece of code.
TREE_SEARCH(x.left,k)
*Searching a Binary Search Tree* Given a root node x and a key k, the TREE_SEARCH(x,k) procedure recursively searches the tree for the key K, returning NIL if k is not found. Identify the missing piece of code.
x = L.head
*Searching a linked list* The procedure LIST-SEARCH(L,k) finds the first element with key k in list L by a simple linear search, returning a pointer to this element. If no object with key k appears in the list, then the procedure returns NIL. Identify the missing piece of code.
T[j] == NIL
*Searching an open addressing hash table* Given a key k, the HASH_SEARCH(T, k) procedure searches for the key k in the hash table T. This hash table is implemented as an array T[1..m], and includes a hash function h(k, i) that, for every key k, the probe sequence {h(k,0),h(k,1),...,h(k,m-1)} is a permutation of {0,1...,m-1}. The HASH_SEARCH(T, k) procedure returns the index j where the key k is found or NIL if the key is not in the hash table. Identify the missing piece of code.