Searching via Traversals Searching a Binary Search Tree (BST) Binary Search on a Sorted Array Data Structure Conversion and Helper Modules.

Slides:



Advertisements
Similar presentations
Traversing a Binary Tree Binary Search Tree Insertion Deleting from a Binary Search Tree.
Advertisements

Linked List: Traversal Insertion Deletion. Linked List Traversal LB.
PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists.
Chapter 12 Binary Search Trees
Chapter 4: Trees Part II - AVL Tree
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
Recursion, pt. 2: Thinking it Through. What is Recursion? Recursion is the idea of solving a problem in terms of solving a smaller instance of the same.
CS 206 Introduction to Computer Science II 09 / 24 / 2008 Instructor: Michael Eckmann.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Announcements NP-Complete Problems Decidable vs. Undecidable Problems Review.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Chapter 11 Arrays Continued
Searching Given a collection and an element (key) to find… Output –Print a message (“Found”, “Not Found) –Return a value (position of key ) Don’t modify.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
COMP Recursion, Searching, and Selection Yi Hong June 12, 2015.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
CSC 211 Data Structures Lecture 13
Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one
CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.
Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.
CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Design and Analysis of Algorithms.
Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.
Announcements Review problems Review Outline. Online Survey
Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Review Problems. What is the Big O? i
Binary Search Trees (BST)
Graphs Arrays Iteration Combining Data Structures.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Data Structures Arrays and Lists Part 2 More List Operations.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
LINKED LISTS.
Searching and Sorting Searching algorithms with simple arrays
Searching Given a collection and an element (key) to find… Output
Sorting.
Lecture 14 Traversals of Linked Lists Preorder BST Traversal Postorder BST Traversal Miscellaneous BST Traversals Depth First vs Breadth First Array Traversals.
Data Structures Interview / VIVA Questions and Answers
ENEE150 Discussion 13 Section 0101 Adam Wang.
Simple Dynamic Data (Linked Lists)
Chapter 20: Binary Trees.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 21: Binary Trees.
Linear and Binary Search
Search,Sort,Recursion.
Searching CLRS, Sections 9.1 – 9.3.
Lecture 16 Bubble Sort Merge Sort.
Data Structures & Algorithms
CSC 143 Binary Search Trees.
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

Searching via Traversals Searching a Binary Search Tree (BST) Binary Search on a Sorted Array Data Structure Conversion and Helper Modules

Plan of Attack Simple searching just involves traversing a data structure until the data element is found. The only twist is that if the collection is ordered (like a linked lists) you can stop if you don’t find an element and you pass the point where it should be located. So we’re going to skip numerous slides which essentially review material we’ve already covered. So we’re going to focus on Binary Search and Data Structure Conversion But first the twist... LB

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search head Page 122

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) okToExit isoftype Boolean okToExit <- FALSE loop exitif(okToExit) if(cur = NIL OR cur^.data > target) then okToExit <- TRUE print(“Target data not found”) elseif(cur^.data = target) then okToExit = TRUE print(“Found target”) // Could transfer data to param here else cur <- cur^.next endif endloop endprocedure // Search LB

Same logic can be applied to Trees Skipping stuff starting on Page 120! LB

Searching via Traversals

An Example Imagine we have a database of students I want to know if Bob Smith is in my class. I want to search my database and have the module print out “IN THE CLASS” or “NOT IN THE CLASS.”

Another Situation I have the same student database. I need to view Alice Jones’ grades and update them. Again, I need to search the database. This time, I need Alice’s information returned to me so I can view and modify them (i.e. need access).

The Scenario We have some collection (stored in an array, tree, or list) –May be sorted or not –May be empty or not We want the determine if a particular element is in the collection or not. We need to search for the element.

Searching Given the collection and an element to find… Determine whether the “target” element was found in the collection –Print a message –Return a value (an index or pointer, etc.) Don’t modify the collection in the search!

Key Fields Often, our collections will hold complex structures (i.e. have many items of information in each). It is common to organize our collection using one “part” (or field) –Name –Student Number This is called the “key field” Then we can search on the key field.

A Simple Search A search traverses the collection until –The desired element is found –Or the collection is exhausted If the collection is ordered, I might not have to look at all elements –I can stop looking when I know the element cannot be in the collection.

Searching in an Unordered Collection Let’s determine if the value 12 is in the collection: \\ 12 Found! Head

Searching in an Unordered Collection Let’s determine if the value 13 is in the collection: \\ 13 Not Found! Head

Searching in an Ordered Collection Let’s determine if the value 13 is in the collection: \\ 13 Not Found! Head

Search Examples Arrays Linked Lists Binary Trees

Search Example on a List

