0% found this document useful (0 votes)
10 views19 pages

Tut 10

This document discusses binary search trees (BSTs) and their use for predecessor and successor queries. It provides examples of BSTs on sample data sets and the nodes accessed to find predecessors and successors of given values. It also describes how to construct a balanced BST from a sorted array in O(n) time by recursively building the left and right subtrees, and how to rebalance BSTs through rotations when insertions cause height imbalances.

Uploaded by

Daniel Leung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views19 pages

Tut 10

This document discusses binary search trees (BSTs) and their use for predecessor and successor queries. It provides examples of BSTs on sample data sets and the nodes accessed to find predecessors and successors of given values. It also describes how to construct a balanced BST from a sorted array in O(n) time by recursively building the left and right subtrees, and how to rebalance BSTs through rotations when insertions cause height imbalances.

Uploaded by

Daniel Leung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Tutorial 10

Tutorial 10

CSCI2100 Teaching Team

Department of Computer Science and Engineering


The Chinese University of Hong Kong

1/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Binary Search Tree Example

Two possible BSTs on S = {3, 11, 12, 15, 18, 29, 40, 41, 47, 68, 71, 92}:
3

11

12

92
40
71
15 68
68

11 29 41 92 15

18
3 12 18 47 71

47

41

40

29

2/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Predecessor Query

Let S be a set of integers. A predecessor query for a given integer q is to


find its predecessor in S, which is the largest integer in S that does not
exceed q.

3/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Example

Suppose that S = {3, 11, 12, 15, 18, 29, 40, 41, 47, 68, 71, 92} and we have
a balanced BST T on S:
40

15 68

11 29 41 92

3 12 18 47 71

We want to find the predecessor of q = 42 in S.


Nodes accessed: 40, 68, 41, and 47.

4/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Successor Query

Let S be a set of integers. A successor query for a given integer q is to


find its successor in S, which is the smallest integer in S that is no
smaller than q.

5/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Example

We want to find the successor of q = 17 in S.


40

15 68

11 29 41 92

3 12 18 47 71

Nodes accessed: 40, 15, 29, and 18.

6/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Construction of a Balanced BST

In the following, we will discuss how to construct a balanced BST T on a


sorted set S of n integers in O(n) time.

7/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Construction of a Balanced BST

Assume that S is stored in an array A and A is sorted.


Observation: The subtree of any node in a balanced BST is also a
balanced BST.

Main idea:
The median A[b n+1
2
c]

balanced BST of b n−1


2
c nodes balanced BST of d n−1
2
e nodes

8/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Example

Let us construct a balanced BST T on the following sorted array A.

29

12 47

3 15 40 71

11 18 41 68 92

A 3 11 12 15 18 29 40 41 47 68 71 92

9/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Construction of a Balanced BST

Let f (n) be the maximum running time for constructing a balanced BST
from an array of length n. We have:

f (1) = O(1)
f (n) = O(1) + 2 · f (⌈n/2⌉)
Solving the recurrence gives f (n) = O(n).

10/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Rebalancing

In lectures we explored the Left-Left and Left-Right cases in detail, so


here we will look at Right-Right and Right-Left:

h h+2 h h+2

h or h + 1 h+1 h+1 h

11/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Right-Right

Fix by a rotation (symmetric to left-left):

a b
h h+2
x+1 h+1
b
a
x h+1
h x
A =⇒

B A B
C

Note that x = h or h + 1, and the ordering from left to right of


A, a, B, b, C is preserved after rotation.

12/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Right-Left

Fix by a double rotation (symmetric to left-right):

a c
h h+2 h h+2 h+1 h+1

b a b
h x y h
h+1 h h
⇒ ⇒
A c

x y
A B C D
D

B C

Note that x and y must be h or h − 1. Futhermore at least one of


them must be h.
13/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Right-Right Example

Inserting 50:
20 20 30
1 ? 1 3 2 2

10 30 10 30 20 40
1 ? 1 2 1 1 0 1
⇒ ⇒

25 40 25 40 10 25 50
? 0 1

50 50

14/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Right-Left Example

Inserting 38:
30 30 35
2 ? 2 4 3 3

20 40 20 40 30 40
1 1 ? 2 1 1 3 2 2 1 2 2

10 25 35 50 10 25 35 50 20 33 37 50
1 ? 0 1 1 2 0 1 1 1 0 1 0 1
⇒ ⇒

33 37 60 33 37 60 10 25 38 60
0 ? 0 1

38 38

15/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Range Reporting

Let S be a set of n integers. Given an interval [q, ∞), a range query


reports all the integers of S that fall in [q, ∞). Describe an algorithm to
use a balanced BST on S to answer a query in O(log n + k), where k is
the number of integers reported.
35

30 40

20 33 37 50

10 25 38 60

For the query [27, +∞), we need to report the integers in pink.

16/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Range Reporting

To answer a query [q, ∞), we do the following at the root:


If a < q, recursively report the integers in T2 that fall in [q, ∞).
If a = q, report a and all the integers in T2 .
If a > q, report a and all the integers in T2 . After that, recursively
report the integers in T1 that fall in [q, ∞).

T1 T2

17/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Range Reporting

The tutor will explain the algorithm using [27, +∞) as the example query.

35

30 40

20 33 37 50

10 25 38 60

18/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
In each level of the recursion, we do the following:
Compare q to the integer stored in the root, the cost of which is
O(1).
(If necessary) report all the integers in the right subtree, the cost of
which is proportional to the number of integers in the right subtree.
As the height of the BST is O(log n), the first bullet costs O(log n) in
total. The second step reports integers from disjoint subtrees and,
therefore, incurs cost O(k) in total. The overall cost is O(log n + k)

19/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10

You might also like