Documente Academic
Documente Profesional
Documente Cultură
http://cse.iitkgp.ac.in/~abhij/course/theory/Algo1...
CS29003 Algorithms
Laboratory
1 of 4
CS21003/CS29003
http://cse.iitkgp.ac.in/~abhij/course/theory/Algo1...
Sample Output
n = 40
2 of 4
CS21003/CS29003
http://cse.iitkgp.ac.in/~abhij/course/theory/Algo1...
m = 20
+++ Inserting 40 random integers between 1 and 20
1 (1)
1 (2)
1 (3)
1 (4)
2 (1)
4 (1)
4 (2)
5 (1)
5 (2)
5 (3)
8 (2)
9 (1)
9 (2)
9 (3)
9 (4)
11 (2)
11 (3)
12 (1)
13 (1)
13 (2)
17 (1)
18 (1)
18 (2)
18 (3)
19 (1)
+++
+++
+++
+++
+++
+++
+++
+++
+++
+++
Search
Search
Search
Search
Search
Search
Search
Search
Search
Search
+++ Delete
1 (1)
4 (1)
8 (2)
11 (2)
18 (1)
+++ Delete
1 (1)
4 (1)
8 (2)
14 (1)
19 (3)
+++ Delete
1 (1)
4 (1)
10 (1)
16 (1)
19 (4)
+++ Delete
1 (1)
4 (1)
10 (1)
16 (1)
19 (4)
+++ Delete
1 (1)
4 (1)
10 (1)
18 (2)
(
(
(
(
(
(
(
(
(
(
12)
18)
5)
12)
6)
12)
7)
17)
12)
20)
=
=
=
=
=
=
=
=
=
=
( 17):
1 (2)
4 (2)
9 (1)
11 (3)
18 (2)
( 9):
1 (2)
4 (2)
10 (1)
16 (1)
19 (4)
( 7):
1 (2)
4 (2)
11 (1)
16 (2)
3
5
9
14
19
(1)
(4)
(5)
(1)
(2)
3
7
10
16
19
(2)
(1)
(1)
(1)
(3)
3
8
11
16
19
(3)
(1)
(1)
(2)
(4)
1
3
4
1
0
1
1
1
1
0
1
5
9
12
18
(3)
(1)
(2)
(1)
(3)
1
5
9
13
19
(4)
(2)
(3)
(1)
(1)
2
5
9
13
19
(1)
(3)
(4)
(2)
(2)
3
5
9
14
19
(1)
(4)
(5)
(1)
(3)
3
7
10
16
19
(2)
(1)
(1)
(1)
(4)
3
8
11
16
(3)
(1)
(1)
(2)
1
5
11
16
(3)
(1)
(1)
(2)
1
5
11
18
(4)
(2)
(2)
(1)
2
5
11
18
(1)
(3)
(3)
(2)
3
5
12
18
(1)
(4)
(1)
(3)
3
7
13
19
(2)
(1)
(1)
(1)
3
8
13
19
(3)
(1)
(2)
(2)
1
5
11
18
(3)
(1)
(2)
(1)
1
5
11
18
(4)
(2)
(3)
(2)
2
5
12
18
(1)
(3)
(1)
(3)
3
5
13
19
(1)
(4)
(1)
(1)
3
8
13
19
(2)
(1)
(2)
(2)
3
8
14
19
(3)
(2)
(1)
(3)
( 20):
1 (2)
4 (2)
11 (1)
16 (2)
1
5
11
18
(3)
(1)
(2)
(1)
1
5
11
18
(4)
(2)
(3)
(2)
2
5
12
18
(1)
(3)
(1)
(3)
3
5
13
19
(1)
(4)
(1)
(1)
3
8
13
19
(2)
(1)
(2)
(2)
3
8
14
19
(3)
(2)
(1)
(3)
( 11):
1 (2)
4 (2)
12 (1)
18 (3)
1
5
13
19
(3)
(1)
(1)
(1)
1
5
13
19
(4)
(2)
(2)
(2)
2
5
14
19
(1)
(3)
(1)
(3)
3
5
16
19
(1)
(4)
(1)
(4)
3 (2)
8 (1)
16 (2)
3 (3)
8 (2)
18 (1)
Cleaning up...
Note: Although you do not need to balance the height of BSTs supporting
duplicates, think about the difficulties in the process. In the extreme situation when
all the n values inserted in T are equal, we get a tree of height n 1. As per the
definition, this tree cannot be height balanced.
3 of 4
CS21003/CS29003
http://cse.iitkgp.ac.in/~abhij/course/theory/Algo1...
In order to make height balancing possible, we require both the left and the right
subtrees to contain equal values. We insert in the above way (equal values initially
go to the right subtree), but subsequent rotations may bring some equal values to
the left subtree. In this case, the BST ordering is the lexicographic ordering on the
pair (value,insertion_time). Although insertion times are not explicitly stored in the
nodes, this insertion with rotations will respect the lexicographic ordering. But then,
how will search and delete behave? Will they still be doable in O( h) time?
4 of 4