1 / 29

Trees

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.

Download Presentation

Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Trees Lecture 54 Section 11.5 Fri, Apr 28, 2006

  2. Trees • A graph is circuit-free (or acyclic) if it has no nontrivial circuits. • A tree is a connected circuit-free graph.

  3. 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.

  4. My Documents My Music My Pictures My Homework Math 262 Coms 262 Directory Trees

  5. 50 30 75 10 40 85 Binary Search Trees

  6. * +  10 15 25 Expression Trees

  7. 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

  8. Huffman Trees 12 7 5 4 3 3 2 A T 2 1 1 1 W K N 1 1 C D

  9. Huffman Trees 0 1 0 1 0 1 0 1 0 1 A T 0 1 W K N C D

  10. Huffman Trees • Encoding scheme: • A = 00 • T = 01 • C = 1000 • D = 1001 • K = 110 • N = 111 • W = 101

  11. 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.

  12. 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

  13. 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  >

  14. stmt if ( rel-expr ) stmt expr rel-op expr expr ; var > num var = expr num Parse Trees x 0 y 1

  15. 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.

  16. Proof • This contradicts the property that trees are circuit free. • Therefore, there must exist a vertex of degree 1.

  17. 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.

  18. 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.

  19. Proof • By the induction hypothesis, Thas k – 1 edges. • Therefore, T has k edges. • Therefore, the statement is true for all n 1.

  20. 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.

  21. 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).

  22. 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.

  23. 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.

  24. 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.

  25. Counting Rooted Binary Trees • Let Cn be the number of binary trees of size n. • Then

  26. Counting Rooted Binary Trees • The first five terms are 1, 2, 5, 14, 41. • The 6th term is 141 + 214 + 55 + 142 + 411 = 163. • Is there a non-recursive formula?

  27. The Catalan Numbers • These numbers turn out to be the Catalan numbers. • The non-recursive formula is

  28. Restricted Walks • How many walks are there from A to B, by going only east and north? B N A

  29. Counting Unrooted Trees • How many non-isomorphic unrooted trees are there with n vertices? • n = 5:

More Related