Midterm 2 Solutions | Data Structures and Algorithms Basics | COP 3530, Exams of Data Structures and Algorithms

Material Type: Exam; Class: DATA STRUC/ALGORITHMS; Subject: COMPUTER PROGRAMMING; University: University of Florida; Term: Unknown 1989;

Typology: Exams

Pre 2010

Uploaded on 03/18/2009

koofers-user-vl9
koofers-user-vl9 🇺🇸

4

(1)

10 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Midterm 2 Solutions
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Midterm 2 Solutions | Data Structures and Algorithms Basics | COP 3530 and more Exams Data Structures and Algorithms in PDF only on Docsity!

Midterm 2 Solutions

Problem 1.

Recall that the degree of a node in a tree is defined as the number of its children, and the degree of a tree as the maximum degree of its children. Also recall that the height of a tree is the number of its levels (tree with just one node has height 1), or equivalently the length of the longest path starting from the root. Now consider a tree which has degree d, and height h.

(a) What is the maximum number of nodes n this tree can have?

(b) What is the minimum number of nodes n this tree can have?

(c) Assuming this tree has n nodes, what is the minimum possible height (in terms of d and n) this tree can have?

(d) If a binary tree has n leaves, how many internal nodes (non-leaves) does it have?

Problem 2.

Suppose you have a binary tree whose data fields are single characters. An inorder traversal of the tree yields the output "BHGCEDIJFA". A level order traversal of the tree yields the output "CGABFHDEIJ". Draw the binary tree, showing the data in each node. Show the steps used to arrive at the result.

Solution 2:

C /
G A / / B F \ / H D /
E I
J

Solution 3: (a) 10 /
5 18 /
12 22
15 / 13

(b) For key 12: 10,18,

For key 14: 10,18,

(c) 10(1) /
5(0) 18(3) /
12 (0) 22(0)
15(1) / 13(0)

(d)Comparison with 10, 1<4, new rank = 4-1-1 = 2, looking in right subtree Comparison with 18, 2<3, new rank = 2, looking in left subtree Comparison with 12, 0<2, new rank = 2-0-1= 1, looking in right subtree Comparision with 15, leftSize = rank, element found

(e)

10 /
5 15 /
12 22
13

Problem 4.

(a) Construct a min height-biased leftist tree from the following data: [10, 12, 22, 8, 4, 2, 6, 20, 18].

b) Draw the height biased max leftist tree that results when the two height biased max leftist trees below are melded by following their rightmost paths. Use the strategy used in the meld algorithm of the text, and discussed in class. Show the steps used to arrive at the

/ \ / \

/ \ / \

/ \ /\ / \ / \

Problem 5.

(a) Start with an empty AVL search tree and insert the following keys in the given order: 25, 35, 45, 20, 22, 27. Draw the trees following each insertion, and also after each rotation. Specify the rotation types. You do need to specify the balancing factors.

(b) Consider the following AVL search tree.

/ \

/ \

/ \ / \

/ / / \

Draw the tree that results from performing the operation remove(31) on the above tree. Perform the rotations necessary to restore the tree to an AVL search tree, specify the balance factor of each node.

Solution 5:

(a) (^250) /
(^221 ) / /
(^200 270 )

(b) (^350) /
(^20) -1 550 / \ /
(^100 251 520 60) - / / \
(^220 400 530 )

Solution 6: Best fit : [1, 5, 4] [3, 7] [6] [2, 8] 8, 7, 6, 5, 4, 3, 2, 1 Best fit decreasing: [2, 8] [3, 7] [4, 6] [1, 5]

Problem 7

a). 92

Delete max:

Insert 60

b) 6 6 6

Problem 8

LastFitPack (int n)

current=root;

if(root.unusedcapacity<n)

new newnode;

newnode.unusedcapacity=capacity-n;

winertree.add(newnode);

else if(root.unusedcapacity>=n)

while(current->rightchild!=null)

if(current->rightchild.unusedcapacity>=n)

current=current->rightchild;

else if(current->leftchild.unusedcapacity>=n)

current=current->leftchild;