Data Structures and Data Manipulation

Transcription

1 Data Structures and Data Manipulation What the Specification Says: Explain how static data structures may be used to implement dynamic data structures; Describe algorithms for the insertion, retrieval and deletion of data items stored in stack, queue and tree structures; Explain the difference between binary searching and serial searching, highlighting the disadvantages and disadvantages of each; Explain how to merge data files; Explain the differences between the insertion and quick sort methods, highlighting the characteristics, advantages and disadvantages of each.

2 Static and Dynamic Data Structures Static data structures are those which do not change in size while the program is running. Most arrays are static, once you declare them, they cannot change in size. Dynamic data structures can increase and decrease in size while the program is running. Advantages of Static Data Structures Compiler can allocate space during compilation Easy to program Easy to check for overflow An array allows random access Disadvantages of Static Data Structures Programmer has to estimate maximum amount of space needed Can waste a lot of space Advantages of Dynamic Data Structures Only uses the space that is needed at any time Makes efficient use of the memory Storage no longer required can be returned to the system for other uses Disadvantages of Dynamic Data Structures Difficult to program Can be slow to implement searches A linked list only allows serial access Static Structures holding Dynamic Structures A static data structure (like an array) can hold a dynamic structure. The static structure must be big enough. Stacks A stack is a last in first out (LIFO or FILO) data structure. The head pointer will point to the most recent item of data which will be at the top. There are only two operations that can be applied, inserting and deleting/reading. Inserting Data into a Stack First check that the stack is not full. If it is stop, and return an error. Next, increment the stack pointer, so it will now be pointing to the next empty data location. Finally insert the data into the location pointed to by the stack pointer. Deleting and Reading from a Stack Check to see if the stack is empty. If it is stop and return an error. Copy the data item in the cell pointed to be the stack pointer. Decrement the stack pointer and stop.

3 Queues A queue is a last in last out (LILO or FIFO) data structures. It has a head pointer, like that of a stack which points to the next empty data location, and a tail pointer which points to the last data item. Again, there are only two operations that can be done to a queue. Inserting Data into a Queue Check that the queue is not full, if it is report an error and stop. Insert the new data item into the cell pointed to by the head pointer. Increment the head pointer and stop. Deleting and Reading from a Queue Check that the queue is not empty, if it is report an error and stop. Copy the data item in the cell pointed to by the tail pointer. Increment the tail pointer and stop. Binary Tree s A binary tree is a data structure, where each item of data points to another two items, and a rule is needed to determine the route taken from any data item. The data items are held in nodes. The possible routes are called paths. Each node has two possible paths. The nodes are arranged in layers. The first node is called the root, or root node. Inserting Data into a Binary Tree Look at each node starting from the root If the new value is less than the value of the of the node, move left, other wise move right Repeat this for each node arrived until there is no node Then create a new node and insert the data. This can be written as: 1. If tree is empty enter data item at root and stop. 2. Current node = root. 3. Repeat steps 4 and 5 until current node is null. 4. If new data item is less than value at current node go left else go right. 5. Current node = node reached (null if no node). 6. Create new node and enter data. Deleting Data from a Tree Deleting data from a tree is quite complicated, because if it has sub-nodes, these will also be deleted. There are two options. The structure could be left the same, but the value of that node set to deleted. The tree could be traversed, the value removed, then put back into a binary tree.

