Lab 7 - Binary Search and Binary Search Trees
The purpose of this lab is to explore the binary search concept by creating
code that will implement the recursive binary search algorithm and by tracing
the insertion operation for binary search trees out on paper.
Part 1 - Recursive Binary Search (5 pts)
For this portion of the lab, you will implement the recursive binary search
pseudocode. Use lab7.cpp for your main
function. This sets up a sorted array and passes it into the binary search
function. You can alter the values in the array, but make sure to keep them
sorted.
The pseudocode for binary search is:
Given: sorted array, key to search for, start index, end index
Returns: index where key is found or -1 if key is not in array
if start or end indices are invalid
return -1
end-if
if start is equal to end
if array[start] is equal to key
return start
else
return -1
end-if
end-if
calculate mid = (start + end) / 2
if array[mid] is equal to key
return mid
else if array[mid] is less-than key (search right)
return binarySearch(array, key, mid+1, end)
else (key is less-than array[mid], search left)
return binarySearch(array, key, start, mid-1)
end-if
Email your completed lab7.cpp to me for this portion of the assignment.
Part 2 - Binary Search Tree Insertions (5 pts)
Insert the following values into a binary search tree (BST) in the order given.
You only have to turn in the final BST but partial credit will be given for
incorrect trees if all work is shown (e.g. the tree after each insertion):
56, 43, 12, 78, 92, 5, 23, 62
You can turn in a hard-copy or upload an electronic document to Moodle for
this portion of the assignment.