Sie sind auf Seite 1von 3

Cover page for

CSC263 Homework #2
(fill and attach this page to your homework)

Submitted by:

(1) Family Name: (2) Family Name:

Given Name: Given Name:

Student Number: Student Number:

Graded Homework should be returned in Tutorial: (tutorial room number)

By virtue of submitting this homework I/we acknowledge that I am/we are aware of the policy on
homework collaboration for this course.

1
Computer Science CSC263H February 3, 2011
St. George Campus University of Toronto

Homework Assignment #2
Due: February 17, 2011, by 5:30 pm
(in the drop box for this course in Bahen 2220)

1. On the cover page of your assignment, in addition to your name(s), you must write the location of
the tutorial where you want the graded homework to be returned.

2. If you do not know the answer to a question, and you write “I (We) do not know the answer to this
question”, you will receive 20% of the marks of that question. If you just leave a question blank
with no such statement, you get 0 marks for that question.

3. Unless we explicitly state otherwise, you should justify your answers. Your paper will be marked
based on the correctness and completeness of your answers, and the clarity, precision and conciseness
of your presentation.

Question 1. (10 marks) In this question, you must use the insertion and deletion algorithms as described
in the “Balanced Search Trees: AVL trees” handout posted on the course web site.
Insert into an initially empty AVL tree each of the following keys, in the order in which they appear in
the sequence: 17, 7, 8, 14, 19, 6, 10, 21, 15, 12, 9, 11.
Show the resulting AVL tree T . (Only the final tree should be shown; any intermediate trees shown
will be disregarded, and not given partial credit.)
From AVL tree T , delete 14, and show the resulting tree.
In the two trees you must show the key and balance factor of each node.

Question 2. (18 marks) Say that we call a binary search tree slightly balanced if for every node, the
height of its left subtree differs from the height of its right subtree (in absolute value) by at most 2. (Recall
the height of a tree is the number of edges in a longest path from the root to a leaf, and that we regard
the height of an empty tree as being -1.)
For every nonnegative integer h, let mh be the smallest possible number of nodes that can be in a
slightly balanced binary search tree of height at least h.
a. (6 marks) Give a recurrence for mh . Make sure to state all the base cases.
b. (12 marks) Prove carefully by induction that for every h ≥ 0, mh ≥ 1.4h − 1.

Question 3. (32 marks) Consider an abstract data type that consists of a sequence of integers upon
which the following operations can be performed:

Query(A, i) Returns the ith element in A. The sequence A remains unchanged. This operation is defined
only if 1 ≤ i ≤ length(A).

Sum(A, i, j) Returns the sum of the ith through jth elements in A. The sequence A remains unchanged.
This operation is defined only if 1 ≤ i ≤ j ≤ length(A).

Insert(A, i, a) Insert the integer a into the sequence A immediately before the ith element. If i =
length(A) + 1, then append a to the end of A. This operation is defined only if 1 ≤ i ≤ length(A) + 1.

Delete(A, i) Delete the ith element from sequence A. This operation is defined only if 1 ≤ i ≤ length(A).

2
For example, suppose A = (5, −2, 5, 1, 3). Then Query(A, 4) returns 1 and Sum(A, 2, 4) returns 4.
If Insert(A, 3, 7) is performed, then A = (5, −2, 7, 5, 1, 3). If Delete(A, 5) is performed next, then
A = (5, −2, 7, 5, 3).
A sequence A can be represented as a binary tree whose inorder traversal visits the elements of the
sequence A in order from beginning to end.
Describe a data structure based on this representation that implements this abstract data type and
performs each of these operations in O(log n) time, where n = length(A).
Hint: Your implementation should be based on a data structure and algorithms described in class,
and your description can focus on the extensions and modifications that are needed to solve this problem.
a. (5 marks) Give a precise and full description of your data structure. Make sure you describe what
information is stored at each node of your binary tree and what properties your trees have.
b. (3 marks) Draw a diagram of your data structure for the sequence A = (5, −2, 7, 5, 1, 3).
c. (24 marks) Explain how to implement Query(A, i), Sum(A, i, j), Insert(A, i, a) and Delete(A, i),
so that each operation runs in O(log n) time. Justify your time complexity claims.

Das könnte Ihnen auch gefallen