120 likes | 259 Views
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. Linked Lists.
E N D
Using Visualization Tools To TeachData Structures and Algorithms Java applets by Dr. R. Mukundan, University of Canterbury, NZ http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/appldsal.html
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.
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.
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.
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.
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.
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.
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.
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. Bubble Sort
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.
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