lessons 10, 11, 12 & 13 mcmanus cop1006 1. basic concepts sorting techniques stacks queues ...

Download Lessons 10, 11, 12 & 13 McManus COP1006 1.  Basic Concepts  Sorting Techniques  Stacks  Queues  Records  Linked Lists  Binary Trees McManusCOP10062

If you can't read please download the document

Upload: liliana-coleen-hodges

Post on 17-Jan-2018

221 views

Category:

Documents


1 download

DESCRIPTION

Whether using arrays or lists…there has to be some way to: Search the data structure Sort the data structure To do this, we use keys… McManusCOP10063

TRANSCRIPT

Lessons 10, 11, 12 & 13 McManus COP1006 1 Basic Concepts Sorting Techniques Stacks Queues Records Linked Lists Binary Trees McManusCOP10062 Whether using arrays or liststhere has to be some way to: Search the data structure Sort the data structure To do this, we use keys McManusCOP10063 Used to make a record unique from all others Ex. There is only one each: Social Security Number Drivers License Number Birth Certificate ID Can be used in searching for a record and for sorting records within a data structure such as arrays or files. McManusCOP10064 McManus COP1006 5 Where the elements are placed in some particular order. Sort order can be in by Ascending or Descending McManusCOP10066 Bubble (slow!) Selection Sort or Exchange Sort Insertion Sort Merge Sort Quick Sort Radix Sort Shell Sort (variation of Insertion Sort) McManusCOP10067 Compare A comparison is made between two pieces of data upon which a decision is made to move the data or not. Exchange An exchange is each time a piece of data is switched with another piece of data. The Swap McManusCOP10068 Exchanges Two Values Private Sub Swap(Array(J), Array(J+1)) If Array(J) > Array(J+1) Then Temp = Array(J) Array(J) = Array(J+1) Array(J+1) = Temp End If End Sub Use the Call statement to access the Swap Routine. McManusCOP10069 One of the simplest to understand The Concept: Lower numbers float to the top of the array and larger numbers sink to the bottom of the array. McManusCOP Successively exchanges adjacent pairs of elements in a series of passes, repeating the process until the entire sequence is in sorted order. Each pass starts at one end of the array and works toward the other end, with each pair of elements that are out of order being exchanged. The entire sequence considers n-1 pieces of data, but with each succeeding pass one less piece of data than the previous pass needs to be considered. McManusCOP100611 McManusCOP Worst Case Exchanges = 1/2 n(n - 1) = 1/2 5(5 - 1) = 10 Compares = 1/2 n(n - 1)= 1/2 5(5 - 1) = 10 Best CaseExchanges = 0Compares = n - 1 Advantage: If no exchanges are made during the first pass, the sequence is already in sorted order. Disadvantage: Is one of the slowest sorting algorithms and is probably only used because its logic is easily understood. McManusCOP100613 Get Array (or List) Input For Index = 1 to ListLength Input Value into Array(Index) Next Index Then Sort For I = 1 to ListLength For J = 1 to ListLength 1 If Array(J)>Array(J+1) Then Call Swap (Array(J), Array(J+1)) Next J Next I McManusCOP100614 A rearrangement of data such that the data are in increasing (or decreasing) sequence. The Algorithm for a Selection Sort For Index 1 to ListLength-1 do Find the position of the smallest element in list[1..ListLength]. If List(Index) is not the position of the smallest element then Exchange the smallest element with the one at position List(Index). McManusCOP100615 Selects the smallest (or largest) element from a sequence of elements. The values are moved into position by successively exchanging values in a list until the sequence is in sorted order. The only desirable property of the selection sort is that records of successively smaller keys are identified one by one, so that output of the sorted sequence can proceed virtually in parallel with the sort itself. McManusCOP100616 McManusCOP Exchanges = n - 1= = 4 Compares = 1/2 n(n - 1)= 1/2 5(5 - 1) = 10 Note: 390 is not included as it is the last item in list. Advantages Easiest to remember The only desirable property is that records of successively smaller keys are identified one by one, so that output (or processing) of the sorted sequence can proceed virtually in parallel with the sort itself. Disadvantages Still slow McManusCOP100618 Inserts each element into a sequence of sorted elements so that the resulting sequence is still sorted. With arrays, a new array is used to insert the values from the old array On average, half of the array will have to be compared. With lists, a new list is created from the values of the old list. McManusCOP100619 McManusCOP Exchanges = n - 1= = 4 Compares = 1/2 n(n - 1)= 1/2 5(5 - 1) = 10 Old List New List McManus COP Searches the list for a specific item. If the list is not ordered, the entire list must be searched before a conclusion may be made that the item is not in the list. If the list is ordered, the list is searched only until a value is found that is larger than the search item. McManusCOP100622 function ItemSearch (List : ListType; Item : ComponentType ): Boolean; var Index : Integer; begin Index := 1; List.Items[List.Length+1] := Item; while List.Items[Index] Item do Index := Index + 1; ItemSearch := Index List.Length + 1 end; McManusCOP Pascal Code function SeqSearch (List : ListType; Item : ComponentType ): Boolean; varIndex : Integer;Stop : Boolean; begin Index := 1; Stop := False;{Initialize} List.Items[List.Length+1] := Item; while not Stop do {Item is not in List.Items[1]..List.Items[Index-1]} If Item > List.Items[Index] then Index := Index + 1 else Stop := True;{Item is either found or not there} SeqSearch := (Index List.Length + 1) and (Item = List.Items[Index]) end; McManusCOP Pascal Code Processes the list by dividing the list and then searching each half. List must be sorted. Much more efficient that a sequential search. McManusCOP100625 Divides the List into 3 components List[1..Middle-1] List[Middle] List[Middle+1..Last] McManusCOP [First] [Middle] [Last] Item FM-1MM+1L Compute the subscript of the middle element. If the target is the middle value then Middle value is target location. Return with success. else if the target is less than the middle value then Search sublist with subscripts First..Middle-1. else Search sublist with subscripts Middle + 1..Last. McManusCOP100627 procedure BinarySearch (var List {Input} : IntArray; Target {Input} : Integer; First, Last {Input} : Integer; var Index {output} : Integer; var Found {output} : Boolean); varMiddle : Integer; begin Middle := (First + Last) div 2; if First > Last then Found := False else if Target = List[Middle] then beginFound := True; Index := Middleend else if Target < List[Middle] then BinarySearch (List, Target, First, Middle-1, Index, Found) else BinarySearch(List, Target, Middle+1, Last, Index, Found) end; McManusCOP Pascal Code McManus COP Each data structure element contains not only the elements data value but also the addresses of one or more other data elements. Examples: Stacks Queues Trees McManusCOP100630 Probably the simplest linked structure. Contain records Each element contains the address of the next list element. Are extremely flexible. They make it easy to add new information by creating a new node and inserting it between two existing nodes. It is also easy to delete a node. McManusCOP100631 A data type whose values are the locations of values of other data types and are stored in memory. Considered a Referenced Variable A variable created and accessed not by a name but by a pointer variable -- a dynamic variable. McManusCOP Pointer Linked List Node Node nil Linked Lists A connected group of dynamically allocated records. Nodes Records within a linked list. McManusCOP P Instance of Node key data McManusCOP headcurrent nil first node current node List Head The first node in a list. Inserting at the Head of a List Is more efficient and easier Insertion at the End of a List Less efficient because there is no specific pointer to the end of the list. The list must be followed from the head to the last list node and then perform the insertion. McManusCOP100635 Deleting a Node Change the Link field of the node that points to its predecessor and point to the nodes successor. Traversing a List Processing the nodes in a list starting with the list head and ending with the last node following the trail of pointers. Head nil is typical for processing loops that process lists. McManusCOP100636 McManus COP Is a data structure in which only the top element can be accessed. Classic Example: Plates in a buffet line. Customer always takes the top-most plate. Plates are replaced from the top. LIFO Last-In First-Out Structure Last element stored is the first to be removed. McManusCOP100638 Pushing Onto The Stack Placing a new top element on the stack. Popping The Stack Removing the top element of a stack. McManusCOP head pointer a stack head pointer 1 a pushed stack a popped stack head pointer Pointer Variable Top points to the list head of the stack. Only the stack element pointed to by Top can be accessed. B can be accessed only if element A is removed from the stack. A new element can only be added to the stack at the Top. McManusCOP ACB top Stack as a Linked List A data structure in which elements are inserted at one end and removed from the other end. Classic Example: Customers in a Theater Ticket Line or a list of jobs waiting to be executed. FIFO First-In First-Out Structure First element stored is the first to be removed. Also Array Queues, Priority Queues, and Schedule Queues. McManusCOP100641 McManusCOP tail pointer head pointer an empty queue tail pointer head pointer cat dog after enqueuing another element tail pointer head pointer cat after dequeuing an element tail pointer head pointer dog after enqueuing an element Similar to a linked list, except that each element carries with it the addresses of 2 or more other elements, rather than just one. McManusCOP100643 Contains at most two subtrees (or two children). Each subtree is identified as being either the left subtree or the right subtree of its parent. It may be empty (a pointer with no successors). Each node in a binary tree can have 0, 1, or 2 successor nodes. McManusCOP100644 Leaf Node - the nodes at the bottom of a binary tree node with zero successors. Root - a binary tree with at least one node at the top. Left and Right subtrees - the two disjoint binary trees attached to the root of a binary tree. Disjoint subtrees - nodes cannot be on both a left and right subtree of the same root node. McManusCOP100645 Parent-child relationship - the relationship between a node and its successors. Parent - the predecessor of a node. Child - the successor of a node. Siblings - two children of the same parent node. McManusCOP100646 Ancestors - all predecessors of a node, unless it is the root. The root has no ancestors. Descendants - all successors of a node. Sohow are these terms used? McManusCOP100647 McManusCOP A Balanced, Inorder, Minimal Path Binary Search Tree root descendant to 50 siblings (or twins) to 25 parent to 10 & 45 leaves (or terminal nodes) ascendant to 60 & 95 descendant to 50 left child right child edge children ancestors to 60 & 95 A subtree to 50 with nodes 75, 60, 95 Now apply the concept to a Binary Tree McManusCOP [First] [Middle] [Last] Root M-1 M+1 A tree structure that stores data in such a way that the data can be retrieved very efficiently. Each item in a binary search tree has a unique key. Is either empty or has the property that the item in its root has a larger key than each item in its left subtree and a smaller key than each item in its right subtree. Each subtree must be binary search trees. McManusCOP100650 A finite collection of objects means to process each object in the collection exactly once. Find the first node Find the next node. Determine when there are no more nodes. Process the current node. McManusCOP100651 McManusCOP This is called Inorder Traversal There are two others Each node is processed after all nodes in its left subtree but before any node in its right subtree. (left, center, right) Private Sub InOrder (P : NodePointer) If P nil Then InOrder (P^.Left) Process (P) InOrder (P^.Right) End If End Sub McManusCOP inorder Each node is processed before any node in either of its subtrees. (center, left, right) Private Sub PreOrder (P : NodePointer) If P nil Then Process (P) PreOrder (P^.Left) PreOrder (P^.Right) End If End Sub McManusCOP preorder Each node is processed after all nodes in both of its subtrees. (left, right, center) Private Sub PostOrder (P : NodePointer) If P nil Then PostOrder (P^.Left) PostOrder (P^.Right) Process (P) End If End Sub McManusCOP postorder Given a list 10, 15, 20, 25, 30, 35, 40 Two trees can be created from this: A linear list A balanced inorder binary tree Why the difference? Depends on the instructions McManusCOP100656 McManusCOP Two Versions of the Trees Linear List Balanced, Inorder, Minimal Path Binary Search Tree Created in order from first to last item in list Created from dividing list into two and then dividing each side into two keeping keys in order Patricia Trees Example B-Trees Example 23 Trees 234 Trees Tries Search T McManusCOP100658 McManusCOP Patricia Tree Example a aa ab aaa aab aba abb McManusCOP B-Trees Example - 10 [10, 11, 12, 13, 19] [1, 2, 3, 4, 9] McManusCOP Next?