Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Insertion Operation - Data Structures - Solved Problems, Exams of Data Structures and Algorithms

Main points of this exam paper are: Insertion Operation, Originally Empty, Additional Property, Node Differ, Performing Rotations, Further Height, Underlying Data

Typology: Exams

2012/2013

Uploaded on 04/07/2013

seshu_lin3
seshu_lin3 🇮🇳

4

(3)

59 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSE3358 Problem Set 8
Solution
Problem 1: Practice Insertions (10 points)
Show the results of insering the following keys
F, S, Q, K, C, L, H, T, V, W, R, N, P, A, B, X, Y, D, Z, E
in the order shown into an originally empty
(a) (5 points) Red-Black Tree
ANSWER:
Q
K
S
W
A
D
R
V
Y
N
B
T
F
H
E
L
P
X
Z
C
(b) (5 points) B-tree with t= 2
ANSWER:
Q
B F K T W
Q
C D E H L N P R S
X Y Z V
Problem 2: AVL tree (30 points)
In 1962, Adelson-Velskii and Landis invented the AVL tree. An AVL tree is a binary search tree that
satisfies the following additional property:
AVL property: For any node x,|h[lef t[x]] h[right[x]]| 1 where h[x] denotes the height of x, and
we define h[NI L] = 1.
In other words, the heights of the left and right subtrees (children) of a node differ by at most one.
(a) (5 points) Let S(h) be the minimum number of nodes in an AVL tree of height h. Show that
S(0) = 1 and S(1) = 2 and that for h2, S(h) = S(h1) + S(h2) + 1.
1
pf3
pf4
pf5

Partial preview of the text

Download Insertion Operation - Data Structures - Solved Problems and more Exams Data Structures and Algorithms in PDF only on Docsity!

CSE3358 Problem Set 8 Solution

Problem 1: Practice Insertions (10 points) Show the results of insering the following keys

F, S, Q, K, C, L, H, T, V, W, R, N, P, A, B, X, Y, D, Z, E

in the order shown into an originally empty

(a) (5 points) Red-Black Tree

ANSWER:

Q

K S W

A D N R V Y

B

F T

H

C E L P X Z

(b) (5 points) B-tree with t = 2

ANSWER:

Q

B F K T W

Q C D E H L N P R S V X Y Z

Problem 2: AVL tree (30 points) In 1962, Adelson-Velskii and Landis invented the AVL tree. An AVL tree is a binary search tree that satisfies the following additional property:

AVL property: For any node x, |h[lef t[x]] − h[right[x]]| ≤ 1 where h[x] denotes the height of x, and we define h[N IL] = −1.

In other words, the heights of the left and right subtrees (children) of a node differ by at most one.

(a) (5 points) Let S(h) be the minimum number of nodes in an AVL tree of height h. Show that S(0) = 1 and S(1) = 2 and that for h ≥ 2, S(h) = S(h − 1) + S(h − 2) + 1.

ANSWER: We require one node (a root) to make a tree of height 0; therefore, S(0) = 1. We require at least 2 nodes to make a tree of height 1; therefore, S(1) = 2. Now consider an AVL tree with height h ≥ 2. This tree must have

  • a root
  • a left AVL subtree L
  • a right AVL subtree R

At least one of L and R must have height h−1 for our tree to have height h. Without loss of generality, say L has height h − 1. Since L is an AVL tree, it has at least S(h − 1) nodes. The height of R could be either h − 1 or h − 2 but not less, since our tree is an AVL tree. Therefore, R must have at least S(h − 2) nodes. As a result, our AVL tree with height h must have at least S(h − 1) + S(h − 2) + 1 nodes and hence S(h) = S(h − 1) + S(h − 2) + 1 for h ≥ 2.

(b) (5 points) Using induction, show that S(h) ≥ φh, where φ = 1+

√ 5

ANSWER:

Base case: S(0) = 1 ≥ φ^0 = 1 and S(1) = 2 ≥ φ^1 = 1.618.

Inductive step: Assume this hold for all heights ≤ h. Let’s show it holds for height h + 1. S(h + 1) = S(h) + S(h − 1) + 1 < S(h) + S(h − 1) ≤ φh^ + φh−^1 = φh+1( (^1) φ + (^) φ^12 ) = φh+1.1 = φh+1.

(c) (5 points) Conclude that the height h of an AVL tree on n nodes satisfies h = Θ(log n).

ANSWER: n ≥ S(h) ≥ φh. Therefore, h ≤ logφ n, i.e. h = O(log n). Since h = Ω(log n) for any binary tree, we conclude that h = Θ(log n).

