Upload
merry-jones
View
249
Download
0
Tags:
Embed Size (px)
Citation preview
Chapter 15: Advanced Topics: Introducing Data Structures
and Recursion
Visual Basic .NET Programming: From Problem Analysis to Program Design
Visual Basic .NET Programming: From Problem Analysis to Program Design 2
Objectives
• Create linked lists
• Create a stack
• Create a queue
• Understand recursion
• Write recursive methods
Visual Basic .NET Programming: From Problem Analysis to Program Design 3
Introducing Data Structures
• Linked list
– List of object instances that are linked together
– Objects may be added to:
• Beginning
• End
• Somewhere in between
Visual Basic .NET Programming: From Problem Analysis to Program Design 5
Introducing Data Structures (continued)
• Stack
– Can be implemented as specialized case of linked list
– Objects are added and removed only at beginning of list
– Employs LIFO logic
• Last in, first out
Visual Basic .NET Programming: From Problem Analysis to Program Design 7
Introducing Data Structures (continued)
• Queue
– Implemented as linked list
– Objects added at end and removed from beginning
– Employ FIFO logic
• First in, first out
Visual Basic .NET Programming: From Problem Analysis to Program Design 9
Creating Linked Lists
• Node
– Each instance in linked list contains
• Data
• Reference attribute linking it to next node
• Maintain references to first and last item in list
– To facilitate access
Visual Basic .NET Programming: From Problem Analysis to Program Design 10
Creating Linked Lists (continued)
• Self-referencing class
– Class that references instances of itself
Visual Basic .NET Programming: From Problem Analysis to Program Design 15
Creating a Stack
• Stack can be implemented as LinkedList
– LIFO structure:
• Add and remove nodes from only beginning of Stack
– Employ inheritance to create Stack class
• As subclass of LinkedList
Visual Basic .NET Programming: From Problem Analysis to Program Design 17
Exploring the FCL Stack Class
• Stores nodes as instances of Object
• Methods:
– Push
– Pop
– Peek
– ToArray
Visual Basic .NET Programming: From Problem Analysis to Program Design 18
Creating a Queue
• Queue can be implemented as linked list• Objects added only at end • Removed only from beginning
– Employing FIFO logic
• Enqueue– Add object
• Dequeue– Remove object
Visual Basic .NET Programming: From Problem Analysis to Program Design 20
Creating a Queue (continued)
• Placing common attributes and methods in superclass eliminates redundancy in subclasses
– Count and IsEmpty added to superclass
Visual Basic .NET Programming: From Problem Analysis to Program Design 21
Exploring the FCL Queue class
• Queue class
– Stores nodes as instances of Object
• Methods:
– Enqueue
– Dequeue
– Peek
– ToArray
Visual Basic .NET Programming: From Problem Analysis to Program Design 22
Programming Example: Reservation Application
• Implements reservation system
• Reservation class methods:
– MakeReservation
– CancelReservation
– ListReservations
Visual Basic .NET Programming: From Problem Analysis to Program Design 24
Understanding Recursion
• Programming technique in which method calls itself
• For some problems recursive approach leads to simple and elegant solution
• Recursive problem-solving approach– Divides complex problem into successively
smaller but similar subproblems
– Solves each subproblem
– Assembles result
Visual Basic .NET Programming: From Problem Analysis to Program Design 25
Example 15-8: Using a Recursive Method to Simulate
Opening Boxes
Module OpenBoxesRecursively
Sub Main()
'open largest box (size 10)
OpenBox(10)
System.Console.WriteLine(”Done”)
End Sub
Visual Basic .NET Programming: From Problem Analysis to Program Design 26
Example 15-8: Using a Recursive Method to Simulate
Opening Boxes (continued)Sub OpenBox(ByVal boxSize As Integer)
If boxSize = 0 Then
System.Console.WriteLine(”No more boxes”)
Else
System.Console.WriteLine(”Box “ & boxSize & _
“ opened”)
OpenBox(boxSize - 1)
End If
Visual Basic .NET Programming: From Problem Analysis to Program Design 29
Example 15-8: Using a Recursive Method to Simulate
Opening Boxes (continued)
• Recursive algorithm must have stopping point
– Called base case
– When encountered, recursion stops
Visual Basic .NET Programming: From Problem Analysis to Program Design 30
Example 15-8: Using a Recursive Method to Simulate
Opening Boxes (continued)
• Each time recursive method calls itself
– Fresh copy of the method created
– Each copy has own set of values and parameters
– Remains unfinished until base case is recognized
Visual Basic .NET Programming: From Problem Analysis to Program Design 31
Example 15-8: Using a Recursive Method to Simulate
Opening Boxes (continued)
• Upon each return
– Execution resumes within that copy at line of code immediately following recursive call
• Recursion is similar to iteration
– Any problem that can be solved iteratively can also be solved recursively, and vice versa
– Recursive solutions are less efficient
Visual Basic .NET Programming: From Problem Analysis to Program Design 32
Writing Recursive Methods
• Write recursive method:
– Must think recursively
– Think of problem in terms of series of successively smaller but virtually identical subproblems
– Smallest subproblem must be one that can be solved directly
Visual Basic .NET Programming: From Problem Analysis to Program Design 33
Computing Factorials
• Classic example of recursion involves computing factorials
• Definition:
– n! = n * (n – 1) * (n – 2) * . . . * 1, where n is a positive integer
– 0! = 1
• Thinking recursively:
– 4! can be defined as 4 * 3!
Visual Basic .NET Programming: From Problem Analysis to Program Design 34
Computing Factorials (continued)
• Calculating factorials can produce a number that is too large to be represented in memory space allocated to a particular variable
– 12! is largest factorial that can be computed using Integer data type to hold result
Visual Basic .NET Programming: From Problem Analysis to Program Design 39
Displaying a Directory Tree
• Generate and display tree containing directories and subdirectories on hard drive
• Depending on size of your hard drive and speed of your computer
– Initially creating directory tree may take a few minutes
Visual Basic .NET Programming: From Problem Analysis to Program Design 41
Sorting an Array
• Recursive methods are frequently used for array processing
• Quicksort
– Recursive technique for sorting one-dimensional array
– Begins by selecting array element to serve as pivot
Visual Basic .NET Programming: From Problem Analysis to Program Design 42
Sorting an Array (continued)
• Quicksort (continued)
– Position pivot such that
• All array elements to right are greater than or equal to pivot
• All array elements to left are less than or equal to pivot
– Elements to right and left of pivot form subarrays
• Sort them using Quicksort
Visual Basic .NET Programming: From Problem Analysis to Program Design 43
Programming Example: Towers of Hanoi
• Classic computer science problem
• Priests in Far Eastern temple were given task of moving stack of 64 disks from one pole to another
• Each disk was slightly different size
– Disks were arranged on pole from top to bottom in order of increasing size
– Intermediate pole also available to hold disks
Visual Basic .NET Programming: From Problem Analysis to Program Design 44
Programming Example: Towers of Hanoi (continued)
• Priests were to move all 64 disks to third pole using the following rules:
– Only one disk can be moved at a time
– Removed disk must be placed on one of other poles
– Under no circumstance may larger disk be placed upon smaller disk
• Priests were told that world would end when they completed their task
Visual Basic .NET Programming: From Problem Analysis to Program Design 47
Programming Example: Towers of Hanoi (continued)
• Recursive algorithm:
– Move top n-1 disks from pole 1 to pole 2, using pole 3 to temporarily hold disks
– Move bottom (nth) disk from pole 1 to pole 3
– Move n-1 disks on pole 2 to pole 3, using pole 1 to temporarily hold disks
• Number of moves required to move the disks is 264-1
Visual Basic .NET Programming: From Problem Analysis to Program Design 48
Summary
• Linked list
– List of object instances that are linked together
– Each instance is called a node
• Stack
– Can be implemented as special form of LinkedList
– LIFO structure
– FCL provides Stack class
Visual Basic .NET Programming: From Problem Analysis to Program Design 49
Summary (continued)
• Queue – Implemented as linked list with objects added at
end and removed from beginning
– Employ FIFO logic
– FCL includes Queue class
• Recursive problem-solving approach – Divides problem into series of smaller problems
– Solves each subproblem
– Assembles result