Tải bản đầy đủ (.pdf) (38 trang)

assignment name assignment 1 data structure and algorithm

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.38 MB, 38 trang )

<span class="text_page_counter">Trang 1</span><div class="page_container" data-page="1">

1. Data Structure ...4

</div><span class="text_page_counter">Trang 2</span><div class="page_container" data-page="2">

2. Stack ...4

3. Queue ...5

4. Link list ...8

5. How stack help to store the required data? ...8

Binary Search Tree: ...9

What is a Binary Search Tree? ...9

Demonstration of the insertion operation ...11

II. Determine the operations of a memory stack and how it is used to implement function calls in a computer ...14

1. The operations of a memory stack ...14

2. LIFO (Last In First Out) ...14

3. Stack Operations ...15

3.1 The Push Operation ...15

3.2 The Pop Operation ...15

3.3 The Peek Operation ...15

3.4 The Search Operation ...16

4. How it is used to implement function calls in a computer stack memory in computers ..16

5. Stack memory implements function call in computer ...18

III. Illustrate, with an example, a concrete data structure for a First In First out (FIFO) queue. ...19

1. First in First out (FIFO) Queue: ...19

FIFO Queue basic operations: ...20

Example of selection sort:...25

Comparison between bubble sort and selection sort: ...26

IV. Examine the advantages of encapsulation and information hiding when using an ADT. ....27

Advantages of encapsulation ...27

</div><span class="text_page_counter">Trang 3</span><div class="page_container" data-page="3">

V. Using an imperative definition, specify the abstract data type for a software stack. ...28

Specify the abstract data type for a software stack...28

VI. Two network shortest path algorithms ...30

3. Two network shortest path algorithms.

4. Explanation on how to specify an abstract data type using the example of software stack.

5. Explanation of the advantages of encapsulation and information hiding when using an ADT.

6. Discussion of imperative ADTs with regard to object orientation.

</div><span class="text_page_counter">Trang 4</span><div class="page_container" data-page="4">

I. Create a design specification for data structures explaining the valid operations that can be carried out on the structures.

1. Data Structure

A data structure is a container that stores data in a specific layout. This “layout” allows a data structure to be efficient in some operations and inefficient in others. There are different kinds of the data structure which are as follows:

Return top element of stack. Is Empty:

Returns true if stack is empty, else false.

</div><span class="text_page_counter">Trang 5</span><div class="page_container" data-page="5">

Example code of stack When you input a number, for example input = 6:

return 77; multiply(Integer 11 * 7 = 77) –

minus(Integer - 26 – 15 = 11) plus(Integer - 6 + 20 = 26)

Input(Integer - 6)result(Integer - multiply)

data(Integer - 6)Scan(Scanner - System.in)

3. Queue

Queue is also an abstract data type or a linear data structure, just like stack data structure, in which the first element is inserted from one end called the REAR (also called tail), and the removal of existing element takes place from the other end called as FRONT (also called head). This makes queue as FIFO (First in First Out) data structure, which means that element inserted first will be removed first.

</div><span class="text_page_counter">Trang 6</span><div class="page_container" data-page="6">

Which is exactly how queue system works in real world. If we go to a ticket counter to buy movie tickets, and are first in the queue, then we will be the first one to get the tickets. Right? Same is the case with Queue data structure. Data inserted first, will leave the queue first.

The process to add an element into queue is called Enqueue and the process of removal of an element from queue is called Dequeue.

Fig: Queue data structure

</div><span class="text_page_counter">Trang 7</span><div class="page_container" data-page="7">

Example queue code

Output of the code

</div><span class="text_page_counter">Trang 8</span><div class="page_container" data-page="8">

4. Link list

It is a data structure that contains a finite sequence of n data element. Linked list is the second most-used data structure after array.

Linked list can be visualized as a chain of nodes, where every node point to the next node.

Fig: representation of the link list. Types of link list:

Following are the various types of the link list:

Simple Linked List − Item navigation is forward only.

Doubly Linked List − Items can be navigated forward and backward.

Circular Linked List − Last item contains link of the first element as next and the first element has a link to the last element as previous.

Basic operations:

Following are the basic operation supported by the link list: Insertion − Adds an element at the beginning of the list. Deletion − Deletes an element at the beginning of the list. Display − Displays the complete list.

Search − Searches an element using the given key. Delete − Deletes an element using the given key. 5. How stack help to store the required data?

Basically, stack is used to store the data and it has small size so, that the large amount of data cannot be store in the stack. After inserting the data then only the storing of the data could be

</div><span class="text_page_counter">Trang 9</span><div class="page_container" data-page="9">

done. For the insertion of the data I am using the link list data structure and in order to store the data I am using the stack data structure.

Binary Search Tree:

What is a Binary Search Tree?

