Using Visualization Tools to Teach Data Structures and Algorithms

advertisement
Using Visualization Tools
To Teach
Data Structures and Algorithms
Java applets by Dr. R. Mukundan, University of Canterbury, NZ
http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/appldsal.html
ACM/JETT Workshop - August 4-5, 2005
Linked Lists
• A linked list is a sequence of items arranged one
after another, with each containing a link to the
next.
• Insertions may be made at either end.
• Removals can only be made at the beginning.
• Access is sequential starting from the first item.
ACM/JETT Workshop - August 4-5, 2005
Queue
• A queue is a “First-In, First-Out”
(FIFO) data structure similar to a
line of people at a ticket window.
• Insertions are made at one end,
called the tail.
• Removals are made at the other
end, called the head.
ACM/JETT Workshop - August 4-5, 2005
Queue as Linked List
• A queue is often implemented using a linked list.
• The first position in the list becomes the head.
– That’s the only place in a linked list where removals
are possible!
• The last position in the list becomes the tail.
ACM/JETT Workshop - August 4-5, 2005
Stack
• A stack is an ordered collection of items
that can only be accessed at one end
called the “top” of the stack.
• Insertions are “pushed” onto the top.
• Removals are “popped” off the top in
the reverse order of insertions.
• A stack is a “Last-In, First-Out” (LIFO)
data structure.
ACM/JETT Workshop - August 4-5, 2005
Stack as Linked List
• A stack is often implemented using a linked list.
• The first position in the list becomes the top of
the stack.
– That’s the only place in a linked list where
both insertions and removals are possible.
• The pointer to the last position in the list is
unused.
ACM/JETT Workshop - August 4-5, 2005
Linear Search
• A linear search examines items sequentially.
• Assuming that the items are unordered, then
– The worst-case number of probes is N.
– The average number of probes is N÷2.
ACM/JETT Workshop - August 4-5, 2005
Binary Search
• A binary search is similar to a dictionary search.
– Items must be ordered.
– Start at the middle, then search left or right half.
Repeat until found.
• The worst-case number of probes is log2N.
• The average number of probes is (log2N)÷2.
ACM/JETT Workshop - August 4-5, 2005
Bubble Sort
• Pass 1: Process items 0..N-1 (all)
• Pass 2: Process items 0..N-2
…
• Pass N-1: Process items 0..1
• Each Pass: Move largest value to the end.
– sequentially compare adjacent pairs and exchange
when out of order.
ACM/JETT Workshop - August 4-5, 2005
Merge Sort
• Divide the data into two
subsets of equal size.
• Sort each subset
– Invoke Merge Sort recursively
• Merge the two subsets into a
single set.
• Note that comparisons and
exchanges do not occur until
recursion has fully descended.
ACM/JETT Workshop - August 4-5, 2005
Binary Tree Traversal
• Pre-Order:
– Process the root
– Pre-Order traverse left subtree
– Pre-Order traverse right subtree
• In-Order:
– In-Order traverse left subtree
– Process the root
– In-Order traverse right subtree
• Post-Order:
– Post-Order traverse left subtree
– Post-Order traverse right subtree
– Process the root
ACM/JETT Workshop - August 4-5, 2005
Download