You are on page 1of 2

Data Structures – Assignment 7

IDC, Spring 2022

Lecturer: Prof. Yael Moses, Dr. Ben Lee Volk

TAs: Yael Hitron, Guy Kornowski, Elad Tzalik

Submission day: 20.4.22, 23:50 (you can use an extension and submit by 24.4.22, 23:50).

Honor code:
• Do not copy the answers from any source.
• You may work in small groups but write your own solution. Add whom you
worked with.
• Cheating students will face Committee on Discipline (COD).
• Do not forget – you are here to learn!

Submission:
• Submit your solution as a PDF file only. Other formats will not be graded.
• Typed submissions will get a bonus of 3 points.
• If you choose not to type your solution, make sure the scan looks good. We will
deduct points for hard-to-read submissions.

Question A
Show the AVL trees that result after successively inserting the following keys (from left
to right) into an initially empty AVL tree {1, 6, 11, −8, −4, 0, 9, 13, 2, −1}. Draw the tree
after each node is inserted, and explain which rotation or rotations (direction, and the node
around the rotation was performed) were used at each insertion.

Question B
We say that a binary tree is k-loosely-balanced, if for every node it holds that the heights
of its right and left sub-trees differ by at most k (including k). Prove that for any k the
height of a k-loosely-balanced binary tree is O(k log(n)), where n is the number of nodes
in the tree.

1
Question C
Given two binary search trees T1 , T2 of sizes n1 , n2 and heights h1 , h2 respectively, describe
an O(n1 + n2 ) time algorithm (in the worst case) for each of the following tasks:

1. Check whether the keys of T1 and T2 are the same.

2. Check whether T1 and T2 are identical as binary search trees. That is, do they contain
the same keys in the same structure. You are allowed to use only O(max{h1 , h2 })
additional space.

3. Create an AVL tree that contains all elements in T1 ∪ T2 . You should provide pseudo-
code for your suggested algorithm. Notice that T1 , T2 are not assumed to be balanced,
but you may assume for simplicity that |T1 ∪ T2 | = 2k − 1 for some (unknown) integer
k.

Question D
In order to compute the revenue from ”arnona” taxes, a city wishes to construct a data
structure that stores the area of each apartment in the city. Suggest a data structure that
supports the following operations in O(log n) time in the worst case, where n is the number
of apartments in the data structure when the operation is executed.

• Insert(a) – Insert apartment a into the data structure.

• M edianP ercentile(k) – Returns the median of the k’th percentile of the areas. The
100 percentile of a data set divide it into 100 equal parts. That is, in a data set of size
n, the k’th percentile is defined to be all values with rank between (k − 1)n/100 and
kn/100. So, for example, if k = 60, the function needs to return the median among
all areas whose rank is between ⌊ 59n
100
n
⌋ and ⌊60 100 ⌋.
Hint: what is the rank of the element you are looking for?

• AvgP ercentile(k) – Returns the average area of the k’th percentile.

1. Describe the data structure. Specify all the data structures you are using and any
additional fields or variables.

2. Provide a short description for Insert(a) and justify the time complexity.

3. Provide a short description for M edianP ercentile(k) and justify the time complexity.

4. Write a pseudo-code for AvgP ercentile(k) and analyze the time complexity.

You might also like