300 likes | 396 Views
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.
E N D
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.
My Documents My Music My Pictures My Homework Math 262 Coms 262 Directory Trees
50 30 75 10 40 85 Binary Search Trees
* + 10 15 25 Expression Trees
K – 1 N – 1 W – 1 Huffman Trees • Consider the message ATTACK AT DAWN • Count the frequencies of the letters. • A – 4 • T – 3 • C – 1 • D – 1
Huffman Trees 12 7 5 4 3 3 2 A T 2 1 1 1 W K N 1 1 C D
Huffman Trees 0 1 0 1 0 1 0 1 0 1 A T 0 1 W K N C D
Huffman Trees • Encoding scheme: • A = 00 • T = 01 • C = 1000 • D = 1001 • K = 110 • N = 111 • W = 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 statement if (x > 0) y = 1 • The statement includes the following “terminals.” • Keyword if • Parentheses ( and ) • Relative operators > and = • Variables x and y • Numbers 0 and 1
Parse Trees • Parsing the statement uses the following “grammar” rules. • stmt if(rel-expr)stmt • stmt expr ; • expr var = expr • expr num • rel-expr exprrel-opexpr • rel-op >
stmt if ( rel-expr ) stmt expr rel-op expr expr ; var > num var = expr num Parse Trees 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, Thas 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
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
Restricted Walks • How many walks are there from A to B, by going only east and north? B N A
Counting Unrooted Trees • How many non-isomorphic unrooted trees are there with n vertices? • n = 5: