You are on page 1of 9

Hong Kong University of Science and Technology

Department of Computer Science


Comp171: Data Structure and Algorithm
Written Assignment 2 Solution


1.
Main idea:
We will use the property of binary search tree to do the range search. For a certain node in
the binary search tree, we check whether the value v stored in the node n falls into the range
and if it does we report it. If (v<i) we know that we dont have to check the left child of n, as
all the values under the left child are smaller than v, therefore smaller than i. Similarly, if
(v>j), we dont have to check the right child of n. We can use the following figure to show
our strategy.
Pseudo code:
We define a function void BST_Find_Range(BSTnode* bn, int i, int j) to find all the nodes
that contain value which falls between the range [i, j] in a binary search tree with the root
node bn.
void BST_Find_Range(BSTnode* bn, int i, int j)
{
if (bn= =NULL) return; // if bn is a null node, do nothing.
int value=bn->value; // retrieve the value stored in the node bn.
if (value < i) BST_Find_Range(bn->right, i, j) // if value<i, we only have to check
the right child of bn.
if (value > j) BST_Find_Range(bn->left, i, j) // if value>j, we only have to check
the left child of bn.
else { // the value falls within the range, we report it and we have to check both of
its left and right child.
output(value);
BST_Find_Range(bn->left, i, j);
BST_Find_Range(bn->right, i, j)
}
}

2.
(a)

(b)

20
11
10
1
Inserting 11
12
11
10
1
20
Inserting 12 Inserting 13
20
11
12
10
13 1
Inserting 14 Inserting 15
14
11
12
10
13 1 20
14
11
12
10
13 1 20
15
Deleting 11
14
12
10
13 1 20
15
15
13
10
14 1 12
Deleting 12 Deleting 13
15
14
10
1 20





3.

6 4
6
2
4
6
Inserting 7 Inserting 10
6
2
4
6
7
10
Inserting 11
6
2
4
6
7
10
11
2
4
6
7
Deleting 14
20
15
10
1
Deleting 15
20
10
1
Inserting 6 Inserting 4 Inserting 2

Inserting 13
6 10
2
4
6
7

10
11
13
Inserting 15
6 10
2
4
6
7

10
11
13
15
Inserting 5
6 10
2
4
5
6
7

10
11
13
15
Inserting 3
6 10
2
3
4
5
6
7

10
11
13
15
Inserting 12
6 10 12
2
3
4
5
6
7

10
11

12
13
15

Inserting 14
6 10 12
2
3
4
5
6
7

10
11

12
13
14
15


Inserting 9
6 10 12
2
3
4
5
6
7
9

10
11

12
13
14
15
Inserting 8
6 10 12
2
3
4
5
6
7
8
9
10
11

12
13
14
15
Inserting 16
6 10 12 14
2
3
4
5
6
7
8
9
10
11

12
13


14
15
16

Inserting 1
3 6
3
4
5
6
7
8
9
10
11

12
13


14
15
16

12 14
10
1
2







Inserting 18
3 6
3
4
5
6
7
8
9
10
11

12
13


14
15


12 14 16
10
1
2
16
17
18

Inserting 17
3 6
3
4
5
6
7
8
9
10
11

12
13


14
15
16
17
12 14
10
1
2
4.

Deleting 5
Deleting 6
3 6
3
4
6
7
8
9
10
11

12
13


14
15


12 14 16
10
1
2
16
17
18

3 7
3
4

7
8
9

10
11

12
13


14
15


12 14 16
10
1
2
16
17
18








Deleting 7
Deleting 8
3 9
3
4

9
10
11

12
13


14
15


14 16
12
1
2
16
17
18

3 8
3
4

8
9

10
11

12
13


14
15


12 14 16
10
1
2
16
17
18


Deleting 10
3 9
3
4

9
11

12
13


14
15


14 16
12
1
2
16
17
18

Deleting 11
3 12 14 16
3
4
9

12
13


14
15


1
2
16
17
18

You might also like