2OCR Specification Points 3.3.5 Data structures and data manipulationa. explain how static data structures may be used to implement dynamic data structures;b. describe algorithms for the insertion, retrieval and deletion of data items stored in stack, queue and tree structures;c. explain the difference between binary searching and serial searching, highlighting the advantages and disadvantages of each;d. explain how to merge data files;e. explain the differences between the insertion and quick sort methods, highlighting the characteristics, advantages and disadvantages of each.OCR Specification Points3.3.5 Data structures and data manipulationTopicsImplementation of data structures, including stacks, queues and trees.Searching, merging and sorting.Candidates should be able to: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 advantages 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.
3Data StructuresA data structure is a way of storing data in a way that its position has meaning.e.g. Listing names in alphabetical order would give their position meaning.There are a number of structures that can be used.For example:Arrays and RecordsSerial, sequential, indexed sequential and direct access filesWhen these are used will depend on the circumstances – the most appropriate structure for the data should be used.Data Structures can be grouped into two main categories:DynamicStatic
4Implementation of Static Data Structures Arrays and Lists
5Static Data Structures Data structure whose size is fixed when it is created in memory.e.g. Array (or List)
6Static Example: Array/List What if the value of memory location 8 was already assigned to somewhere else?The whole of the array must be moved.This is very inefficient – especially when dealing with large amounts of data.This shows an example of an array stored in memory in alphabetical order.Here decisions about the array must be made before it is used:NameData typeSizeShape (number of dimensions)This example is a list as it is one dimensional.You can have arrays with many dimensions.Arrays are accessed by providing the array name and it’s index (from 0).e.g. People would output “Janet”2….People Array Index3Ben4Harry15Janet67Louise80xFFEEN/A92….3Janet4Louise5Ben6Harry7Terry892….3Janet4Louise5Ben6Harry7Terry80xFFEE9Length = 5Array:Name = PeopleData Types = StringSize = 5Shape = 1DAdding an extra item would require redefining the array.
7Static Data Structures Data structure whose size is fixed when it is created in memory.e.g. Array (or List)Advantages:Little to no risk of overflow or underflow errors – as it will always take up the same space in memory.This will most likely be reserved so no other program can access itThe program/memory management system can allocate a fixed amount of memory.Disadvantages:Requires knowledge of the size of the array before it has been created. This can result in:Waste of resources – once they have been reserved the space it can no longer be used by other processes/data.Running out of space when the prediction of space is too little.
8Implementation of Dynamic Data Structures Linked Lists, Queues, Stacks, Binary Trees and their implementation.
9Examples of Dynamic Data Structures There are four type of dynamic data structures we need to know:Linked ListQueueStackBinary TreesBINARY
10Linked ListA linked list is similar to an array in that it stores a list of values, however it is dynamic and can be extended or shortened to the size of the data inside it at will.To do this pointers are used:Each data item no holds two pieces of information:Data = the original data the item held.Pointer = the address of the next item in the list.The Start Pointer stores the address of the first item in the list.This is what actually allows the list to be accessed by the program.The last item in the lists’ pointer will be Null (blank).This indicates the end of the list.Called a Null Pointer.
11Dynamic Example: Linked Lists Start Pointer stores the address of the first item in the list.DataPointersInserting an item simply requires:Change pointer of previous item to its new location.Make its equal to the previous pointers old value.e.g. Inserting “Kerry” between “Janet”, “Louise”.Deleting an item simply requires:Taking its pointer value, and replacing the previous items’ pointer value with this.e.g. Deleting “Janet”.Now “Janet” will be skipped, and can be used for other data.2….3Ben4Harry75.....6“Misc. Data”Janet8Louise109TerryNull115Kerry8...5Null Pointer is stored in the last item to indicate the last item in the list.
12Linked List Implemented By 2D Array A linked list is similar to an array in that it stores a list of values, however it is dynamic and can be extended or shortened to the size of the data inside it at will.To do this pointers are used:Each data item now holds two pieces of information:Data = the original data the item held.Pointer = the address of the next item in the list.wefwef
13Static and Dynamic Data Structures Comparison Data structure whose size is fixed when it is created in memory.e.g. ArrayAdvantages:The program/memory management system can allocate a fixed amount of memory.No risk of overflow or underflow errorsAs it will always take up the same space in memory.Data structure which will extend and change its size to fit the data.e.g. Linked ListAdvantages:Can extend as far as physically possible – more flexible.Allows for the program to be more easily written – less must be determined at compilation time.Inserting, merging and deleting of items is very easy and requires little processing power.
14Static and Dynamic Data Structures Comparison Disadvantages:Requires knowledge of the size of the array before it has been created. This can result in:Waste of resources – once they have been reserved the space it can no longer be used by other processes/data.Running out of space when the prediction of space is too little.Any manipulation other than adding or taking from the end requires moving large amounts of the data.Inefficient use of memory and cpu-time.Disadvantages:Unnecessary + inefficient for small amounts of data.In this case the size of the data may be even smaller than the extra data needed to make it dynamic.Data can be highly fragmented over extended use. This may cause a physical bottleneck when the hardware needs to access this data.
15LILO!QueueA queue is a data structure, similar in implementation to a list/array.However it implements a “First In First Out” (FIFO or LILO) order.Hence le queue!So is therefore a “serial structure where the position is related to the chronological appearance of the data”.It can grow and shrink in size.For example if items are being processed faster than they are being added to the queue, the queues size will be smaller.Have two pointers:Head Pointer holds the address of the oldest item in the queue (next to be read).Tail Pointer holds the free address before the most recently added item in the queue (last to be read).There are two operations you can do to a queue:Enqueue = putting something on the end of the queue.Value added to address pointed to by tail pointer.The tail pointer is then incremented.Dequeue = reading and removing the item at the front of the queue.Value at head pointer read.Head pointer incremented.
16Dynamic Example: Queue LILO!Dynamic Example: QueueEnqueue = putting something on the end of the queue.Value added to address pointed to by tail pointer.The tail pointer is then incremented.Dequeue = reading and removing the item at the front of the queue.Value at head pointer read.Head pointer incremented.The previous head value is now ignored as if it were free space.Tail Pointer = 7=> 8Head Pointer = 0=> 11234567
17Dynamic Example: Stack …13121110987654321A variable which stores the address of the upmost value of the stack.A stack is a method of storing data following the first in last out (FILO) principle.A stack pointer is used to store the location of the most recently added item of the stack.Used to read the data.Note: Only the value pointed to by the stack pointer can be read at any time.Push = the action of adding something to the stack.Stack Pointer Incremented (7=>8)Value is then stored in the address represented by stack pointer.Stack Pointer= 7=> 8
18Dynamic Example: Stack …13121110987654321A variable which stores the address of the upmost value of the stack.Pop = the action of removing an item from the top of the stack.Stack Pointer is decremented (8=>7)Note: The data does not need to be deleted as there is no longer any reference to it.Stack Pointer= 7=> 8=> 7
19Implementing Stacks and Queues Static data structures can be used to implement dynamic data structures.This is because most computers do not implement dynamic data structures natively. And so these must be developed in code.Often higher level languages will have implementations built in.e.g. This is done by vb.net .A stack can be implemented by using an array.Methods can be added to the array class to allow popping and pushing to an array.And it will be stored identically in memory.However a linked list can be used as well. This provides a more flexible implementation.Meaning the stack does not need to take up a continuous section of memory.It can also remove the source of some stack overflows errors.As when the available continuous memory runs out can simply point to more free space. (as opposed to writing over other data).A similar method can be applied to create a queue.2….3Ben4Harry5Janet67Louise8Array stored in memory as a stack by restricting access to the top item only (highest index -> “Louise”).
20Binary TreesA binary tree is data structure which stores items of data.Each item of data points to another two. (binary!)The direction in which they are pointed gives their position meaning.Can be used to sort alphabetically (as the example has been). If the traversing algorithm is known.The first node is called the root node.Each pointer (arrow) is a possible path from the nodeAfter each new set of items are created they are called a new layer.The syllabus specifies one way of traversing trees:If there is a left branch that has yet to be traversed, then follow it and repeat.Read the node if it hasn’t already been read.I there is a right branch traverse it and go back to 1.Go back up one layer.However other algorithms can be used.Such as the method used to traverse binary trees for Reverse Polish.AlexBarryTerenceChloeBenBeckyBexExample of a binary tree.Follow this shape, but recursively.
21Implementing Binary Trees Binary Trees are implemented using a “linked list of arrays”.Each node is represented by an array containing:Data the node representsPointer to right hand child node ↴Pointer to left hand child node → (null if on an end of the tree)The pointers store the address in memory of the nodes’ children.In this way it acts as a linked list.Example node with associated data.DataLeft PointerRight PointerBen0xD023F0xF11A2
23Implementing Binary Trees Example binary tree represented as arrays in memory:Root Node = 0“Chloe”172103“Alex”4Null56...“Barry”8913“Terrence”1112“Ben”1427151816...1718“Bex”19Null2021222324252627“Becky”28293031DataLRChloe710Each node represented by an array of length 3.DataLRBarry313DataLRTerrenceNDataLRAlexNDataLRBen2718DataLRBeckyNDataLRBexN
24Data Manipulation Searching Methods of searching lists. (Recap from F452)
25Fig.1 Sample ordered list. Linear SearchesA serial search is where a list is searched in order from its’ first to its’ last item.This list is not necessarily ordered (but can be).Can be slow – especially as the dataset increases.As there is no order of items, it will have to check each item on the list before it can determine the item does not exist.Inefficient/waste of cpu time.A sequential search is a linear search performed on an ordered dataset.Main advantage over serial searching is that if the item does not exist, this can be determined more quickly.When it passes the point where the item should be, it will stop.e.g. When looking for “Ben” in fig.1, will stop when it gets to “Beth” – as “Beth” is after “Ben” in the alphabet.NamesAndrewBethChadDaveFredFig.1 Sample ordered list.
26Binary SearchA binary search is a method of searching data which has been pre-sorted.Works by splitting the list in two each time, and taking the section which contains the data item.Hence le binary (two)Very efficient – much faster than a serial search.Will take a maximum of log2[Number of items in the list] iterations to find a specific value.As opposed to [Number of items in the list] for a serial/linear search.The algorithm can be summarised as: (in a LIST of length N)Find midpoint value of list: LIST[N/2]If an odd number, round up e.g. 13/2 = 6.5 = 7 -> LISTIf target = midpoint, item found at index of midpoint.If target is greater than midpoint, delete all values above. If target is smaller than midpoint, delete all values below.Go back to 1.
27Searching Example: Binary Midpoint, 5/2 = 2.5 => 3, is “Chad”.“Chad” does not equal “Dave”.“Dave” is more than “Chad” (lower in the alphabet), so remove all value above and including “Chad”.Go back to 1.Midpoint, 12/2 = 6, is “Gareth”“Gareth” does not equal “Dave”.“Dave” is less than “Gareth” (higher in the alphabet), so remove all value below and including “Gareth”.Go back to 1.Midpoint, 2/2 = 1, is “Dave”.“Dave” does not equal “Dave” => “Dave” found at position 4.For example, finding “Dave” in this alphabetical list.In LIST of length NFind midpoint value of list: LIST[N/2]If an odd number, round up e.g. 13/2 = 6.5 = 7 -> LISTIf target = midpoint, item found at index of midpoint.If target is greater than midpoint, delete all values above. If target is smaller than midpoint, delete all values below.Go back to 1.N = 12N = 5N = 2N = 1NamesAndrewBethChadDaveFredGarethHarryMattSteveTerryVanessaZefferyNamesAndrewBethChadDaveFredNamesDaveFredNamesDave
28Data Manipulation Sorting Methods of sorting lists.
29Insertion SortA method of sorting in which each item is copied from the file into a new file, in the correct position.Simple, but has some disadvantages:Inefficient use of time – very slow.Requires a lot space in memory.Algorithm: Read each value, storing the address of the smallest.When all have been read:Copy smallest to the first place in the new fileRemove smallest from old file.Go back to 1, until the old file is empty.
30Quick Sort A Quick sort is an alternative method sorting. Algorithm: Complicated, and cumbersome method but...Becomes increasingly efficient as the number of items increases.Relatively easy to program.Algorithm:Display list in a row, with a fixed arrow on the first value, and a movable arrow on the last (however does not actually matter)If the two pointed to values are in the right order:Move the movable arrow towards the centre.Else:Swap the arrows.Repeat 2-3. until arrows are adjacent – the middle item is now in the correct place.Repeat with sub lists on either side the correctly ordered item.This is a good exemplar use of recursion.
31Sorts Question Perform a quick sort on: Perform an insertion sort on: Answer:NamesVanessaZefferyFredHarrySteveAndrewDaveNamesNamesAndrewDaveFredHarrySteveTerryZefferyAnswer:NamesBethChadGarethMattVanessaNamesNamesVanessaGarethChadBethMatt
32Data Manipulation Merging Methods of merging lists.
33Merge SortA merge sort is a method of merging two already sorted (sequential) files.Outline:Read first value from each fileCompareWrite smallest value to new fileRead next value from file usedBack to 2. until no more items are left.Write remainder of longest file to new file.
34Merge Sort For example: Answer: Names Andrew Beth Chad Dave Fred GarethHarryMattSteveTerryVanessaZefferyNamesAndrewDaveFredHarrySteveTerryZefferyNamesBethChadGarethMattVanessaNames