Algorithm Analysis (CSC 301 2015F) : Outlines
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] [FAQ] [IRC] [Teaching & Learning]
Current: [Outline] [EBoard] [Reading] [Lab] [Assignment]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Readings]
Reference: [Algorist]
Related Courses: [Walker (2014F)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] [Issue Tracker]
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
SetOfStrings Implementations
SetOfStrings ADT provides
add(set,val), remove(set,val), contains(set,val)union(set,set), subtract(set,set)SetofStrings.
(You need not write code; just describe memory arrangement and
related issues.)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>, whereVis a set,ris a designated element ofV, andchildis a relation (set of tuples of the form<p,c>) in whichpandcare elements ofVsubject to the following restrictions. (For the relation, we might also writechild(p,c).)i. There exist no
vsuch that<v,r>is inchild. (The root is not the child of any other value.)ii. For all
vinVnot equal tor, there exists exactly onepinVsuch that<p,v>is inchild. (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>, whereVis a set,ris a designated element ofV, andchildis a relation (set of tuples of the form<p,i,c>) in whichpandcare elements ofVandiis an element of the natural numbers, subject to the following restrictions.i. There exist no
vandisuch that<v,i,r>is inchild. (The root is not the child of any other value.)ii. For all
vinVnot equal tor, there exist exactly onepinVandiin the natural numbers such that<p,i,v>is inchild. (Every value has exactly one parent.)iii. For all
pinVandiin the natural numbers, there is at most onecinVsuch that<p,i,c>is inchild. (Each parent has at most one ith child.)iv. For all
pinNandi> 0, if there exists ancsuch that<p,i,c>is inchild, then there exists absuch that<p,i-1,b>is inchild. (There are no gaps in the children.)
These are (mostly) informal.
child tuples with the obvious properties (e.g.,
that the parent or child of one is the parent or child of the next
one).z is a descendent of a value a if there exist a series
of values v_0, v_1, ..., v_(n-1), v_n
such that for all i in [0..n), <v_i,v_(i+1)> is in child,
v_0 = a, and v_n = z.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.
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 have special properties. There is a total
ordering on the values in N which we will designate as
<.
<p,0,c> is in child, then c < p.<p,1,c> is in child, then p < c.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.
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)