0% found this document useful (0 votes)
18 views

Quiz2 Solution

This document contains a quiz with 3 questions on data structures. Question 1 asks to complete a method to print the postfix expression of an arithmetic expression tree. The running time is O(n) where n is the number of nodes. Question 2 asks about an augmented BST where each node stores its size. An algorithm is given to find the kth smallest element in O(log n) time by using the size attribute. Without size, the algorithm would take O(n) time via inorder traversal. Question 3 asks to perform insert and delete operations on an AVL tree and draw the resulting tree showing any rotations.

Uploaded by

Suiaj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Quiz2 Solution

This document contains a quiz with 3 questions on data structures. Question 1 asks to complete a method to print the postfix expression of an arithmetic expression tree. The running time is O(n) where n is the number of nodes. Question 2 asks about an augmented BST where each node stores its size. An algorithm is given to find the kth smallest element in O(log n) time by using the size attribute. Without size, the algorithm would take O(n) time via inorder traversal. Question 3 asks to perform insert and delete operations on an AVL tree and draw the resulting tree showing any rotations.

Uploaded by

Suiaj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

COL106: Quiz-2 Name:

Maximum marks: 10 Entry number: 2 0

Question 1 Consider the data structure to store arithmetic expressions using ArithmeticNode
class. Complete the method printPostFix. [3 marks]

public class ArithmeticNode {


int val;
String op; // can be ’+’,’-’ or ’*’
boolean isOperator;
ArithmeticNode left, right;

void printPostfix() {

if( isOperator ){

left.printPostfix() ;

right.printPostfix() ;

System.out.print(op) ;
}
else{ System.out.print(val) ;}
}
}

Also analyse the running time of your algorithm. [1 marks]

The running time is O(number of nodes in tree) as


T ime(x) = T ime(x.lef t) + T ime(x.right) + 1.
Solving the recurrence gives T ime(x) is order of number of nodes in tree rooted at x.

Alternative argument: This algorithm performs a postorder traversal of tree. This traversal
takes time which is again order of number of nodes in the tree.

Example:

* 3

4 8

Postfix expression: 4 8 * 3 +
Question 2 Consider an augmented BST where each node x has four attributes: data, left, right,
size; where the size of a node denotes the number of nodes in its subtree (see example below).

data=33
size=6

data=15 data=47
size=3 size=2

data=10 data=20 data=38


size=1 size=1 size=1

Complete the algorithm below to find the k th smallest element in a BST. [2 marks]

x ← head ;
if (k ⪈ x.size) then Raise error;
while (1) do
if (x.left ̸= Null and k ≤ x.left.size) then
x = x.left ;
else if (x.right ̸= Null and k ⪈ x.size − x.right.size ) then
x = x.right ;
k = k − x.size + x.right.size ;
else
Return x.data ;

Algorithm 1: OrderStatistics(Node head, int k)

How will the algorithm and running time change if you do not store ‘size’? [1 marks]

If we do not store size, then we will need to perform an inorder traversal and return the data of
k th node in the traversal. This will take O(n) time in the worst case as compared to O(log n)
running time of above algorithm.
Question 3 In the given AVL tree, perform the following operations and draw the tree after
each opearation. Clearly show the rotations used. The operations are to be performed in order,
i.e., the resultant tree after one operation should be used for the next operation. [3 marks]

20

10 57

12 29 100

90

a. insert 95

b. insert 92

c. delete 10

You might also like