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

CT077-3-2-DSTR Individual Assignment Page 1 of 34

Name : SASVINAA KANDASAMY


Student ID : TP053388
Intake : UC2F1908SE
Lab Group : LAB 1

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 2 of 34

Answer:
An Operating System (OS) is a system software which is an interface between a computer user
and computer hardware where performs all the important functions of an operating system such
as memory management, processor management, security, file management, handling input
and output, and device management. Memory management in operating system is the
functionality which is responsible for handling and managing primary memory of computer.

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 3 of 34

Stack concept is a special part of your computer's memory which is a linear data structure. It
stores temporary variables which is created by every function including the main function().In
computing memory, a stack serves as a collection of elements with push and pop principle
operations. Push is the addition of an element to the collection whereas pop is elimination of
the most recent added element which have not yet eliminate. A method is used in this case
called Last In First Out(LIFO). This meant it involves a series of addition and elimination
operations which will be done in an order manner. The computer uses this method to service
data request which is received by the computer's memory. LIFO method stated that if the data
added or stored last in any stack, it should be the first eliminate. Stack should at least support
the two operations which are push and pop. Let demonstrate of stack behaviour. For example,
restaurant plates dispenser in figure below. For the push operation, the plates being pushed to
add plates (data items) to the plate dispenser(stack). The first plate was pushed to the bottom
of the stack, proceeded by all the following plates. The first data item added is unreachable and
stayed at the bottom of the stack. In pop operation, the last plate (data item) added is stay at the
top of the stack. When the first item to be removed, this data item is popped out of the stack.
The items continue to pop out in this order in computer memory.

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 4 of 34

Answer:

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 5 of 34

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 6 of 34

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 7 of 34

Infix Expression : A+B-F *(C*D-E)


Token Action Result Stack Notes
A Add A to the result A
+ Push + to the stack A +
B Add B to the result AB +
-same priority as + . Thus, pop + and
- Push - to the stack AB+ -
push -

Postfix Expression:

Infix Expression : A+B-F *(C*D-E)


Reverse Infix Expression : )E-D*C(*F-B+A
Reverse brackets: (E-D*C)*F-B+A
Token Action Result Stack Notes

Prefix Expression (Reversed result):

Your answer:

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 8 of 34

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 9 of 34

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 10 of 34

Answer:

i) Quick Sort is concept of Divide and Conquer in order to gain benefits as the merge sort, but
it is not using additional storage. In quick sort all the heavy major work is complete while
dividing the array into subarrays. Quick Sort is also called partition-exchange sort. First and
foremost, a quick sort selects a value, which is pivot value. It can be the first element which
can be any element from the array. The main aim of the pivot value is to help in splitting the

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 11 of 34

list and split point which is used for quick sort's division. Two main basic operations are done
in algorithm which are swapping items in place and partioning a section of the array.
Below is a diagram of an array, which have to be sorted. We will use the quick sort algorithm
to sort this array:

Let's consider an array with values

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 12 of 34

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 13 of 34

ii) explanation with respect to the shopping list items:

By applying the same concept of quick sorting algorithm as mentioned above we find the below
sorted list:

We can prioritize the list of items to be purchased as per the priority by applying the quick sort
algorithm. Meat is most essential than bread then water and then eggs and so on. According to
the list before applying quick sorting, egg was the first element to be purchased but then the
priority of purchase is lower than the bread, water and meat so it is moved to 4th position for
purchase. Similarity detergent has 5th priority and milk have the lowest priority after sorting.

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 14 of 34

Answer:
DEFINITION

Stack is a Last In First Out (LIFO) based linear data structure and it has only one side
which entry point in stack(top) and has exit point in stacks. The stack supports below core
operations such as Push, Pop, Peek and is Empty. On the other hand, Queue is defined by
its property of First In First Out(FIFO) based data structure and it has two points which
are entry and exit. Queue supports operations such as enqueue, dequeue, peek and is Empty

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 15 of 34

ILLUSTRATION

IMPLEMENTATION

In this question above we will be using stack data structure in queue data structure for
storing the data. Stack data structure uses push and pop operations to implement a queue.
Two stacks can implement queue. Queue can be implemented in two methods which are
method 1(by making enQueue operation costly) and method 2(by making deQueue
operation costly).

Method 1(By making enQueue operation costly)

In this approach, we will make sure that the first element inserted is at the top of the stack.
We need two stacks to achieve this. There are solution steps will be involved at the same
time enqueuing a new element to the queue. The function of first stack 1 is to store the

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 16 of 34

data whereas the stack 2 being used to assist and store data temporarily during operations.
Stack 2 is used to put the element at top of stack 1.

The solution steps for algorithm for insertion:

1) Take two stacks which are S1 and S2


2) Insert directly into S1 if S1 is empty.
3) While S1 is not empty, push all the elements from S1 to S2
4) Push those elements to be inserted into S1.
5)Push everything back from S2 to S1.

The solution steps for algorithm for deletion:

1) If S1 is not empty, pop-out the elements from S1


2) Return-1 if S1 is not empty.

Complexity Analysis:

1) time complexity: - push operations(O(N) for insertion) , pop operation( O(N) for deletion)
2) space complexity - the usage of stack for storing values.

Method 2 (By making deQueue operation costly)