(d) (15 points) Given that every node x stores its height h[x], an AVL tree can be maintained after an insertion operation. Here’s how: After inserting a new node x as a leaf, we set h[x] = 0 and we go up the tree updating the height of every node on the path from x to the root by incrementing its height by 1. When we encounter the first node y for which |h[lef t[y]] − h[right[y]]| = 2 (if any), we fix the AVL property for y by performing rotations. Depending on the position of x relative to y, different rotation(s) must be performed. We have two cases to consider (the other cases are symmetric)

  • case 1: x is in the left subtree of lef t[y]
  • case 2: x is in the right subtree of lef t[y]

The above two cases suggest that, since node y violates the AVL property after insertion, the left subtree of y is higher than the right subtree of y (the insertion was done in the left subtree of y), and the height difference is 2 (more than 1). For each of the two above cases, describe the rotation(s) required to re-establish the AVL property for node y. Argue that after re-establishing the AVL property for node y, no more height updates or fixes will be needed, i.e. the resulting tree is AVL.

ANSWER: Before insertion, the left subtree of y had height h and the right subtree of y had height h − 1. After insertion, the left subtree of y has height h + 1 resulting in a difference of 2 in the heights for both subtrees.

Rotate left around x

h-

  • h

insertion here

y

  • h

  • these must be h because y is the first node to violate the property and the tree was AVL before the insertion

x

y

h-

h+

h-

Rotate right around y

h h-1 h

h+1 h+

h

Since in all cases, the maximum depth of a node from node y goes back to h, the height of y does not change and no further height updates or rotations are needed.

Problem 3: The dishonest successor (30 points) As you know, the homework policy states that dishonest students are given red tags. Therefore, every student has a grade and a red-tag boolean flag to identifying whether the student is dishonest or not.

We wish to support all dynamic set operations on students such as INSERT, DELETE, SEARCH, MINIMUM, MAXIMUM, SUCCESSOR, PREDECESSOR in addition to the following operation:

Given a student x with grade k, find the dishonest student y (if any) with the lowest grade k′^ such that k′^ > k in O(log n) time, where n is the number of students. In other words, given a student x, we wish to find the dishonest successor of x in O(log n) time.

Explain how you can augment a red-black tree to support this operation. In doing so, describe the 4 steps discussed in class:

(a) (0 points) Choose the underlying data structure (red-black tree in this case) and determine what constitute the keys.

ANSWER: They keys will be the grades.

(b) (10 points) Identify the additional information that will augment the red-black tree

ANSWER: In addition to the key key[x], the flag dishonext[x] and the color color[x], every node x will have a field n[x] that represents the number of dishonest students in x’s subtree (i.e. including x itself if dishonet[x] = 1).

(c) (10 points) Argue that the additional information can be maintained easily by insertion and deletion and describe how it is maintained by rotations

ANSWER: Since n[x] can be completely obtained from lef t[x] and right[x], it is possible to update and maintain n[x] efficiently upon insertions, deletions, and during rotations.

n[x] = n[lef t[x]] + n[right[x]] + dishonest[x]

n[N IL] = 0

(d) (10 points) Provide a pseudocode for the operation DISHONEST-SUCCESSOR(x)

DISHONEST-SUCCESSOR(x) if n[right[x]] 6 = 0 then y←right[x] while n[lef t[y]] 6 = 0 or (dishonest[y] = 0 and n[right[y]] 6 = 0) do if n[lef t[y]] 6 = 0 then y←lef t[y] else y←right[y] return y y←first ancestor of x such that x in left subtree of y if y 6 = N IL then if dishonest[y] then return y else return DISHONEST-SUCCESSOR(y) else return N IL

exists if one rectangle entirely covers another, even if the boundary lines do not intersect. (Hint: Move a “sweep” line across the set of rectangles and maintain their heights in an interval tree).

ANSWER: Move a sweep line from left to right, while maintaining the set of rectangles currently intersected by the line in an interval tree. The interval tree will organize all rectangles whose x interval includes the current position of the sweep line, and will be based on the y intervals of the rectangles, so that any overlapping y intervals in the interval tree correspond to overlapping rectangles.

To emulate a sweep line, sort the rectangles by their x-coordinates. Each rectangle must appear twice in the sorted list, once for its left x-coordinate and once for its right x-coordinate.

Now scan the sorted list (from lowest to highest in x-coordinate).

Entering a rectangle:

When an x-coordinate of a left edge it found, check whether the rectangle’s y-ccordinate interval overlaps an interval in the tree, and insert the rectangle (keyed on its y-coordinate interval) into the tree.

Exiting a rectangle:

When a x-cordinate of a right edge is found, delete the rectangle from the interval tree.

The interval tree always contains the set of “open” rectangles intersected by the sweep line. If an overlap is ever found in the interval tree, there are overlapping rectangles.

The total time is O(n log n) to sort the rectangles (e.g. mergesort, heapsort) and O(n log n) for interval- tree operations (insert, delete, check for overlap).