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

DataStructures Assignment3 2023

This document contains an assignment for a data structures course. It includes 4 questions about AVL trees, implementing a data structure with specific time complexities, merge/split operations on AVL trees, and an example of inserting elements into a B-tree in different orders resulting in different trees. The assignment asks students to provide written answers and explanations for the questions.

Uploaded by

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

DataStructures Assignment3 2023

This document contains an assignment for a data structures course. It includes 4 questions about AVL trees, implementing a data structure with specific time complexities, merge/split operations on AVL trees, and an example of inserting elements into a B-tree in different orders resulting in different trees. The assignment asks students to provide written answers and explanations for the questions.

Uploaded by

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

Ben-Gurion University of the Negev

Data Structures
202.1.1031

Assignment No. 3

Responsible staff members:


Or Sattath ([email protected]) Dor Amzaleg([email protected])

Authors:
John Doe (123456789) Jane Doe (987654321)

Publication date: May 4th, 2023


Submission date: May 18th, 2023
0 Integrity Statement
I, <Insert your name/s and ID here>, certify that the work I have submitted is entirely my own. I have not
received any part of it from any other person, nor have I given any part of it to others for their use.
I have not copied any part of my answers from any other source, and what I submit is my own creation. I
understand that formal proceedings will be taken against me before the BGU Disciplinary Committee if there
is any suspicion that my work contains code/answers that is not my own.
If you have relied on or used an external source, you must cite that source at the end of your integrity statement.
External sources include shared file drivers, Large Language Models (LLMs) including ChatGPT, forums,
websites, books, etc.

1 AVL trees
1. (8 points) Let x be a leaf in an AVL tree. Prove that d(x) ≥ ⌈ h2 ⌉. Here, d(x) is the depth of x, and h is
the height of the tree.

2. (2 points) Give an example for a class of AVL trees (this class of trees was mentioned in class) in which
the bound above is sharp (namely, there exists a leaf x with d(x) = ⌈ h2 ⌉ ). There is no need to justify
your answer; the answer should take at most 1 line.
3. (10 points) Explain how it is possible, by using additional space, to support the operation f ind(x, k) that
returns the first k elements greater than the key x in an AVL tree, in time complexity O((log n) + k). If
the number of elements with a key greater than x is less than k, the operation should return all these
elements.

Answer to Question 1: (Write your answers here:)

1.
2.
3.

1
2 Data structure by requirements and time complexity constrains
(25 points)
Implement, for a client who owns a products factory, a data structure that supports the following operations in
the given time complexities:
Operation Description Time complexity
Init() Initiate an empty data structure. O(1)
Insert a product with a unique identification
number id (unbounded natural number),
Insert(id, quality) O(log n)
and its quality quality (integer between 0 to 5),
to the data structure.
Delete the product with the unique identification
Delete(id) number id from the data structure O(log n)
(if there exists such an element in the data structure).
Return the median quality of the products in the data structure.
M edianQuality() O(1)
The median is defined as the ⌈ n2 ⌉-th smallest element.
AvgQuality() Return the average quality of the products in the data structure. O(1)
Delete all the elements with quality 0 from the data structure.
JunkW orst() O(1)
This operation should return a linked list with all the deleted elements.
Print all the elements in the data structure,
P rintout() O(n)
ordered by identification number, from the smallest to the biggest.
Describe briefly your data structure.
Give an algorithm and a short time-complexity analysis to every operation.
Answer to Question 2: (Write your answers below:)

• The data structure consists of:

• Init():

• Insert(id, quality):

• Delete(id):

• M edianQuality():

• AvgQuality():

• JunkW orst():

• P rintout():

2
3 Merge/Split
1. (10 points) Let T1 , T2 be two AVL trees with heights h1 , h2 respectively, such that max(T1 ) < min(T2 )
(every key in T2 is greater than every key in T1 ). Let x be an additional key, which satisfies max(T1 ) <
x < min(T2 ). Describe an algorithm for merging T1 , x, T2 to a new AVL tree (the new AVL tree should
contain all the keys in T1 , all the keys in T2 , and x), in time complexity O(|h1 − h2 |). Guidance: If
h1 > h2 , try to insert the node x and the tree T2 to the appropriate point in the rightmost path from the
root to a leaf in the tree T1 . Show that the tree we get by this insertion can be balanced by rotations.
Think about the symmetric case (h1 ≤ h2 ).
2. (5 points) What is the height of the merged tree in your solution?
(a) max{h1 , h2 }
(b) max{h1 , h2 }, or max{h1 , h2 }+1
(c) Can be as high as max{h1 , h2 } + |h1 − h2 |
(d) None of the above.
3. (20 points) The operation Split(T, k), on an AVL tree T and a key k, returns two AVL trees T1 , T2 , such
that T1 contains all the keys in T which are less or equal to k, and T2 contains all the keys in T which
are greater than k. Describe an implementation to the operation Split(T, k) in time complexity O(log n),
where n is the number of elements in the tree T .
Guidance:
• Think of a way to get a list of nodes and sub-trees that should be contained in T1 , and a list of nodes
and sub-trees that should be contained in T2 .
• Find an efficient ordering to merge those sub-trees and nodes, using your algorithm from the previous
section. Note that a wrong ordering might give you a running time of O(log2 (n)).
• Analyze the time complexity of your algorithm. The time complexity of the merge algorithm from
the previous section and the upper bound you found for the height of the merged AVL tree, should
be used in this analysis.

4. (10 points) Describe a data structure that stores elements, where every element has a key (natural num-
ber) and a color (black or white). The data structure should support the following operations:
Operation Description Time complexity
Add(x) Insert the element x (where x has fields x.key and x.color). O(log n)
Return the color of the element with the key k
Color(k) O(log n)
(return null if no such exists).
Flip the colors of all the elements with a key less or equal to k.
F lipColors(k) You may assume that there is an element with key k O(log n)
in your data-structure.
There are various solutions. Some of them use the previous sections. Of course, all correct solutions will
be accepted.

Answer to Question 3: (Write your answers here:)


1.
2.

3.
4.

3
4 B-trees (10 points)
Give an example to a B-tree T with minimal degree t = 3, and (different) elements x, y which are not in the
tree, such that the tree we get by inserting x and then y to T is different than the tree we get by inserting y
and then x to T .
Add pictures of the tree T , and the trees after inserting x, y in both cases.
You should give an example with the smallest number of elements possible.

Answer to Question 4:

Picture of the tree T :

The element x is: (Write your answer here)


The element y is: (Write your answer here)

Picture of the tree after inserting x and then y:

Picture of the tree after inserting y and then x:

You might also like