Data Structure and Algorithm Assignment 4: Comsats University Islamabad
Data Structure and Algorithm Assignment 4: Comsats University Islamabad
Assignment 4
Submitted by:
Sammad Farooq
Registration No:
SP18-BSE-133
Submitted to:
Sir Tanveer Ahmed
Date 9/11/2019
6
5
4
8 9
b) Reconstruct a Binary Tree from the following in-order and post-order traversal
In-order Traversal: 1,2,3,4,5
5
Question:No:02
i. Consider the following data items in an array. i. Draw Binary Search Tree (BST).
3,1,4,1,5,9,2,6,5,3
1 4
2 5
In-order Traversal(left-root-right):
iii. Illustrate how Quick Sort works on above array. Assume that Quick sort uses the last
item in the list as pivot element
Pseudo code:
Pivot=A[end]
I=start
if A[i]<=pivot
swap A[i] with A[j]
i=i+1
return i
6
12
11
1 5 15
8
4
13
9
17
14
10
16
In-order Traversal(left-root-right):
iii. Illustrate how Quick Sort works on above array. Assume that Quick sort uses the last
item in the list as pivot element
Pseudo code:
Partition(A, start, end)
Pivot=A[end]
I=start
if A[i]<=pivot
i=i+1
return i
11
21
33
45
56
78
89
110
ii. Perform in order traversal on BST created in part i
In-order Traversal(left-root-right):
iii. Illustrate how Quick Sort works on above array. Assume that Quick sort uses the last
item in the list as pivot element
Pseudo code:
Pivot=A[end]
I=start
if A[i]<=pivot
i=i+1
return i
Question:
a) Represents the following expressions as a binary tree and write the prefix and postfix forms of the
expression.
+ -
+
* * /
D
B
C D
B
A A
B
Prefix: -+*AB*CD-/AB+DE
Postfix: AB*CD*+AB/DE+--
Prefix: -**+AB+CDE*+AB-CD
Postfix: AB+CD+*E*AB+CD-*-
-
*
*
* -
E +
+
C D
A
+ B
A B
C D