trees lecture 54 section 11.5 fri, apr 28, 2006. trees a graph is circuit-free (or acyclic) if it...

29
Trees Lecture 54 Section 11.5 Fri, Apr 28, 2006

Upload: melinda-bradley

Post on 29-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Trees

Lecture 54

Section 11.5

Fri, Apr 28, 2006

Trees

A graph is circuit-free (or acyclic) if it has no nontrivial circuits.

A tree is a connected circuit-free graph.

Examples of Trees

A directory structure on a hard drive is a tree structure.

Binary search tree. Expression tree. Huffman tree for data compression. Parse tree in a compiler.

Directory Trees

My Documents

My PicturesMy Music My Homework

Coms 262Math 262

Binary Search Trees

50

854010

7530

Expression Trees

*

251510

+

Huffman Trees

Consider the message

ATTACK AT DAWN Count the frequencies of the letters.

A – 4T – 3C – 1 D – 1

K – 1 N – 1 W – 1

Huffman Trees

12

234

57

3

12 11A T

C D

11W K N

Huffman Trees

A T

C D

W K N

0

10

10

10

1010

1

Huffman Trees

Encoding scheme:A = 00T = 01C = 1000D = 1001K = 110N = 111W = 101

Huffman Trees

Encoding of the message ATTACK AT DAWN:0001010010001100001100100101111.A total of 31 bits.ASCII would require 96 bits (12 chars).Compression ratio = 31/96 < 1/3.

Parse Trees

Consider the program statementif (x > 0) y = 1

The statement includes the following “terminals.”Keyword ifParentheses ( and )Relative operators > and =Variables x and yNumbers 0 and 1

Parse Trees

Parsing the statement uses the following “grammar” rules.stmt if ( rel-expr ) stmtstmt expr ;expr var = exprexpr numrel-expr expr rel-op exprrel-op >

Parse Trees

stmt

;

stmt)rel-expr(if

exprrel-opexpr expr

var>var num

num

= expr

x 0 y

1

Tree Characteristics

Theorem: A tree must have at least one vertex of degree 1.

Proof:Suppose not.Then each edge has degree at least 2.Using the idea in the proof for Euler circuits,

if we pursue a walk, we will hit a dead-end only at a previously visited vertex.

Proof

This contradicts the property that trees are circuit free.

Therefore, there must exist a vertex of degree 1.

Tree Characteristics

Theorem: If a tree has n vertices, then it has exactly n – 1 edges.

Proof by induction. Base case: n = 1.

The tree has 1 vertex and 0 edges, so the statement is true when n = 1.

Proof

Inductive case:Suppose the statement is true when n = k

for some integer k 1.Let T be a tree with k + 1 vertices.Let v be a vertex of T of degree 1.Create a new graph T by removing v and

its one incident edge.Then T is a tree with k vertices.

Proof

By the induction hypothesis, T has k – 1 edges.

Therefore, T has k edges. Therefore, the statement is true for all n

1.

Tree Characteristics

Theorem: Let G be a graph with n vertices. If G has any two of the following three properties, then it has all three properties and G is a tree.G is circuit free.G is connected.G has n – 1 edges.

Rooted Binary Trees

A binary tree is rooted if it has one node designated the root.

The other nodes are arranged in levels, depending on their distance from the root.Level 0 – The root.Level 1 – Nodes adjacent to the root.Level n – Nodes adjacent to level n – 1

nodes (but not the level n – 2 nodes).

Counting Rooted Binary Trees

How many rooted binary trees are there with n vertices?

The tree must have 1 root vertex. That leaves n – 1 vertices for the left and

right subtrees.

Counting Rooted Binary Trees

They can be divvied up in n ways:0 on left, n – 1 on right.1 on left, n – 2 on right.2 on left. n – 3 on right.

:n – 1 on left, 0 on right.

Counting Rooted Binary Trees

In each case, the number of such binary trees is the number of left subtrees of size k times the number of right subtrees of size n – k – 1.

Counting Rooted Binary Trees

Let Cn be the number of binary trees of size n.

Then

.1 allfor ,

)definition(by 11

01

0

nCCC

Cn

kknkn

Counting Rooted Binary Trees

The first five terms are 1, 2, 5, 14, 41. The 6th term is

141 + 214 + 55 + 142 + 411 = 163. Is there a non-recursive formula?

The Catalan Numbers

These numbers turn out to be the Catalan numbers.

The non-recursive formula is

1

2

n

n

n

Cn

Restricted Walks

How many walks are there from A to B, by going only east and north?

N

A

B

Counting Unrooted Trees

How many non-isomorphic unrooted trees are there with n vertices?

n = 5: