The Department of Computer Science & Engineering
 STUART C. SHAPIRO: CSE 116 B

# Binary Search Trees

Riley Chapter 9

Introduction
A binary search tree is a binary tree in which, if rootval is the value of the root node, then for every value, leftval, in the left subtree, ```rootval.compareTo(leftval) > 0```, and for every value, rightval, in the right subtree, `rootval.compareTo(rightval) < 0`. We will assume that no two values are content equal.

Example

Notice that it's possible to have a badly balanced binary tree:

The time to insert a value into, or search for a value in, a binary tree could be as quick as O(log n), or as slow as O(n), and sorting by inserting a set of values into a binary tree is between O(n log n) and O(n2). The fastest times can be achieved using height-balanced trees, such as AVL trees (see Chap 9), but we will not discuss them in detail.

Removal
1. of a leaf
 ===>
2. of a branch node with no right subtree
 ===>
3. of a branch node with a right subtree (and possibly a left subtree)
 ===>

SeeSimulator by Michael T. Goodrich.

See implementation of BSTree.java.