An Iterative Search Let’s build a module to search a list of numbers using iteration. We’ll print whether the number was found or not. Steps: –Traverse until we find a match or reach the end. –Print the results.

An Iterative Un-Ordered Search procedure Search ( ??? ) loop ??? exitif( ??? ) ??? endloop if( ??? ) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search

An Iterative Un-Ordered Search procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop ??? exitif( ??? ) ??? endloop if( ??? ) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search

An Iterative Un-Ordered Search procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif( cur = NIL ) cur <- cur^.next endloop if( ??? ) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search

An Iterative Un-Ordered Search procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if( ??? ) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search

An Iterative Un-Ordered Search procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if( cur = NIL ) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search

An Iterative Un-Ordered Search procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if( cur = NIL ) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search

What’s Different for An Iterative Ordered Search? procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if( cur = NIL ) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search

An Iterative Ordered Search procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data >= target)) cur <- cur^.next endloop if((cur = NIL) OR (cur^.data > target)) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search

Tracing the Search Now let’s call the un-ordered version of the module: algorithm Example... Search(head, 4)... endalgorithm // Example head

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search head target = 4 cur

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search head target = 4 cur

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search head target = 4 cur

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search head target = 4 cur

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search head target = 4 cur

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search head target = 4 cur

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search head target = 4 cur

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search head target = 4 cur

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search head target = 4 cur

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search head target = 4 cur

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search head target = 4 cur

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search head target = 4 cur

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search head target = 4 cur

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search head target = 4 cur

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search head target = 4 cur

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search head target = 4 cur

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search head target = 4 cur Target data found

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data = target)) cur <- cur^.next endloop if(cur = NIL) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search head target = 4 cur

Tracing the Ordered Search Now let’s call the ordered version of the module: algorithm Example... Search(head, 9)... endalgorithm // Example head

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data >= target)) cur <- cur^.next endloop if((cur = NIL) OR (cur^.data > target)) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search target = 9 cur head

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data >= target)) cur <- cur^.next endloop if((cur = NIL) OR (cur^.data > target)) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search target = 9 cur head

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data >= target)) cur <- cur^.next endloop if((cur = NIL) OR (cur^.data > target)) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search target = 9 cur head

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data >= target)) cur <- cur^.next endloop if((cur = NIL) OR (cur^.data > target)) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search target = 9 cur head

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data >= target)) cur <- cur^.next endloop if((cur = NIL) OR (cur^.data > target)) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search target = 9 cur head

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data >= target)) cur <- cur^.next endloop if((cur = NIL) OR (cur^.data > target)) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search target = 9 cur head

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data >= target)) cur <- cur^.next endloop if((cur = NIL) OR (cur^.data > target)) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search target = 9 cur head Target data not found

procedure Search ( cur isoftype in ptr toa Node, target isoftype in Num ) loop exitif((cur = NIL) OR (cur^.data >= target)) cur <- cur^.next endloop if((cur = NIL) OR (cur^.data > target)) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search target = 9 cur head

Searching an Array Same principles apply: –Examine cells until we find a match or exhaust the possibilities –Then indicate whether the item was in the collection (print or return information)

Un-Ordered Iterative Array Search procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search

Calling the Module algorithm Example2 MAX is 6... Search(MyNumArray, 13)... endalgorithm // Example2

procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search my_array target = 13

procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search my_array target = 13

procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search my_array target = 13

procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search my_array target = 13

procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search my_array target = 13

procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search my_array target = 13

procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search my_array target = 13

procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search my_array target = 13 Target data found

procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print(“Target data not found”) else print(“Target data found”) endif endprocedure // Search my_array target = 13

Binary Tree Searches Page 130

Searching With Binary Trees Have to visit every node in the binary tree. 3 types of traversals: PreorderInorderPostorder Check Node Go left Go right Go left Check Node Go right Go left Go right Check Node

Pre-Order Search Traversal Algorithm As soon as we get to a node, check to see if we have a match Otherwise, look for the element in the left sub-tree Otherwise, look for the element in the right sub-tree 14 Left ??? Right ???

Search on a Binary Tree function BTSearch returnsa Boolean (cur iot in ptr toa Node, toFind iot in Num) if (cur = NIL) then BTSearch returns false elseif (cur^.data = toFind) then BTSearch returns true else BTSearch returns BTSearch(cur^.left, toFind) OR BTSearch(cur^.right, toFind) endif endfunction // BTSearch

94 cur Find 14 LB

94 cur Find 14 LB

Find 14 cur LB

Find 14 cur LB

Find 14 cur LB

Find 14...Found!!! cur LB

