Title: 308-203A Introduction to Computing II Lecture 13: Trees
1308-203AIntroduction to Computing IILecture
13 Trees
Fall Session 2000
2Trees as graphs
- Weve already seen particular examples of
- trees, aka
- Binary search trees
- Call trees of recursive functions (e.g.
Merge-sort) - Heaps
- More generally trees are a special kind of
graphs
3Definition
Definition A Forest is an undirected, acyclic
graph
4Definition
Definition A Free Tree is a connected,
undirected, acyclic graph
Note the connected components of a forest are
(free) trees
5Is this really what weveseen before as trees?
We have not (yet) made any distinction of
parent or child vertices. Topologically, the
structure has the same important properties, as
we will see presently...
6Defining Characteristics
The following statements are equivalent
1. G (V, E) is a (free) tree 2. Any two
vertices v0 and v1 in V are connected by a
unique, simple path 3. G is singly-connected
(removing any edge disconnects the graph)
More on next slide
7Defining Characteristics
The following statements are equivalent
4. G is connected and E V -1 5. G is
acyclic and E V -1 6. G is acyclic but the
addition of any edge will create a cycle
More on previous slide
8Proof
We prove the following sequence of the
above facts 1 ? 2 ? 3 ? 4 ? 5 ? 6 ? 1
This means any one of these six assertions
proves all the others.
9Any questions?
10Rooted Trees
For a tree G (V, E), pick any vertex v in V and
call it the root. All vs neighbors are
considered children of v. In this way, we can
define a parent-child relationship for
neighboring nodes in G.
11Rooted Trees
You can think of rooting a tree in terms of the
following recursive pseudocode
RootTree(Vertex v) for u ? neighbors(v)
parent(v) u if v ? parent(u), child(u)
child(u) ? v RootTree(u)
12Example
Consider b the root of this (otherwise free)
tree
root
c
b
h
a
d
g
b
e
f
13Example
Consider b the root of this (otherwise free)
tree
c
b
h
a
a
d
d
g
b
e
f
14Example
Consider b the root of this (otherwise free)
tree
c
b
h
a
a
d
d
g
b
c
g
e
f
15Example
Consider b the root of this (otherwise free)
tree
c
b
h
a
a
d
d
g
b
c
g
e
f
h
e
f
Isomorphic tree with parent/child relationships
Original tree
16K-ary trees
Definition A rooted tree for which every vertex
has at most k children is called a k-ary
tree. Definition In particular, a k-ary tree in
which every vertex has at most 2 children is
called binary tree. Definition A k-ary tree in
which every vertex has exactly k children is
called a complete k-ary tree.
17Facts
- For a complete k-ary tree with of depth d
- There are k i nodes at depth i
- There are n (k d - 1)/(k-1) nodes
- Conversely, for a complete k-ary tree of n
- nodes, the depth is
- d logk n (k-1) 1 O( log n)
18Any questions?