Algorithm Analysis (CSC 301 2015F) : Outlines

Outline 11: Binary Search Trees


Held: Monday, 21 September 2015

Back to Outline 10 - An Introduction to Trees. On to Outline 12 - Red-Black Trees.

Summary

We continue our initial explorations of trees and consider their use in binary search trees.

Related Pages

Overview

Administrivia

Upcoming Work

Extra Credit

Academic

Peer

Trees, Formalized

We all have an informal notion of what a tree is. However, as we design more careful algorithms, we also need to think more formally about the definition of trees.

Pause! How might you define trees using formal notation?

Here's what I might say. (I assume the natural numbers start with 0.)

A tree is triplet, <V,r,child>, where V is a set, r is a designated element of V, and child is a relation (set of tuples of the form <p,c>) in which p and c are elements of V subject to the following restrictions. (For the relation, we might also write child(p,c).)

i. There exist no v such that <v,r> is in child. (The root is not the child of any other value.)

ii. For all v in V not equal to r, there exists exactly one p in V such that <p,v> is in child. (Every value has exactly one parent.)

We might also have ordered trees, in which we number the children.

A tree is triplet, <V,r,child>, where V is a set, r is a designated element of V, and child is a relation (set of tuples of the form <p,i,c>) in which p and c are elements of V and i is an element of the natural numbers, subject to the following restrictions.

i. There exist no v and i such that <v,i,r> is in child. (The root is not the child of any other value.)

ii. For all v in V not equal to r, there exist exactly one p in V and i in the natural numbers such that <p,i,v> is in child. (Every value has exactly one parent.)

iii. For all p in V and i in the natural numbers, there is at most one c in V such that <p,i,c> is in child. (Each parent has at most one ith child.)

iv. For all p in N and i > 0, if there exists an c such that <p,i,c> is in child, then there exists a b such that <p,i-1,b> is in child. (There are no gaps in the children.)

Terminology

These are (mostly) informal.

Binary Trees

For binary trees, we say that each value has at most two children.
(We limit i to 0 or 1.) We often designate the child relationships as left and right, rather than child. We don't usually require that nodes have both left and right children (removing restriction iv.

Traversing Trees

We normally speak about this in terms of the order in which we process values, rather than the order in which we visit them.

Binary Search Trees

Binary search trees have special properties. There is a total ordering on the values in N which we will designate as <.

We like binary search trees because we can find any value in O(height) time. If the tree is kept well balanced, that's O(logn), where n is the number of values in the tree.

Balancing Binary Search Trees

How do we keep trees balanced? Let's start by thinking about some slightly imbalanced trees. Lowercase letters will represent values, uppercase letters will represent trees. Numbers in parentheses represent the height of a tree

       a(n+3)
        / \
       /   \
    b(n+2) C(n)
     / \
    /   \
 D(n+1) E(n)