4 Serial Search Expects data to be in consecutive locations (such as, an array). Doesn t expect the data to be in any particular order. To find the position of a value, look at each value in turn, and compare it with the value that you are looking for. When the value is found, it s position must be noted. If it gets to the end and has not found the value, it is not in the array. Can be slow, especially for a large amount of data. The algorithm for this is: 1. If n < 1 then report error, array is empty. 2. For i = 1 to n do a. If DataArray[i] = X then return i and stop. 3. Report error, X is not in the array and stop. Binary Search Where the list is arranged in a particular order. The list is split in two, and compared to be either higher or lower than the value being searched for. The list is continually split further halving each time until the value is found. The algorithm for this is: 1. While the list is not empty do a. Find the mid-point cell in the current list. b. If the value in this cell is the required value, return the cell position and stop. c. If the value to be found is less than the value in the mid-point cell then Make the search list the first half of the current search list Else make the search list the second half of the current search list. 2. Report error, item not in the list. Sorting Sorting is placing values in order, such as numeric or alphabetic. There are two main types we need to know about. Insertion sort and quick sort. Insertion Sort Where the data of the files is copied into a new file, but copied into the correct location. The result is that the new file is in the correct order, although it s very time consuming. Quick Sort First the data is placed in a row with an arrow under the first and last values, pointing at each other, one is fixed where as one is movable. If the two values are in the correct order then move the movable arrow towards the fixed arrow, else swap the items and the arrows. Continue to repeat this until the arrows collide. Continue to repeat this process until the files are of a length of one.

5 Key Words Data Structure Method of storing a group of related data. List A simple one dimensional array Pointers The numbers after the data, they point to the next data item. Linked List A dynamic data structure similar to an array. Queue A fist in first out data structure, containing a head and tail pointer. Tree A data structure where each item of data points to two others. Nodes These hold the data items. Paths These are the routes between the nodes in a tree. Layers Binary trees are arranged into layers, by the different levels. Root The first node in a tree. Insertion Sort A method of sorting data where all the items are copied to a new file, and put into the correct order. Quick Sort A faster method of sorting data, involving two pointers which move towards each other, and swap values if data pointed at are in the wrong order. Static and Dynamic Data Structures Summary:

6 Past Exam Questions and Answers Showing the steps of serial search e.g. Find York Aberdeen, Belfast, Cardiff, Oxford, York 1. start at Aberdeen 2. look at each word in turn/then Belfast, Cardiff etc 3. until York is found Showing the steps of binary search e.g. Find York Aberdeen,Belfast, Cardiff, Oxford, York look at middle/ Cardiff / Glasgow York is in second half of list repeated halving until York is found What are the advantages of binary search over seial search? (usually) faster because alf of data is discarded at each step/fewer items are checked How do you add a data item into a stack? if stack is full report error and stop increment pointer add data item at position pointer

7 How can quicksort be used to put a set of numbers in ascending order? e.g ** highlight first number in the list (the search number ) pointer at each end of list repeat: compare numbers being pointed to f in wrong order, swap move pointer of non-search number until pointers coincide so search number in correct position split list into 2 sublists quick sort each sublist repeat until all sublists have a single number put sublists back together Name another method or sorting insertion sort or bubble sort What is a static data structure? size is fixed when structure is created/size cannot change during processing What would be an advantage of static data structures over dynamic structures? amount of storage is known/easier to program

8 How would you merge two files? e.g. File A: Anna, Cleo,Helen, Pretti Fie B: Billy, Ian, Omar, Rob, Tom (Anna, Billy, Cleo, Helen, Ian, Omar, Pritti, Rob, Tom) You must: get correct order, use all names used once State the algorithm for merging two sorted files open existing files create new file check existing files are not empty use pointers/counters to identify records for comparison repeat compare records indicated by pointers copy earlier value record to new file move correct pointer until end of one file copy remaining records from other file close files assume common key assume if 2 records are the same only 1 is written to new file What is a dynamic data structure? size changes as data is added & removed/size is not fixed What would be a disadvantage to the programer of using dynamic data structures over static ones? more complex program to write State a data structure which must be static array/fixed length record

9 What steps need to be taken to add a new item to a binary tree start at root repeat compare new data with current data if new data < current data, follow left pointer else follow right pointer until pointer is null write new data create (null) pointers for new data How can insertion sort be used to arrange numbers in order? e.g list of sorted numbers is built up ith one number at a time being inserted into correct position plus 1 mark per correct row [max 4 rows] * ** What features of quick sort are not used in insertion sort? set of numbers broken into multiple sets uses pivots What steps are needed to be taken to pop a data item from a stack? if stack is empty eport error and stop output data(stack_pointer) decrement stack_pointer What is the meaning of a dynamic data structure size changes as data is added & removed/size is not fixed