The new element is inserted at the top of stack 1 using push () operation in this method, in en-
queue operation. For de-queue operation, if stack 2 is empty then all the elements will move to
stack 2 and then the top of stack 2 is returned.

The solution steps for algorithm for insertion:

1) Take two stacks which are S1 and S2.


2) Push all to S1 taking into consideration which S1 has unlimited size.

The solution steps for algorithm for deletion:

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 17 of 34

1) Return -1 if S1 and S2 are empty


2) Push all to S2 from S1
3)Pop the top element from S2

Complexity Analysis:

1) time complexity: - (O(N) for dequeue), ( O(1) for enqueue)


2) space complexity - the usage of stack for storing values.

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 18 of 34

Answer:
i) False. Both are not same. The definitions are quite similar but logically
independent. This is because a full binary tree is called as proper binary tree or 2-
tree which is every node is either a leaf or possesses exactly two child nodes. At
the same time, a complete binary tree if all the levels ae filled except possibly the
last level, and all nodes are as far left as possible.

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 19 of 34

ii) True. Binary tree is not same to binary search tree. Binary tree is a hierarchical
data structure where each node can have at most 2 child nodes. On the other hand,
binary search tree is an ordered binary tree where the nodes are arranged in order
which means for each node, all elements in left subtree have a key with less value
to the node(<) whereas all the elements in right subtree have higher value than the
node(>).There are a lot of differences between binary tree and binary search tree.

The differences are shown in table form below:

iii) True. The tree structure can access data very efficiently. This is the main benefit
of using tree structure.
The access time is O(h) where h is the height of the tree.

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 20 of 34

When a Binary search tree is balanced with n nodes, h = O (log n). For instance, a balanced
BST. The diagram shown below:

Thus, the access time is O (log n) which is much better than O (n) provided by linear data
structure such as an array or linked list but if not balanced, a BST may degenerate into a linear
structure. BST becomes degenerate tree and search take O(n) time when 1,2,3 ... n is inserted
into BST.

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 21 of 34

Hence BST must be balanced. AVL is a balanced BST and ensures the height of the tree is
always O(log n) by rotations.
Search, insertion and deletion in AVL tree is always log n always whereas search, insertion
and deletion in BST is O(n) in worst case.

iv) True. we have to print the nodes of the binary expression tree utilizing all together traversal
of the tree. The essential activity of building a binary expression tree from a post-fix/prefix
articulation is like that of assessing post-fix articulation.

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 22 of 34

Answer:

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 23 of 34

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 24 of 34

ii) Post-order traversal of BST

Left right, Left, Right, Root

E, C, A, L, S, O, I, Y, U, T, F

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 25 of 34

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 26 of 34

iv) We must remove node t from BST. Node T has two Childs in BST. We must
replace node T with inorder predecessor or inorder successor. I choosed inorder
predecessor.
Here goes inorder predecessor for T is S.
The diagram shown below:

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 27 of 34

Answer:
i) A spanning tree of a graph is a tree and it is a subgraph which include all the vertices.
This graph might have a lot of spanning tree. The spanning tree of a graph do not form a
cycle. It is a collection of connected edges which contain every vertex in the graph.
Furthermore, this is a kind of tree that is induced form the graph with includes all the
vertices of the graph, but the number of edges is totally different. The number of edges in
the MST is equal to the n-1. Moreover, each edge of the graph has some numerical value
associated with it known as weighted graphs. The MST is the tree such that is the cost of
the all the edges included in it will be minimum.

ii) Algorithm is Prims:

Consider cable company has 3 neighbourhoods having three weight parameters (w1, w2, w3)
on each edge between them. Apply Prim's-MST to identify the optimal solution considering all
the weights

1. Sort the weights according the increasing order of weight.


2. Take the first edge correspond to lowers weight and add to the Prims.

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 28 of 34

3. Similar do the seconds second weight.

For example, consider the graph having vertices as A, B, C with


w1= 5
w2= 6
w3=7

Now sorting the weights in increasing order:


w1, w2, w3

Now, add w1 to MST and then w2 to MST which will give the whole MST.
The cost of the optimal MST = 5 + 6 = 11

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 29 of 34

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 30 of 34

Answer:

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 31 of 34

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 32 of 34

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 33 of 34

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01


CT077-3-2-DSTR Individual Assignment Page 34 of 34

References:

1. TK, 2017. Everything you need to know about tree data structures. [Online]
Available at: https://medium.freecodecamp.org/all-you-need-to-know-about-tree-data-
structures-bceacb85490c
[Accessed 12 june 2020]

2. Tutorialspoint, 2018. Data Structure and Algorithms - Queue. [Online]


Available at:
https://www.tutorialspoint.com/data_structures_algorithms/dsa_queue.htm
[Accessed 13 june 2020].

3. Tutorialspoint, 2018. Data Structure and Algorithms - Tree. [Online]


Available at:
https://www.tutorialspoint.com/data_structures_algorithms/tree_data_structure.htm
[Accessed 13 june 2020].

4. Wikibooks, 2018. Data Structures/Stacks and Queues. [Online]


Available at: https://en.wikibooks.org/wiki/Data_Structures/Stacks_and_Queues
[Accessed 13 june 2020].

Level 2 Asia Pacific University of Technology and Innovation 2020/06/01

You might also like