Binary Search Tree (BST) is a basic data structure in which each node has at most two children, referred to as left and right children, and the top node in the tree is called root. It also fulfills the binary search requirement, stating that the key in each node must be greater than or equal to any key stored in the left sub-tree, and less than or equal to any key stored in the correct sub-tree. Binary search tree enables fast search, addition and removal of items. They keep their keys in order, so that other searches and operations can apply the principle of Binary Search: when looking for a key in a tree (or where to insert a new key), they cross a tree from root to leaf, comparing it to the key stored in it. nodes and decide, by comparison, to continue searching in the left or right subtree. On average, this means that each comparison allows operations to jump to about half of the tree, so that each search, insertion or deletion takes time relative to the logarithm of the number of items stored in the tree. This is much better than the linear time required to find an item with the key in the array (unsorted), but slower than the same operation on the hash table.

</div><span class="text_page_counter">Trang 10</span><div class="page_container" data-page="10">

How data is stored in a tree is important to know about the underlying data structure is that each node in the tree has a value associated with it. No nodes allowed to be empty. If the node has no value, it does not exist. Here's an example I mean by that.

How to find values in BST, as the name might suggest, a common use case for a binary search tree is to find if there are values. Let's look at an example:

How to insert a value into BST, it is also useful if you can add something to the tree. Doing so is actually quite easy, and looks like a quest. You start at the root. If the value you want to enter is missing, go left. If it's more, go right. One caveat is that you cannot enter an existing value. BTS cannot have duplicate values, because the value is not greater or less than itself! To avoid this, at every step along the way, just make sure that the value you are trying to enter does not match the node you are currently viewing. If that happens, just blame it or do something.

</div><span class="text_page_counter">Trang 11</span><div class="page_container" data-page="11">

Demonstration of the insertion operation

In the binary search tree, the insertion operation is performed with time complexity O (log n). In the binary search tree, new nodes are always included as leaf nodes. The insertion operation is performed as follows:

Step 1 - Create a newNode with the given value and set its left and right to NULL. Step 2 - Check whether the tree is empty.

Step 3 - If the tree is Empty, then set the root to newNode.

Step 4 - If the tree is Not Empty, then check whether the newNode value is smaller or larger than the node (here is the root node).

Step 5 - If the newNode is smaller than or equal to the node then switch to its left. If the newNode is larger than the node then move to the correct child.

Step 6- Repeat the steps above until we get to the leaf node (that is, to NULL).

Step 7 - After reaching the leaf node, enter newNode as the remaining child if the newNode is smaller or equal to the leaf node or enter it as the correct child.

Construct a Binary Search Trees by entering the following sequence of numbers 65, 10, 40, 5, 82, 75 and 90. The element is included in the Binary Search Tree as follows:

1. Insert 65

2. Insert 10

<small>65 </small>

</div><span class="text_page_counter">Trang 13</span><div class="page_container" data-page="13">

<small>82 </small>

<small>65 </small>

<small>10 </small>

<small>40 5 </small>

<small>82 </small>

</div><span class="text_page_counter">Trang 14</span><div class="page_container" data-page="14">

II. Determine the operations of a memory stack and how it is used to implement function calls in a computer

1. The operations of a memory stack

Stack memory is a memory usage mechanism that allows the system memory to be used as temporary data storage that behaves as a first-in, last-out buffer. An example of a stack is illustrated on the below:

<small>Figure 1 Example of stacks / queues </small>

Items in a stack are inserted or removed in a linear order and not in any random sequence. As in any queue or collection that is assembled, the data items in a stack are stored and accessed in a specific way. In this case, a technique called LIFO (Last In First Out) is used. This involves a series of insertion and removal operations which we will discuss in the following section.

2. LIFO (Last In First Out)

When a stack is accessed (there are inserted or deleted items), it is done in an orderly manner by LIFO. LIFO stands for Last In First Out. Computers use this method to request service data that the computer's memory receives. LIFO dictates that data is last inserted or stored in any particular stack, must be the first data to be deleted. If that applies to our queue for movies in Figure 1 above, there will be chaos! The operations on the stack are discussed in the following sections with image on the below:

</div><span class="text_page_counter">Trang 15</span><div class="page_container" data-page="15">

<small>Figure 2 : Last in First out </small>

3. Stack Operations 3.1 The Push Operation

Push operation involves inserting data items into a stack. Let us check our restaurant plate dispenser in Figure 9. Operation pushes additional plates (data items) into the plate dispenser (stack). The first plate is pushed down to the bottom of the stack with all subsequent plates in the following order after it. The first inserted data item is the most inaccessible and is located at the bottom of the stack.

3.2 The Pop Operation

Pop operation involves removing data items from the loaded stack. In our plate distributor illustration, the last sheet (data item) added is placed at the top of the stack. This data item is turned off the stack as the first item is deleted. Think of the spring loading system in the base of the plate distributor. It pushes the stack on top each time you remove the plate. In memory, items continue to be turned on in that order.

3.3 The Peek Operation

In this operation, no data items are added or deleted from the stack. The snapshot operation only requires the address location of the data item at the top of the stack (the last item is pushed).