10 What s the main disadvantage of a dynamic data structures over static one? more complex program to write What data structure must be static array/fixed length record How would you add a new item to an existing binary tree? start at root repeat compare new data with current data if new data < current data, follow left pointer else follow right pointer until pointer is null write new data create (null) pointers for new data

Following are the multiple choice questions (MCQs) or objective questions from Data Structures and Algorithms. The questions are set from the topics such as arrays, records, pointers, linked lists, stacks,

Data Structures Interview / VIVA Questions and Answers This Content is Provided by 1. What is data structure? The logical and mathematical model of a particular organization of data is called data structure.

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D. base address 2. The memory address of fifth element of an array can be calculated

CSE 326 Lecture 16: All sorts of sorts What s on our plate today? Sorting Algorithms: The Best of the Fastest Heapsort Mergesort Quicksort Covered in Chapter 7 of the textbook 1 Review of Sorting Algorithms

Unit I (Analysis of Algorithms) 1. What are algorithms and how they are useful? 2. Describe the factor on best algorithms depends on? 3. Differentiate: Correct & Incorrect Algorithms? 4. Write short note:

2015 Fall Computer Science I Section 2 Exam 2 Multiple Choice VERSION A The following code implements a stack using an array. Several lines of the implementation have been omitted. Questions 1-5 will be

Data Structures and Algorithms Recursive Sorting Chris Brooks Department of Computer Science University of San Francisco Department of Computer Science University of San Francisco p.1/45 12-0: Recursive

Sr. No. B. V.Patel Institute of Business Management, Allocated Hours Problem statements to be perform in laboratory List of Problems 1. 2 1. Write a program to insert 10 elements into array and Perform

Some CPSC 259 Sample Midterm and Final Exam Questions (Part 1) Sample Solutions DON T LOOK AT THESE SOLUTIONS UNTIL YOU VE MADE AN HONEST ATTEMPT AT ANSWERING THE QUESTIONS YOURSELF. 1. 4 marks You will

UCS406 QUIZ1 for CML, CA 10 minutes - 1 Which one of the following is the worst case time complexity of inserting an object into a binary search tree of n nodes? O(1) O(log n) O(n) O(n log n) - 2 Which

VIDYARTHIPLUS - Anna University Students Online Community CS6202-PROGRAMMING AND DATASTRUCTURES I IMPORTANT 2 MARKS UNIT I- 2 MARKS 1. Define global declaration? The variables that are used in more than

2.2 Data Structures and Algorithsms This course is concerned with the efficient allocation and manipulation of data. In general, the information you find here is applied to computers, but often it applies

Lecture III: Lists Data Structures data structure: a collection of data organized in some fashion which also supports operations for accessing and manipulating data. also known as a container or container

Second Semester [MCA] MAY-JUNE 2006 Subject: Data Structure Time: 3 Hours Maximum Marks: 60 Note: Question 1. is compulsory and is of 20 marks. Attempt one out of two questions from remaining four units.

ISAM Indexed Sequential Access Method ISAM is a static index structure effective when the file is not frequently updated. Not suitable for files that grow and shrink. When an ISAM file is created, index

Questions 1 through 25 are worth 2 points each. Choose one best answer for each. 1. For the singly linked list implementation of the queue, where are the enqueues and dequeues performed? c a. Enqueue in

COS 226 Algorithms and Data Structures Spring 200 Midterm This test has questions worth a total of 0 points. You have 0 minutes. The exam is closed book, except that you are allowed to use a one page cheatsheet.

Lecture IV: Lists II Skip Lists Skip Lists Skip Lists are a more efficient implementation of a Linked List. a Skip List is a linked list with more links which skip over various nodes. Skip Lists can be

