Data Structure and Algorithm
Assignment 4
Submitted by:
Sammad Farooq
Registration No:
SP18-BSE-133
Submitted to:
Sir Tanveer Ahmed
Date 9/11/2019
COMSATS UNIVERSITY ISLAMABAD
Question No:01
a) Reconstruct a Binary Tree from the following in-order and pre-order traversal.
In-order Traversal: 4,7,2,8,5,1,6,9,3
Pre-order Traversal: 1,2,4,7,5,8,3,6,9
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
Post-order Traversal: 5,4,3,2,1
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
ii. Perform in order traversal on BST created in part i
In-order Traversal(left-root-right):
The in-order traversal will be 1,2,3,4,5,6,9
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
for j=start to end-1
if A[i]<=pivot
swap A[i] with A[j]
i=i+1
swap A[i] with A[end]
return i
b) Consider the following data items in an array.
7,6,12,3,11,8,7,1,15,13,17,5,16,14,9,4,10
6
12
11
1 5 15
8
4
13
9
17
14
10
16
ii. Perform in order traversal on BST created in part i
In-order Traversal(left-root-right):
The in-order traversal will be 1,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17
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
for j=start to end-1
if A[i]<=pivot
swap A[i] with A[j]
i=i+1
swap A[i] with A[end]
return i
c) Consider the following data items in an array.
2,9,11,21,33,45,56,78,89,110
i. Draw Binary Search Tree (BST)
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):
The in-order traversal will be 2,9,11,21,33,45,56,78,89,110
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
for j=start to end-1
if A[i]<=pivot
swap A[i] with A[j]
i=i+1
swap A[i] with A[end]
return i
Question:
a) Represents the following expressions as a binary tree and write the prefix and postfix forms of the
expression.
i. (A*B+C*D) –(A/B- (D+E))
+ -
+
* * /
D
B
C D
B
A A
B
Prefix: -+*AB*CD-/AB+DE
Postfix: AB*CD*+AB/DE+--
ii. (((A+B)*C+D)*E) – ((A+B)*C-D)
Prefix: -**+AB+CDE*+AB-CD
Postfix: AB+CD+*E*AB+CD-*-
-
*
*
* -
E +
+
C D
A
+ B
A B
C D