Search on a Binary Tree function BTSearch returnsa Boolean (cur iot in ptr toa Node, toFind iot in Num) if (cur = NIL) then BTSearch returns false elseif (cur^.data = toFind) then BTSearch returns true else BTSearch returns BTSearch(cur^.left, toFind) OR BTSearch(cur^.right, toFind) endif endfunction // BTSearch LB Once we’ve found the 14 do we search the right subtree?

Summary Searches involve visiting elements in a collection until –A match is found –Or until all possible locations are exhausted With sorted collections, we may be able to “stop early” Once we determine if the element is in the collection, then we do some work: –Print the results –Return some information (pointer, index)

Questions?

Searching a Binary Search Tree (BST)

The Scenario We’ve got a Binary Search Tree and we want to determine if an element is in the collection. 14 Less Than 14 Greater Than 14

Cutting the Work in Half In searching for a match, we can ignore half of the tree at each comparison. 14 Less Than 14 Looking for 42… It must be to the right! Greater Than 14

The Binary Search Algorithm if at NIL // not found DO NOT FOUND WORK elseif ( match ) then // found DO FOUND WORK elseif ( value to match < current value ) recurse left // must be to left else recurse right // must be to right

The Binary Search for a BST procedure Search(cur iot in Ptr toa Node, target isoftype in Num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search

head 42. Search(head, 35) Search(head, 87)

head 42. Search(head, 35) Search(head, 87)

. Search(head, 35) Search(head, 87). head Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 target = 35 cur

. Search(head, 35) Search(head, 87). head Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 target = 35 cur

. Search(head, 35) Search(head, 87). head Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 target = 35 cur

. Search(head, 35) Search(head, 87). head Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 target = 35 cur

. Search(head, 35) Search(head, 87). head Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 target = 35 Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur

. Search(head, 35) Search(head, 87). head Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 target = 35 Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur

. Search(head, 35) Search(head, 87). head Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 target = 35 Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur

. Search(head, 35) Search(head, 87). head Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 target = 35 Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur

. Search(head, 35) Search(head, 87). head Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 target = 35 Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur

. Search(head, 35) Search(head, 87). head Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 target = 35 Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur

. Search(head, 35) Search(head, 87). head Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search Target Found cur

. Search(head, 35) Search(head, 87). head Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur

. Search(head, 35) Search(head, 87). head Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur

. Search(head, 35) Search(head, 87). head Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search 47 cur

. Search(head, 35) Search(head, 87). head

. Search(head, 35) Search(head, 87). head Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search target = 87 cur

. Search(head, 35) Search(head, 87). head Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search target = 87 cur

. Search(head, 35) Search(head, 87). head Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search target = 87 cur

. Search(head, 35) Search(head, 87). head Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search target = 87 cur

. Search(head, 35) Search(head, 87). head Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search target = 87 Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur

. Search(head, 35) Search(head, 87). head Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search target = 87 Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur

. Search(head, 35) Search(head, 87). head Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search target = 87 Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur

. Search(head, 35) Search(head, 87). head Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search target = 87 Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur

. Search(head, 35) Search(head, 87). head Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search target = 87 Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur

. Search(head, 35) Search(head, 87). head Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search Not Found cur

. Search(head, 35) Search(head, 87). head Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur

. Search(head, 35) Search(head, 87). head Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur

. Search(head, 35) Search(head, 87). head Procedure Search( cur iot in Ptr toa Node, target iot in num) if(cur = NIL) then print(“Not Found”) elseif(cur^.data = target) print(“Target Found”) elseif(cur^.data > target) Search(cur^.left, target) else Search(cur^.right, target) endif endprocedure // Search cur

. Search(head, 35) Search(head, 87). head

Summary We can cut the work in half after each comparison. Recurse in one direction – left or right. When we reach NIL, then we no the value wasn’t in the binary search tree.

Questions?

Binary Search on a Sorted Array

The Scenario We have a sorted array We want to determine if a particular element is in the array –Once found, print or return (index, boolean, etc.) –If not found, indicate the element is not in the collection

A Better Search Of course we could use our simpler search and traverse the array But we can use the fact that the array is sorted to our advantage This will allow us to reduce the number of comparisons

Binary Search Requires a sorted array or a binary search tree. Cuts the “search space” in half each time. Keeps cutting the search space in half until the target is found or has exhausted the all possible locations. Inappropriate for linked lists because linked lists lack random access.

The Algorithm look at “middle” element if no match then look left or right

The Algorithm look at “middle” element if no match then look left or right

The Algorithm look at “middle” element if no match then look left or right

The Algorithm look at “middle” element if no match then look left or right

The Binary Search Algorithm Return found or not found (true or false), so it should be a function. We’ll use recursion We must pass the entire array into the module each pass, so set bounds (search space) via index bounds (parameters) –We’ll need a first and last

The Binary Search Algorithm calculate middle position if (first and last have “crossed”) then DO NOT FOUND WORK elseif (element at middle = to_find) then DO FOUND WORK elseif to_find < element at middle then Look to the left else Look to the right

Looking Left Use indices “first” and “last” to keep track of where we are looking Move left by setting last = middle – FLM L

Looking Right Use indices “first” and “last” to keep track of where we are looking Move right by setting first = middle FLM F

Binary Search Example – Found Looking for 42 FLM

Binary Search Example – Found Looking for 42 FLM

Binary Search Example – Found found – in 3 comparisons F L M

Binary Search Example – Not Found Looking for 89 FLM

Binary Search Example – Not Found Looking for 89 FLM

Binary Search Example – Not Found Looking for 89 FL M

Binary Search Example – Not Found not found – 3 comparisons FL

Function Find returnsa boolean (A isoftype in ArrayType, first, last, to_find isoftype in num) // contract (Pre, Post, Purpose) here middle isoftype num middle <- (first + last) div 2 if (first > last) then Find returns false elseif (A[middle] = to_find) then Find returns true elseif (to_find < A[middle]) then Find returns Find(A, first, middle–1, to_find) else Find returns Find(A, middle+1, last, to_find) endfunction

Summary Binary search reduces the work by half at each comparison With the array, we need to keep track of which “part” is currently “visible” We know the element isn’t in the array when our first and last indices “cross”

Questions?

Data Structure Conversion and Helper Modules

The Scenario Imagine you have a collection of data stored in a linked list. But now you need to store this data in a binary search tree. You’ll need to convert the data from one structure to the other without altering the data itself.

An Example: List to Tree

Purpose of Structure Conversion Given the input data arranged in one format: Keep the original data and structure intact Return the same data, but arranged in a different order or within a different data structure

Conversion Examples Linked List to a Binary Search Tree Binary Search Tree to a Sorted Linked List Array to a Linked List Array to a Binary Search Tree Any others are also valid, but going from a dynamic structure to a static structure may run out of allocated space.

Steps Involved Initialize the new structure Traverse the original structure –At each element, insert the current element into the new structure Return the new structure Note that the original structure and data is unchanged.

An Example: List to Tree

Initialize the New Structure Head

Traverse the Original Structure and Insert into the New Structure Head

Traverse the Original Structure and Insert into the New Structure Head

Traverse the Original Structure and Insert into the New Structure Head

Traverse the Original Structure and Insert into the New Structure Head

Traverse the Original Structure and Insert into the New Structure Head

Traverse the Original Structure and Insert into the New Structure Head

Traverse the Original Structure and Insert into the New Structure Head

Traverse the Original Structure and Insert into the New Structure Head

Done Traversing Now Return the New Structure Head

Needed Modules A module to initialize the new structure A module to traverse the initial structure A module to insert an element into the new structure But all of this should be transparent to the user!

Helper Modules In order to correctly perform some tasks, helper modules are often necessary. Helper (or wrapper) modules are other modules that help in the completion of a task. They are often useful in hiding details.

The Conversion Helper Modules Convert Original Structure New Structure

The Conversion Helper Modules Convert Initialize Original Structure New Structure Traverse Original Insert into New

A First Try at the Conversion Module procedure Tree_from_List(cur isoftype in Ptr toa List_Node, head isoftype in/out Ptr toa Tree_Node ) // Pre: head initialized to NIL if( cur <> NIL ) then BST_Insert(head, cur^.data ) Tree_from_List(cur^.next, head ) endif endprocedure

A First Try at the Algorithm algorithm ListToTree // some delcarations TreePtr isoftype Ptr toa Tree_Node ListPtr isoftype Ptr toa List_Node // build the list work here TreePtr <- NIL Tree_from_List( ListPtr, TreePtr ) // now the tree is built endalgorithm // ListToTree The user is required to initialize the list pointer. It is better to wrap the recursive call into a user interface program as follows…

A Better Main Algorithm The initialization should be hidden inside the conversion module itself. algorithm BetterListToTree TreePtr isoftype Ptr toa Tree_Node ListPtr isoftype Ptr toa List_Node // build the list work is here Nice_Tree_from_List( ListPtr, TreePtr ) // now the tree is built endalgorithm // BetterListToTree

The “Wrapper” Module procedure Nice_Tree_from_List( cur isoftype in Ptr toa List_Node, head isoftype out Ptr toa Tree_Node ) // perform the initialization head <- NIL // build the list Tree_From_List( cur, head ) endprocedure

Summary Keep the original structure unchanged Initialize the new structure Traverse the old structure –Insert each element into the new structure Helper (wrapper) modules –Help hide details –Properly abstract tasks into separate modules

Questions?