Docsity

Binary Search and Binary Search Trees, Study notes of Computer Science

Toggle sidebar
bg1
CS112-2008S-33 Binary Search & Trees 1
33-0: Sorting
•Review:
•Insertion Sort
•Selection Sort
33-1: Sorted Lists
•Maintain a sorted list:
•Add an element
•Determine if an element is in the list
•Print out the list in order
•Array vs. Linked List
33-2: Sorted Lists
•Array
•Finding an element is fast
•Inserting an element is slow
33-3: Sorted Lists
•Array
•Finding an element is fast
•Binary Search
•O(lg n)
•Inserting an element is slow
•Move larger elements one to the right
•O(n)
33-4: Binary Search
•If we are looking for an element in an empy list, return false
•If the element in the center of the list is what we are looking for, return true
•If the element in the center of the list is less that what we are looking for, discard the left half of the list, continue
looking
•If the element in the center of the list is greater than what we are looking for, discard the right half of the list,
continue looking
33-5: Binary Search
pf3
pf4
pf5

Partial preview of the text

Download Binary Search and Binary Search Trees and more Study notes Computer Science in PDF only on Docsity!

33-0: Sorting

  • Review:
    • Insertion Sort
    • Selection Sort

33-1: Sorted Lists

  • Maintain a sorted list:
    • Add an element
    • Determine if an element is in the list
    • Print out the list in order
  • Array vs. Linked List

33-2: Sorted Lists

  • Array
    • Finding an element is fast
    • Inserting an element is slow

33-3: Sorted Lists

  • Array
    • Finding an element is fast
      • Binary Search
      • O(lg n)
    • Inserting an element is slow
      • Move larger elements one to the right
      • O(n)

33-4: Binary Search

  • If we are looking for an element in an empy list, return false
  • If the element in the center of the list is what we are looking for, return true
  • If the element in the center of the list is less that what we are looking for, discard the left half of the list, continue looking
  • If the element in the center of the list is greater than what we are looking for, discard the right half of the list, continue looking

33-5: Binary Search

boolean binarySearch(int A[], int elem) { int lowIndex = 0; int highInidex = A.length - 1; while (lowIndex <= highIndex) { int midIndex = (lowIndex + highIndex) / 2; if (A[midIndex] == elem) return true; else if (A[midIndex] < elem) highIndex = midIndex - 1; else lowIndex = midIndex + 1; } return false; }

33-6: Binary Search

boolean binarySearch(int A[], int elem, int lowIndex, int highIndex) { if (lowIndex > highIndex) return false; int midIndex = (lowIndex + highIndex) / 2; if (A[midIndex] == elem) return true; else if (A[midIndex] < elem) return binarySearch(A, elem, lowIndex, midIndex - 1); else return binarySearch(A, elem, midIndex + 1, highIndex); }

33-7: Sorted Lists

  • Linked List
    • Finding an element is slow
      • Linear Search
      • O(n)
    • Inserting an element is fast, once we have a pointer to the location to insert
      • O(n) to find insertion location
      • O(1) to insert, once location is found

33-8: Binary Search

  • What if we could do binary search on a linked list?
    • Find would be fast
    • Insertion would also be fast

33-9: Binary Search

  • To do binary search on a linked list, we need to be able to jump to the middle of the list
  • Instead of pointing at the front of the list, point to the middle of the list

4 8 12 15 22 25 28

33-10: Binary Search

  • To do binary search on a linked list, we need to be able to jump to the middle of the list
  • Instead of pointing at the front of the list, point to the middle of the list
  • For each node n, (value stored at node n) ≥ (value stored in left subtree)
  • For each node n, (value stored at node n) < (value stored in right subtree)

33-16: Example Binary Search Trees

4

3

2

1

4

2 6

1 3 5 7

1

2

3

4

33-17: Binary Search Trees

class BSTNode { private BSTNode left; private BSTNode right; private int data; public BSTNode(BSTNode left, BSTNode right, int data) { this.left = left; this.right = right; this.data = data; } public BSTNode left() { return left; }public BSTNode right() public int data() { { return right; return data; } } }

33-18: Finding an Element in a BST

  • Binary Search Trees are recursive data structures, so most operations on them will be recursive as well
  • Recall how to write a recursive algorithm ...

33-19: Writing a Recursive Algorithm

  • Determine a small version of the problem, which can be solved immediately. This is the base case
  • Determine how to make the problem smaller
  • Once the problem has been made smaller, we can assume that the function that we are writing will work correctly on the smaller problem (Recursive Leap of Faith) - Determine how to use the solution to the smaller problem to solve the larger problem

33-20: Finding an Element in a BST

  • First, the Base Case – when is it easy to determine if an element is stored in a Binary Search Tree?

33-21: Finding an Element in a BST

  • First, the Base Case – when is it easy to determine if an element is stored in a Binary Search Tree?
    • If the tree is empty, then the element can’t be there
    • If the element is stored at the root, then the element is there

33-22: Finding an Element in a BST

  • Next, the Recursive Case – how do we make the problem smaller?

33-23: Finding an Element in a BST

  • Next, the Recursive Case – how do we make the problem smaller?
    • Both the left and right subtrees are smaller versions of the problem. Which one do we use?

33-24: Finding an Element in a BST

  • Next, the Recursive Case – how do we make the problem smaller?
    • Both the left and right subtrees are smaller versions of the problem. Which one do we use?
    • If the element we are trying to find is < the element stored at the root, use the left subtree. Otherwise, use the right subtree.

33-25: Finding an Element in a BST

  • Next, the Recursive Case – how do we make the problem smaller?
    • Both the left and right subtrees are smaller versions of the problem. Which one do we use?
    • If the element we are trying to find is < the element stored at the root, use the left subtree. Otherwise, use the right subtree.
  • How do we use the solution to the subproblem to solve the original problem?

33-26: Finding an Element in a BST

  • Next, the Recursive Case – how do we make the problem smaller?
    • Both the left and right subtrees are smaller versions of the problem. Which one do we use?
    • If the element we are trying to find is < the element stored at the root, use the left subtree. Otherwise, use the right subtree.
  • How do we use the solution to the subproblem to solve the original problem?
    • The solution to the subproblem is the solution to the original problem (this is not always the case in recursive algorithms)

33-27: Finding an Element in a BST To find an element e in a Binary Search Tree T :

  • If T is empty, then e is not in T

33-33: Printing out a BST

void print(Node tree) { if (tree != null) { print(tree.left()); System.out.println(tree.element()); print(tree.right()); } }