Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

DATA STRUCCTURES

Assignment 2

Deadline: Saturday 18th April, 2020

Question 1: (10 marks)

a) Show the result of inserting 3, 1, 4, 6, 9, 2, 5, and 7 in an initially empty binary search


tree. Then show the result of deleting the root.

1 4

2 6

5 9

Deleting root 7

1
4

1 6

2 5 9

Result = 3, 4, 5
5 6
2 3

1 6 1 9

2 9 2 7

7
7 9 2
4 5 6 7

1 9 1 1
1

2 2

Result = 3, 4, 5, 6, 7, 9, 2, 1.

b) Show the result of inserting 2, 1, 4, 5, 9, 3, 6, and 7 into an initially empty AVL tree.

-2 -1
2 2
-2
0 -2 0 0 2

1 5 1 1
1 4
-1 0 0 1 5
left rotate
5 4 9 Right, Left 1 0
4 9
0 0
-2 9
2 -1 3
4
1 1
0
1 4 -2
2 5
1 0
5 0 0 1 Right, Left
9
1
3 9
0
3 -1 0
6
4
0 -1
2 6
0 0 0 1
1 3 9
5
0
7
c) Compare AVL tree to binary search tree. Which is faster? How much faster?

Answer:

Binary Search Tree AVL Tree


1) A Binary tree, at every node Root is 1) AVL Tree is defined as the balanced
greater than Left child and Root is smaller Binary Search Tree. Here Balance means at
than its Right child. every node the difference in the height of Left
Sub Tree and the Right Sub Tree is -1, 0 or 1.
If the balance factor is other than these three
values then the tree is not called balanced
tree.

2) For a n node binary search tree max level 2) AVL Tree number of levels is always log
could be n and min log (n) (n).

3) In worst case the cost Binary Search Tree 3) The cost of AVL tree is O (nlogn) in all
is O(n^2) and O(n log n) in Best Case cases (best case, average case and worst case).

Question 2: (10 marks)


Write the preorder, inorder and postorder traversals of the binary tree shown below.
Preorder: A, B, D, G, I, C, E, H, J, K, F.
Inorder: I, G, D, B, A, J, H, K, E, C, F.
Postorder: I, G, D, B, J, K, H, E, F, C, A.

You might also like