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

CSE 116
Introduction To Computer Science for Majors 2
Lecture B
Lecture Notes
Stuart C. Shapiro
Spring, 2003


Binary Search Trees

Readings
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.

First Previous Next

Copyright © 2003 by Stuart C. Shapiro. All rights reserved.
Trees drawn by dot

Stuart C. Shapiro <shapiro@cse.buffalo.edu>