CSE220 Final Fall-23 Set-A
CSE220 Final Fall-23 Set-A
CSE220 Final Fall-23 Set-A
Page 1 of 4 Set-A
On this BST, these following operations are done step by step:
i) 23 is inserted
ii) Node 29 is deleted using successor
iii) The ROOT is deleted using predecessor
You are given a Linked List, LL1, and an array, dist. Write a method sum_dist(list, arr) that
takes a Linked List and an array as parameters. This method sums the node elements in the
linked list that are away from the head by the elements in the array and returns the sum. Assume
the Node class has only elem and next variable. No need to write Node class and driver code.
LL1 = 10--> 16 --> -5 --> 9 --> 3 --> 4 4 Node Element away from the head at
dist = [2, 0, 5, 2, 8] distance 2 = -5
Node Element away from the head at
Function Call: distance 0 = 10
print(sum_dist(LL1, dist)) Node Element away from the head at
distance 5 = 4
Node Element away from the head at
distance 8 = Doesn't Exist, Considered as
0
The sum is: -5+10+4+-5+0 = 4
Constructor of Queue class is given to you for understanding the initial front and back positions.
Suppose, enqueue(elem), and dequeue() methods are implemented inside the Queue class.
class Queue:
def __init__(self,capacity):
self.circArr=[None]*capacity
self.front=capacity-1
self.back=capacity-1
Write down all the outputs of the following code snippet (That means you should print the
status of the circular queue, front index, back index after each iteration).
Page 2 of 4 Set-A
# Driver code
queue=Queue(4)
arr=[10,20,31,40,53]
test(arr,queue)
Suppose you are given a hash function called check(). In this hashing, forward chaining is used
for resolving conflict, and a 3-length array of singly linked lists is used as the hash table. In the
singly linked list, each node has a next pointer, a string key and a string value, for example:
(“7B12C” (key), “CSE220” (value)). The hash-table stores this key-value pair. The check
function is given below:
I. What is the hashed-index of (“4G14”, “MNS”) key-value pair in the above hashTable?
II. Implement a function remove(hashTable, key) that takes a key and a hash-table as
parameters. The function removes the key-value pair from the aforementioned hashtable if such a
key-value pair (whose key matches the key passed as argument) exists in the hashtable and return
the table. If the key does not exist, the function returns the same hash table.
Consider, Node class, hash_function and hashTable are given to you. You just need to complete
the remove(hashTable, key) function.
class Node:
def __init__(self, key, value, next=None):
self.key, self.value, self.next = key, value, next
Python Notation:
def remove(hashTable, key):
# To Do
Page 3 of 4 Set-A
Sample Input and Output
Input Table Returned Table
I. Given the array representation of a binary tree: [None value means the node is empty]
[None, 19, 22, 10, 32, 20, None, 30, None, 5, None, 7, None, None, 2, -2]
a. Draw the binary tree. [2 marks]
b. Write the post-order and pre-order traversal sequence of the tree. [1 marks]
c. Use the pre-order traversal sequence in part b to insert the elements in that order
in an initially empty binary search tree, and show the resulting binary search tree.
Note: Consider the first element of the pre-order sequence as the root. [2 marks]
d. Perform the following operations step by step sequentially on the Binary Search
Tree you created in part c. [2 marks]
i) Delete node 5 with the help of its successor.
ii) Delete node 20 with the help of its predecessor.
II. Write a recursive function swap_child() that takes the root of a binary tree, node’s level
and a number M as a parameter. The function will swap the left and right children of all
the nodes at level M and above. Here, 0 < M < height of the tree (root’s height). Consider,
the Node class for Binary Tree already defined with elem, left and right variables. YOU
CANNOT USE LIST OR DICTIONARY, any built-in function, global variables.
Python Notation:
def swap_child(root, level, M):
# To do
Function Call :
swap_child(root, 0, 2). Here root refers to the tree below.
Page 4 of 4 Set-A