Download presentation
Presentation is loading. Please wait.
Published byLiliana Coleen Hodges Modified over 9 years ago
1
Lessons 10, 11, 12 & 13 McManus COP1006 1
2
Basic Concepts Sorting Techniques Stacks Queues Records Linked Lists Binary Trees McManusCOP10062
3
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
4
Used to make a record unique from all others ◦ Ex. There is only one each: Social Security Number Driver’s 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
5
McManus COP1006 5
6
Where the elements are placed in some particular order. Sort order can be in by ◦ Ascending or ◦ Descending McManusCOP10066
7
Bubble (slow!) Selection Sort or Exchange Sort Insertion Sort Merge Sort Quick Sort Radix Sort Shell Sort (variation of Insertion Sort) McManusCOP10067
8
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
9
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
10
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. McManusCOP100610 2 3 5 4 6 1
11
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
12
McManusCOP100612 390 205 205 205 205 205 390 182 182 182 182 182 390 45 45 45 45 45 390 235 235 235 235 235 390 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
13
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
14
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
15
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
16
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
17
McManusCOP100617 390 45 45 45 45 45 205 205 182 182 182182 182 182 205 205 205205 45 390 390 390 235235 235 235 235 235 390390 Exchanges = n - 1= 5 - 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.
18
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
19
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
20
McManusCOP100620 390 390 390 390 45 205 205 205 45 182 182 182 45 182 205 45 45 182 205 235 235 235 235 235 390 Exchanges = n - 1= 5 - 1 = 4 Compares = 1/2 n(n - 1)= 1/2 5(5 - 1) = 10 Old List New List
21
McManus COP1006 21
22
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
23
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; McManusCOP100623 Pascal Code
24
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; McManusCOP100624 Pascal Code
25
Processes the list by dividing the list and then searching each half. ◦ List must be sorted. ◦ Much more efficient that a sequential search. McManusCOP100625
26
Divides the List into 3 components ◦ List[1..Middle-1] ◦ List[Middle] ◦ List[Middle+1..Last] McManusCOP100626 [First] [Middle] [Last] Item FM-1MM+1L
27
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
28
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; McManusCOP100628 Pascal Code
29
McManus COP1006 29
30
Each data structure element contains not only the element’s data value but also the addresses of one or more other data elements. Examples: ◦ Stacks ◦ Queues ◦ Trees McManusCOP100630
31
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
32
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. McManusCOP100632 Pointer Linked List Node Node nil
33
Linked Lists ◦ A connected group of dynamically allocated records. Nodes ◦ Records within a linked list. McManusCOP100633 P Instance of Node key data
34
McManusCOP100634 headcurrent nil first node current node
35
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
36
Deleting a Node Change the Link field of the node that points to its predecessor and point to the node’s 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
37
McManus COP1006 37
38
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
39
◦ Pushing Onto The Stack Placing a new top element on the stack. ◦ Popping The Stack Removing the top element of a stack. McManusCOP100639 4321 head pointer a stack 5 432 head pointer 1 a pushed stack a popped stack 4 321 head pointer
40
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. McManusCOP100640 ACB top Stack as a Linked List
41
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
42
McManusCOP100642 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
43
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
44
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
45
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
46
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
47
Ancestors - all predecessors of a node, unless it is the root. The root has no ancestors. Descendants - all successors of a node. So…how are these terms used? McManusCOP100647
48
McManusCOP100648 A Balanced, Inorder, Minimal Path Binary Search Tree 50 25 75 10456095 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
49
Now apply the concept to a Binary Tree… McManusCOP100649 [First] [Middle] [Last] Root M-1 M+1
50
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
51
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
52
McManusCOP100652 40 2565 54783513 13 25 35 40 54 65 78 This is called Inorder Traversal There are two others
53
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 McManusCOP100653 inorder 4 26 5731
54
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 McManusCOP100654 preorder 1 25 6743
55
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 McManusCOP100655 postorder 7 36 4521
56
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
57
McManusCOP100657 Two Versions of the Trees Linear List Balanced, Inorder, Minimal Path Binary Search Tree 10 15 20 30 25 35 40 10 15 20 25 30 35 40 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
58
Patricia Trees – Example B-Trees – Example 23 Trees 234 Trees Tries Search T McManusCOP100658
59
McManusCOP100659 Patricia Tree Example a aa ab aaa aab aba abb
60
McManusCOP100660 B-Trees Example - 10 [10, 11, 12, 13, … 19] [1, 2, 3, 4, … 9]
61
McManusCOP100661 Next?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.