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

unit 19 data structures and algorithms

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 (4.74 MB, 70 trang )

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

ASSIGNMENT 1

Unit number and title Unit 19 Data Structures and Algorithms:

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

Summative Feedback:

Resubmission Feedback:

Internal V<b>erifier’s</b> Comments:

IV Signature:

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

3. Explain Use Cases ... 10

<b>II. Determine the opera ons of a memory stack and how it is used to implement func on calls in a computer (P2) ... 17 </b>

1. Understand the Basics. ... 17

2. Visualize the Stack ... 18

3. Implement and Trace ... 18

<b>III. Using an imperative de ni on, specify the abstract data type for a so ware stack (P3) ... </b>20

1. De ne the Opera ons ... 20

2. Use Pseudocode or Java ... 24

3. Consider Error Handling ... 29

Consola on ... 33

Reference ... 34

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

List Of Figures

Figure 1. Data Structures and Algorithms - Arrays ... 7

Figure 2. Data Structure and Algorithms - Queue ... 7

Figure 3. Data Structure and Algorithms - Tree ... 8

Figure 4. Example code for Array 2 ... 11

Figure 5. Example code for Array 1 ... 11

Figure 6. Result code for Array ... 12

Figure 7. Example code for Queue... 13

Figure 8. Result code for Queue ... 14

Figure 9. Example code for Tree ... 15

Figure 10. Example code for Tree ... 16

Figure 11. Result code for Tree ... 17

Figure 12. Implement and Trace Code ... 19

Figure 13. Implement and Trace Code ... 20

Figure 14. Implement and Trace Result Code ... 20

Figure 15. push(item) ... 21

Figure 16. pop() ... 22

Figure 17. peek() ... 23

Figure 18. isEmpty() ... 24

Figure 19. Class Stack ... 25

Figure 20. Push opera on Code... 26

Figure 21. Class Stack ... 26

Figure 22. Result Push opera on ... 27

Figure 23. Peek opera on Code ... 27.

Figure 24. Result Peek opera on ... 27

Figure 25. Result pop opera on ... 28

Figure 26. pop opera on Code ... 28

Figure 27. Result IsEmpty ... 29

Figure 28. IsEmpty Code ... 29

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

Figure 29. Element Code ... 30

Figure 30. Code before processing ... 30

Figure 31. Results before processing ... 30

Figure 32. Code a er processing ... 31

Figure 33. Results a er processing ... 31

Figure 34. Element added ... 32

Figure 35. Code before processing ... 32

Figure 36. Results before processing ... 32

Figure 37. Code a er processing ... 33

Figure 38. Results a er processing ... 33

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

<b>Introduc on </b>

So net Development Ltd, a leading so ware company specializing in network delivery solu ons, has won a contract for a joint development service delivery project. I was tasked as an in-house so ware developer with building a middleware solu on to improve communication between several service interfaces. The goal of the project is to use abstract data types (ADTs) to improve so ware design, development, and tes ng. To accomplish this task, you will provide detailed presenta ons to partners explaining how ADT improves e ciency, reusability, and maintainability. In addi on, an ar cle detailing the development of data types and abstract algorithms using formal nota on will be released. So net and its partners are posi oned to make signi cant advances in so ware development by focusing on innova on and collabora on.

<b>I. Create a design speci ca on for data structures explaining the valid opera ons that can be carried out on the structures (P1) </b>

<b>1. Data Structures. </b>

Data structure refers to the organiza on and storage of data in a computer's memory or storage system. It provides a systema c method for represen ng and manipula ng data to successfully perform opera ons and solve problems. Data structures establish rela onships between pieces of data and how they are stored and retrieved. Di erent data structures are created to manage di erent types of data and enable di erent ac ons. Arrays, linked lists, stacks, queues, trees, graphs, and hash tables are examples of typical data structures. Each data format has its own

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

bene ts, limita ons, and applica on scenarios. Advantages of data structures:

Performance: The data structure is op mized for simple mathema cal opera ons like searching, inser ng, removing, and accessing data.

Resource Op miza on: Using the proper data structures will help you save on resources like memory and processing me.

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

Flexibility: Di erent data formats allow for customiza on and op miza on based on applica on needs.

Easy to use: Opera ons and interac ons with data structures are frequently designed to be obvious and simple to grasp.

Disadvantages of data structures:

Complex: Some data structures are complex and require a deep understanding of their opera on and analysis.

Resource usage: While certain data structures perform be er in speci c circumstances than others, some may use more resources overall.

Characteris cs of data structures are o en classi ed according to their characteris cs. The following three characteris cs are examples:

Linear or Non-linear: This property signi es whether data elements are organized in a linear sequence, as seen in arrays, or if they lack a specific order, as exempli ed by histograms.

Homogeneous or Heterogeneous: This a ribute indicates whether all data items within a par cular collec on are of the same type, like an array with uniform elements, or if they encompass a variety of types, such as in the case of an abstract data type formulated as a structure in C or a class de ni on in Java.

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

Sta c or Dynamic: This trait relates to how the data structure is constructed. Sta c data structures maintain a xed size, structure, and memory loca on at compile me, while dynamic data structures possess the capability to adjust in size, structure, and memory loca on, depending on their intended usage.

Select the 3 most suitable structures for the project used to create design speci ca ons for data structures such as arrays, Queues, and trees.

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

Array: An array is a form of linear data structure that is described as a grouping of pieces of the same or dis nct data types. They exist in one dimension as well as numerous dimensions. These data structures occur when it is necessary to store several components with comparable a ributes in one loca on.

Queue: A queue is an abstract data structure. What di eren ates it from a stack is that the queue is open at both 1-input and 1-output ends. Pre-managed data will be accessed

rst. Data is added to the queue from one end and withdrawn from the queue from the other end.

<small>Figure 1 Data Structures and Algorithms - Arrays. </small>

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

Tree:Trees are non-linear abstract data structures with a hierarchical structure. These are nodes that are linked together. The tree data structure is composed of subtrees that are linked to a single node known as the root node.

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

<b>2. De ne Opera ons </b>

<small>o </small> Array:

<small> </small> Inser on: A new element is added to an array at a designated index point when an element is inserted. You must supply the index posi on and the value to insert in order to execute an insert. In order to make room for the new element during inser on, it is frequently necessary to

<small></small> move exis ng elements a er the inser on point. This indicates that every element will be shi ed one posi on to the right star ng at the inser on point. The new element is shi ed and then posi oned at the desired index. Usually, insertion is

<small>Figure 3 Data Structure and Algorithms - Tree . </small>

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

performed to change an array's contents or increase its size.

<small> </small> Dele on: An element must be removed from the array by shi ing it to a certain index point. You must supply the element's index in order to carry out the dele on. In order to replace the space le by the deleted element, dele on frequently necessitates moving already-exis ng items a er the dele on point. This implies that all items will move one place to the le following the dele on point. The array size is decreased by one a er shi ing. Using erase, you may take elements out of an array and change the array's size correspondingly.

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

<small></small> Traversal: This process entails itera ng through the array's elements and performing a speci ed opera on on each one. There are several methods of traversal depending on the situa on, such as linear search, binary search, and dynamic programming. In general, traversal is a method of accessing one or more elements in an array; it can be accomplished using an index/pointer or a name-based index.

<small>o </small> Queue:

<small> </small> Enqueue:An element is added to the rear of the queue via the enqueue procedure. You must supply the element to be inserted in order to execute an enqueue. By adding the element at the end of the queue, the enqueue opera on expands the queue's size by one. The newly inserted element moves to the front and rear of the queue if it is empty. The new element is posi oned to the rear of the queue if it already has elements, while the current elements stay in their original places.

<small> </small> Dequeue: The element is taken out of the front of the queue by the dequeue ac on. Just take out and put back the element at the top of the queue to complete a dequeue. By performing this ac on, the queue's size is reduced by one and the front is updated with the subsequent element in the queue. In the event that the dequeue leaves the queue empty, neither the front nor the rear element are le . Peek: The peek opera on allows you to view the element at the front of the queue

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

without removing it. You can obtain and return the element at the front of the queue during a peek without changing the contents of the queue. When you need to verify anything depending on the value of the front element or look at the next element to be dequeued, this operation comes in handy.

<small>o </small> Tree:

<small> </small> Inser on: The inser on opera on adds a new tree element. To insert an element, you must supply the value or key of the element to be inserted. Depending on the type of tree data structure u lized, such as Binary Search Tree (BST), AVL Tree, or Red-Black Tree, the inser on process follows di erent rules. The inser on process

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

guarantees that the tree is balanced and that any stated a ributes or restric ons are maintained.

<small></small> Dele on: The dele on procedure eliminates a tree element. To delete an element, you must supply the value or key of the element to be deleted. The dele on opera on, like the inser on opera on, follows certain rules dependent on the kind of tree being u lized. The dele on opera on deals with a variety of situa ons, including removing a leaf node, dele ng a node with one child, and dele ng a node with two children. To guarantee that the tree stays valid and meets any given criteria, the relevant procedures for restructuring and preserving balance are implemented.

