Data Structuresand Algorithms Assignment
Data Structuresand Algorithms Assignment
CCS/00066/022
CCS/00097/022
CCS/00100/022
Question 1
1. Given the binary tree operations, what tree or trees does the following sequence of
statements produce?
Public void Ex11(){
BinaryTree<Integer> t1 = new
BinaryTree<Integer>(2); t1.attachLeft(5)
BinaryTree<Integer> t2 = new
BinaryTree<Integer>(8); t2.attachLeft(6);
t2.attachRight(7); t1.attachRightSubtree(t2);
BinaryTree<Integer> t3 = new
BinaryTree<Integer>(7); t3.atachLeft(3);
t3.attachRight(1);
Answer
1
2 7
5 8 3 1
6 7
Question 2
2. Execute the following sequence of operations on an initially empty ADT(abstract data
type) implemented as A binary search tree .And show the underlying tree after each
17
operation. t.treeInsert(17) t.treeInsert(78)
17
78
t.treeInsert(20)
17
78
20
t.treeInsert(57)
17
78
20
57
t.treeInsert(51)
17
78
20
57
51
t.treeDelete(17)
78
20
57
51
t.treeInsert(60)
78
20
57
51 60
\
t.treeInsert(70)
78
20
57
51 60
\
70
t.treeInsert(40)
78
20
57
51 60
\
40 70
t.treeDelete(57)
78
20
51
40 60
\
70
t.treeInsert(90)
78
20
51
40 60
\
70
90
t.treeInsert(19)
78
20
19 51
40 60
\
70
90
t.treeDelete(20)
78
19
51
40 60
\
70
90
t.treeDelete(70)
78
19
51
40 60
\
90
Question 3
3. Create BSTs from the same set of data presented each time in a different order:
1. 17 4 14 19 15 7 9 3 16 10 2. 9 10 17 4 3 7 14 16 15 19 3. 19 17 16 15 14 10 9 7 4 3
Order 1: 17, 4, 14, 19, 15, 7, 9, 3, 16, 10
17
4 19
3 14
7 15
\
9 16
10
Order 2: 9, 10, 17, 4, 3, 7, 14, 16, 15, 19
4 10
3 7 17
14 19
\
16
15
Order 3: 19, 17, 16, 15, 14, 10, 9, 7, 4, 3
19
17
16
15
\
14
10