Unformatted text preview:

Lecture 37OutlineEraseOneBSTRemove 1BSTRemove 2BSTRemoveMax 1BSTRemoveMax 2EraseSearchingAnalysis of Algorithms 1Analysis of Algorithms 2Linear SearchAnalysis of Linear SearchBinary Search 1Binary Search 2Binary Search 3Binary Search 4Binary Search 5Analysis of Binary Search 1Analysis of Binary Search 2Analysis of Binary Search 3Friday, November 19 CS 215 Fundamentals of Programming II - Lecture 37 1Lecture 37Log into Linux. No files to copy, but will finish Bag class from last time.Reminder: Homework 8 due today. Try to have Project 7 mostly done by Monday.Homework 9 posted to course webpage.Questions?Friday, November 19 CS 215 Fundamentals of Programming II - Lecture 37 2OutlineFinish Bag class reimplementation using BSTSearchingAlgorithm analysisFriday, November 19 CS 215 Fundamentals of Programming II - Lecture 37 3EraseOneThe erasing algorithm for BSTs can be implemented directly, and is not too difficult. However, a parent pointer must be maintained in order to delete a node from a tree in a manner similar to linked lists.The textbook presents an indirect method using recursion. Two auxilliary functions, BSTRemove and BSTRemoveMax, are used.Friday, November 19 CS 215 Fundamentals of Programming II - Lecture 37 4BSTRemoveThis function takes advantage of the fact that we can use the Left( ) and Right( ) operations to change the values of the left and right children pointers, respectively. The function receives a root pointer and a target, and passes back the root of the tree with the target removed as well as returning true if it removed the target.As usual, we handle the empty tree first and return false.Friday, November 19 CS 215 Fundamentals of Programming II - Lecture 37 5BSTRemoveOtherwise, if the target is less than or greater than the node value, we call BSTRemove with the left or right child pointer, respectively.When the target is found, there are two cases:If there is no left child, we can just make the right child the new root and delete the node.If there is a left child, we need to replace the root node value with its immediate predecessor and delete that node. This is done using BSTRemoveMax.Friday, November 19 CS 215 Fundamentals of Programming II - Lecture 37 6BSTRemoveMaxBSTRemoveMax receives a root pointer and passes back the root of the tree with the maximum element removed. It also passes back the item whose node was delete in removed. (Since we call BSTRemoveMax with the data field of the node where we want the maximum value to go, this causes that value to be stored in the correct place.)Friday, November 19 CS 215 Fundamentals of Programming II - Lecture 37 7BSTRemoveMaxThere are two cases to considerThe node has no right child, which means we have found the maximum value in the tree. We set removed to the value in this node and then replace it with its left child.The node has a right child, so the maximum is still farther down the right subtree and we call BSTRemoveMax with the right child.Complete the BSTRemoveMax function in bag6.hFriday, November 19 CS 215 Fundamentals of Programming II - Lecture 37 8EraseErase is similar to EraseOne, except that it must delete all of the nodes containing the target.It uses BSTRemoveAll, which is left as a bonus exercise.Friday, November 19 CS 215 Fundamentals of Programming II - Lecture 37 9SearchingSearching a "list" of values is a common computational task.We will look at different at different methods of searchingWe will use these methods to develop some algorithm analysis techniques, which we will look at first.Friday, November 19 CS 215 Fundamentals of Programming II - Lecture 37 10Analysis of AlgorithmsAs we noted at the beginning of this course, algorithms are characterized by the number of steps performed as a function of the size of the input to the algorithm written as T(n).Generally, we do not need to count every machine operation, just the "important" ones like array element access, or comparisons, or exchanges. We will count algorithm steps.Recall that we use "Big-O" notation O(f(n)) to describe running time or complexity of an algorithm, where the algorithm runs proportionally to f(n) for sufficiently large n.Friday, November 19 CS 215 Fundamentals of Programming II - Lecture 37 11Analysis of AlgorithmsThere are 3 ways to view running time: best-case, average-case, and worst-case. "Best-case" generally is not very interesting unless it occurs with very high probability.We generally look at the "worst-case". That is, the types of inputs that will cause the most number of steps to be executed. Sometimes also look at "average-case". Most of the time this has the same running-time as the worst-case, but not always.Friday, November 19 CS 215 Fundamentals of Programming II - Lecture 37 12Linear SearchStart with LinearSearch (textbook calls it serial search) function that receives a vector, v, and a value, target. The function returns the index of the target in the vector, or the size of the vector (to indicate target not found).1. Initialize position to 0; found to false2. While position < v.size( ) and not found do 2.1. If v[position] equals target then 2.1.1. Set found to true 2.2. Increment position3. Return positionFriday, November 19 CS 215 Fundamentals of Programming II - Lecture 37 13Analysis of Linear SearchWhat is an example of worst-case input?How many algorithm steps are performed?What is the worst-case running time?What is an example of average-case input?How many algorithm steps are performed?What is the average-case running time?What is an example of best-case input?How many algorithm steps are performed?What is the best-case running time?Friday, November 19 CS 215 Fundamentals of Programming II - Lecture 37 14Binary SearchIf searching is to be performed many times over a large amount of data, it is worthwhile to find a faster algorithm.Binary search is a much faster algorithm, but works only on sorted containers with a random access operation like operator[ ] like vectors. Whether it is worth the effort to sort a vector depends on how many searches are performed on the data.Friday, November 19 CS 215 Fundamentals of Programming II - Lecture 37 15Binary SearchThe BinarySearch function searches an index range [first, first+size) of a vector for a target where size is the number of elements in the range. (Note this range is closed on the


View Full Document

UE CS 215 - Lecture 37

Documents in this Course
Lecture 4

Lecture 4

14 pages

Lecture 5

Lecture 5

18 pages

Lecture 6

Lecture 6

17 pages

Lecture 7

Lecture 7

28 pages

Lecture 1

Lecture 1

16 pages

Lecture 5

Lecture 5

15 pages

Lecture 7

Lecture 7

28 pages

Load more
Download Lecture 37
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 37 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 37 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?