, etc 3/25 (normally) faster than mergesort and heapsort heapsort is another O(n log n) runtime like shell sort, depends somewhat on part of the algorithm n log n on most data the idea: pick some element

M180 Data Structures and Algorithms in Java Mock Final Exam version 2 PART 1: ALL QUESTIONS ARE REQUIRED [10 Marks] Question 1: Choose the correct answer: (10 marks, one mark each) 1) Which of the following

File Organization and Indexing The data of a RDB is ultimately stored in disk files Disk space management: Should Operating System services be used? Should RDBMS manage the disk space by itself? nd option

Data Structures and Algorithms Circular buffer Circular buffer or ring buffer is a data structure that uses a single, fixed-size buffer like it is connected end-to-end. They are usually used for communication

Stacks and ueues CSE 373 Data Structures Unit Reading: Sections 3.3 and 3. Stack ADT A list for which Insert and Delete are allowed only at one end of the list (the top) the implementation defines which

UNIT 6A Organizing Data: Lists 1 Data Structure The organization of data is a very important issue for computation. A data structureis a way of storing data in a computer so that it can be used efficiently.

The General List Abstract Data Type The list is a general ADT where data is stored in a linear list or a sequence. The following functions are included in the ADT: Insert any at a given location Remove

Data Structures questions David Keil 9/08 1 Note: For some questions, answers are provided. These answers need to be checked to see that they match the questions, number for number. Some other checking

Data Structures 1 Common Data Structures Arrays (single and multiple dimensional) Linked Lists Stacks Queues Trees Graphs You should already be familiar with arrays, so they will not be discussed. Trees

9.1 9.2 This lecture is about trees, which are another common data structure. We ll be looking at binary trees, how they re represented and built. We ll also look at ordered binary trees, which are a good

Chapter 20: Binary Trees 20.1 Definition and Application of Binary Trees Definition and Application of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two other

Implementation Next, recall that our goal is to partition all remaining elements based on whether they are smaller than or greater than the pivot We will find two entries: One larger than the pivot (staring

Chapter 11: Priority Queues and Heaps In this chapter we examine yet another variation on the simple Bag data structure. A priority queue maintains values in order of importance. A metaphor for a priority

CS 2420 Exam 2 Fill in the blank (1 point each blank): 1. probing is simple to implement, but typically results in primary, which causes performance to degrade rapidly. 2. probing still causes secondary

Name Computer Science S-111 ination This exam consists of three parts. Part I has 15 multiple-choice questions, worth 3 points each. Answer all of them by marking the letter corresponding to the best answer

STUDENT OUTLINE Lesson 31 Linked-List Algorithms INTRODUCTION: The linked list in this lesson will have the following special characteristic: the random order of the incoming data will be stored in ordered

CS 2401 Final Exam Date: Friday, May 13, 2011. Name: Today is Friday the 13th, the day when vampires, zombies, and other creatures roam the Earth :-( 1-2. Recursion: According to the legend, when a vampire

EECS 281: Data Structures and Algorithms The Foundation: Data Structures and Abstract Data Types Computer science is the science of abstraction. Abstract Data Type Abstraction of a data structure on that

Algorithm Efficiency and Sorting How to Compare Different Problems and Solutions Two different problems Which is harder/more complex? Two different solutions to the same problem Which is better? Questions:

10 B-Trees Figure 10.1. Sequoia trees Sequoia sempervirens in Sequoia National Park, with average lifespans of 1800 years or more Searching data that is too large to store in main memory introduces a number

Algorithms and Data Structures Exam 100 Points Fill in the Blank (1 point each) 1. After many insertions and deletions in a hash table, it is possible that every unused node is marked as having been deleted.

Converting a Number from Decimal to Binary Convert nonnegative integer in decimal format (base 10) into equivalent binary number (base 2) Rightmost bit of x Remainder of x after division by two Recursive