</div><span class="text_page_counter">Trang 16</span><div class="page_container" data-page="16">

3.4 The Search Operation

In this operation, it does not have any data items are added or deleted from the stack. The search operation requires that geographical local of any data items in the stack. It located the item at the top of the stack.

4. How it is used to implement function calls in a computer stack memory in computers The stack memory is a special kind of memory that is used to store information temporarily, when we want to read the information correctly in the opposite direction to the recorded memory, without caring about the organization data addressing. Stack memory is sequential memory with specify access restrictions that allow writing and reading information to or from only one location in this memory called the top of the stack. The stack memory acts as a queue where data can be written to the top of the stack, while changing all the information stored in subsequent locations in a location according to the depth of the queue. This Can only read from the stack from the top of the stack. Reading makes information from the top of the stack removed and a new information is recorded in its place from the depth of the stack with the displacement of all information stored in one position to the top of stack. Then the queue is the last type to enter first. The stack memory works in the same way as the rifle magazine, in which the introduction of a new bullet box pushes all the remaining cartridges into the depths of the magazine and the use of cartridges (one shot) makes The top cartridge is removed and replaced with the first cartridge under the used one. Three instructions are used to manipulate the stack are push (write data into the stack), read stack (read the top of the stack), pop (delete data from the top of the stack with a shift of its contents up). Sometimes, symmetric operations involve the top two positions of the stack used as arguments and positions for the results. After pure operation, pop operation is performed and results are available at the top of the stack.

<small>Figure 3 Stack functioning principles </small>

The first stack deployment is based on the use of registers, in which data is moved in a parallel manner during Push and Pop operations. Modern stack deployment is based on the main memory usage supported by special registers that make it easy to access and manage the stack. A stack is reserved by the operating system, sequences of consecutive memory locations. The boundary of the stack is determined by two special registers - the stack base register and the

</div><span class="text_page_counter">Trang 17</span><div class="page_container" data-page="17">

stack limit register hold the address of the stack top and lower boundary positions. The address space between these addresses is excluded from other uses than the stack implementation. The top of the stack is determined by keeping the address in the third register called the stack pointer register. Before the computer starts executing the program, the top of the stack register is set to the contents of the stack base register. After each Push operation, the contents of the stack pointer register are changed according to the number of bytes corresponding to the stack in the stack limit direction. After each Pop operation, the content of the stack pointer is changed in the direction of the stack base.

<small>Figure 4 Stack implementation the computer- the top of the stack in the memory.</small>

The figure above shows the organization of the stack in the main memory, in which the top of the stack is accessed only in memory. Another solution, shown in the figure below, assumes that the top of the stack and the position just below the top, is stored in special processor registers: the top of the stack register and the top register -first.

</div><span class="text_page_counter">Trang 18</span><div class="page_container" data-page="18">

5. Stack memory implements function call in computer

Among the instructions that control computers, we can find subroutine call instructions with the acronym "Call" and instructions for returning subroutines with the acronym "Ret". A subroutine is a series of instructions that end with the "Ret" return command. In the subroutine the subroutine always has the address of the first instruction in a subprogram called the subroutine address. Mechanism of subroutine calls and the execution of the "Call" i "Ret" commands based on the use of the stack. Execute the "Call" subroutine command including:

1. stored in the stack of the program counter's current content (ie, the return address executes the next instruction after the call) with the "Push" operation,

2. write to the program that accesses the embedded address in the "Call" guide, 3. take the next tutorial according to the new content of the program counter.

The address written to the stack will be used by the "Ret" return command to automatically return from the subroutine to the next command.

Instructions "Call".

1. The return execution from the "Ret" subcommand includes:

2. Read from the top of the stack of the return address (to successfully instruct the "call" command),

3. write this address to the program counter, 4. perform the "Pop" operation on the stack,

5. Fetch a new guide according to the new content of the program counter.

The address written to the stack in a subroutine call is sometimes called a trace and the subroutine call is called a hop with a trace. Often use nested subroutine calls, in which during subroutine execution, new subroutine is called from its body. The mechanism of returning from trace subroutines stored in the stack allows to automatically return the next program call context with the correct return order preserved. The figure below shows actions related to nested subroutine calls from a top program thread. In the subroutine call 1 (Call 500 is stored at address 100), the return address 101 is stored in the stack. In the call of subprogram 2 (Call 900 is written at address 600), the return address 601 is saved in the stack. When returning from sub-program 2, address 601 is taken from the stack. When returning from subroutine 1, address 101 is taken from the stack.

</div><span class="text_page_counter">Trang 19</span><div class="page_container" data-page="19">

<small>Figure 6 The use of stack in subroutine calls </small>

III. Illustrate, with an example, a concrete data structure for a First In First out (FIFO) queue. 1. First in First out (FIFO) Queue:

A Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). A good example of a queue is any queue of consumers for a resource where the consumer that came first is served first. The difference between stacks and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added.

</div>

×