SUNY Geneseo Department of Computer Science


Binary Tree Search

{Date}

CSci 141, Fall 2003
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture


Misc

Hand out Homework 2

Lab handout available in labs tomorrow

Questions?

Binary (Search) Trees

Messages

Searching a binary search tree

  1. Compare data you seek to root
  2. If data equals root, stop
  3. If data is less than root, repeat in left subtree
  4. If data greater than root, repeat in right subtree

Mini-assignment: phrase search w/ tree messages

    boolean search( x )
        if this.isEmpty()
            return false
        else if x.compareTo( this.root() ) > 0
            return this.right().search( x )
        else if x.compareTo( this.root() ) < 0
            return this.left().search( x )
        else
            // root = x
            return true

x.compareTo(y)

Does this really work?


Next Lecture