<small> </small> Traversal: The traversal opera on enables you to visit and process each element in the tree in the order speci ed. Depth-First Search (DFS) and Breadth-First Search (BFS) are two traversal algorithms. There are three types of traversal orders in DFS: pre-order, in-order, and post-order. The current node is visited rst, followed by its o spring. In-order traversal visits the le subtree rst, then the current node, and lastly the right subtree, yielding a BST in sorted order. Before reaching the current node, post-order traversal visits the le and right subtrees.

<b>3. Explain Use Cases </b>

Array:

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

Use cases: Arrays are used to store and access a collec on of items, which are frequently of the same data type.

Example: Inventory Management: In retail, arrays are used to manage product inventories, tracking items, quan es, and prices.

Example Code:

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

<small>Figure 4 Example code for Array 2. </small>

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

Result code:

Queue:

Use Case: Queues are used to handle objects in a First- -First-Out (FIFO) manner, which is Inappropriate for scenarios involving planned or ordered processing.

Example: E-commerce websites use queues to process customer orders, ensuring orders

<small>Figure 6. Result code for Array</small>

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

are ful lled in the order they are received. Example code:

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

Result code :

<small>Figure 7. Example code for Queue</small>

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

Result Code:

<small>Figure . Example code for Tree10</small>

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

<b>II. Determine the opera ons of a memory stack and how it is used to implement func on calls in a computer ( ) P2</b>

<b>1. Understand the Basics. </b>

A stack is a linear data structure that follows the Last- -First-Out (LIFO) principle. It can be Inthought of as a stack of objects, where the last object added is the rst one to be removed. The basic opera ons associated with a stack are:

<small>Figure . Result code for Tree11</small>

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

push: The push ac on adds an element to the stack's top. It accepts the element as an argument and adds it to the stack, increasing the size of the stack. The new element rises to the top of the stack.

pop: The pop ac on removes and returns an element from the stack's top. It removes the topmost element and adjusts the size and top posi on of the stack correspondingly. As a consequence, the element that was popped o is restored.

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

peek: The peek ac on returns the top member of the stack without dele ng it. It allows you to inspect the top element's value without altering the stack's contents. As a consequence of the peek ac on, the element at the top is returned.

size(): The size() procedure returns the current number of items in the stack. It returns the current stack size by coun ng the number of elements saved.

isEmpty: The isEmpty opera on determines whether or not the stack is empty. It produces a boolean result, o en true or false, indica ng if the stack contains any components. It returns true if the stack is empty; otherwise, it returns false.

top: The top ac on, like the peek opera on obtains the element at the top of the stack without dele ng it. It gives you access to the value of the top element. As a consequence of the top ac on, the element at the top is returned.

<b>2. Visualize the Stack </b>

Empty Stack: Ini ally, the stack is empty.

Func on Call: Upon making a new func on call, let's say "New Disk," a corresponding stack frame is added to the stack, represen ng the execu on context of the "New Disk" func on.

Nested Func on Calls: Within the "New Disk" func on, there are nested func on calls, such as "Make a Func on Call (Get Disk)." Each of these nested func on calls creates addi onal stack frames on top of the ini al "New Disk" frame.

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

Return to Previous Func on: A er the nested func on calls are made and completed, the corresponding stack frames are removed as the func ons return. The control ow returns to the "New Disk" func on.

Maintaining the Stack: Throughout this process, the stack maintains the order of func on calls, with new frames being added as new func ons are called, and frames being removed as func ons return.

<b>3. Implement and Trace </b>

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

Prac ce implemen ng stacks in the Java programming language. Watch the code change with each opera on.

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

<small>Figure 12. Implement and Trace Code</small>

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

<small>Figure 13. Implement and Trace Code </small>

Result code:

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

<b>III. Using an impera ve de ni on, specify the abstract data type for a so ware stack (P3) 1. De ne the Opera ons </b>

In computer science, the Stack Abstract Data Type (ADT) is a basic data structure. It speci es a collec on of opera ons and behaviors for a data structure called a "stack." The Last- -First-Out In

<small>Figure . Implement and Trace Result Code14</small>

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

(LIFO) principle is followed by the Stack ADT, which implies that the last element added to the stack is the rst to be deleted. The major opera ons speci ed in the Stack ADT are as follows: push(item): This opera on adds an item to the top of the stack. It involves the following

steps:

o Increment the stack pointer to accommodate the new item.

o Place the new item at the top of the stack.

</div>

×