Chapter 10
This presentation is the property of its rightful owner.
Sponsored Links
1 / 20

Chapter 10 PowerPoint PPT Presentation


  • 111 Views
  • Uploaded on
  • Presentation posted in: General

Chapter 10. Trees. contents. Introduction definitions properties of trees Applications binary search trees Decision trees Prefix codes Tree traversal universal address systems inorder, preorder and postorder traversal infix, prefix and postfix notation – linearize(serialize) trees.

Download Presentation

Chapter 10

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Chapter 10

Chapter 10

Trees


Contents

contents

  • Introduction

    • definitions

    • properties of trees

  • Applications

    • binary search trees

    • Decision trees

    • Prefix codes

  • Tree traversal

    • universal address systems

    • inorder, preorder and postorder traversal

    • infix, prefix and postfix notation – linearize(serialize) trees.

  • trees and sorting -- bubble sort, merge-sort

  • spanning trees

  • minimum spanning trees -- Prim's alg; Kruskal's alg.


10 1 introduction

x

y

==> X is the parent of Y

10.1 Introduction

  • A connected graph w/o simple circuits

  • useful in CS and many fields

    Def1: A tree is a connected undirected graph w/o simple circuits (cf: Forest)

    Ex1: Family Tree:

    Ex2:

tree

tree

has cycle

not connected


Theorem 1

Theorem 1:

Theorm1:An undirected graph is a tree iff there is a unique simple path b/t any two of its vertices.

Pf:”=>”: T: any tree; x,y: two distinct vertices.

Since T is connected, by def., there is a path and hence a simple path a from x to y. Assume there is another simple path b from x to y.

Then by a theorem of ch 8 about simple circuit, there is a simple circuit in T.=> T is not a tree, a contradiction!

“<=“: There is a path b/t any two vertices ==> T is connected. Assume T contains a simple cycle containing x and y. (i.e., x --a-- y --b-- x

==> T contains two simple paths from x to y,

a contradiction.


Rooted tree

Rooted Tree

  • A rooted tree is a tree T=(V,E) associated with a distinguished vertex r ∈ V called the root of T.

  • Given a rooted tree with root r, we can form a directed graph with the property that (u,v) ∈ V2 is an edge of the new directed graph iff u appears immediately before v in the unique simple path from the root r to v.


Chapter 10

f

f

g

g

f

g

d

d

d

b

b

b

e

e

e

a

a

a

c

c

c

T

With root a

With root c

A tree and Rooted Tree formed by designating two roots


Some terminology of rooted tree

Some terminology of rooted tree

  • T=<V,E,r>: a rooted tree.

  • The associated digraph of T is a digraph GT = (V,E’) where E’ =

    {(u,v) ∈ V2|{u,v}∈E and ∃a path r---….---u---v in T}

  • if (u,v) ∈ E’ => u is called the parent of v (and v a child of u)

  • Vertices with the same parent are called siblings.

  • All vertices(except v) in the unique path from the root r to a vertex v are ancestors of v.

  • v is a descendant of u iff u is an ancestor of v.

  • Vertices without children are called leaves

  • Vertices having children are called internal nodes.


Subtrees

f

g

d

b

e

a

c

subtrees

  • T=(V,E,r): a rooted tree; a: a vertex.

  • The subtree of T with a as root is the tree T’=(V’,E’,a) where

    • V’ = {{a} U {v | v is a descendant of a}

    • E’ = E|V’ (E restricted to V’) = { {u,v} ∈ E | {u,v} ⊆ V’ }

  • Example:

    • root : c

    • internal nodes : a,b,c

    • leaves: d,e,f,g

    • parent, child : (c,a),(a,b),…

    • sibling: (b,d), (a,e),(f,g)

    • ancestor, descendant: (c,b),(a,b),…


M ary tree

m-ary tree

Def: A rooted tree is an m-ary tree (m > 0) if every internal vertex has no more than m children.

  • A tree is a full m-ary tree if every internal vertex has exactly m children.

  • A 2-ary tree is also called a binary tree.

  • Ordered rooted tree: the children of each vertex are ordered.

  • The first (second) child of a vertex in an ordered binary tree is called its left (right) child.

  • The subtree rooted at the left [right] child of the root of an ordered binary tree is called its left [right] subtree.


Example 4

a

c

b

g

d

f

e

h

i

k

j

l

m

Example 4:

  • left and right child of d = ?

  • left and right subtree of c = ?

    Notes: 1. Like graphs, there is no standard terminology used to describe trees, rooted trees , ordered rooted trees

    and binary trees.

    2. Readers should

    carefully check

    meanings given to terms

    dealing with trees whenever they occur.


Trees as models

Trees as models

  • Molecule structure:

    • atom  vertex; bond  edge

    • e.g., saturated hydrocarbons CnH2n+2.

    • where C: degree = 4; H : degree = 1

    • => there are (4n+2n+ 2)/2 = 3n+1 edges = #atoms -1

    • => the connected graph must be a tree.

    • => non-isomorphic trees CnH2n+2 are isomers of CnH2n+2

    • => for n = 4 => 2 isomers: butane(丁烷) : C-C-C-C

    • and isobutane(異丁烷) :

  • Representing organizations

  • Computer file system

  • tree-connected parallel processors

C-C-C

|

C


Properties of trees

Properties of trees

Theorem 2: A tree with n ( n >0) vertices has exactly n-1 edges.

Pf: Let T = (V,E) be a tree and choose a vertex r as the root. Now define the mapping g: EV- {r} by

g(e) = v iff e={u,v} and v is the child of u.

Obviously g is 1-1 and onto. Hence |E| = |V|-1.

1-1: if e1 = {u1,v}, e2 = {u2,v} with g(e1) = g(e2)=v =>

both u1 and u2 are the vertex appearing immediately before v in the unique simple path from r to v => u1 = u2. => if e1=e2

onto: v ∈ V – {r}, there is a unique simple path of length > 0 from r to v => there is a vertex u occurring immediately before v => g({u,v}) = v.


Number of vertices in a complete m ary tree

b

d

e

h

i

Number of vertices in a complete m-ary tree

Theorem 3: A full m-ary tree with i internal vertices contains n = mi + 1 vertices.

Pf: Note: every edge in a tree T=(V,E,r) connects one parent(out-degree  ) and one child (in-degree).

=> the number of edges = |E| = Sv∈V #in-degree(v)

[ = |V|-1 : another proof of theorem 2 ]

= Sv∈V #out-degree(V) = Sv∈V #children(v)

= Sv∈V-all leaves #children(v)

--- leaves have no children.

= Sv∈V-all leaves m = mi

----- every internal vertex has m children.

=> |V| = mi + 1.


Theorem 4

Theorem 4

T: a full m-ary tree with n vertices, i internal vertices and l leaves. Then

  • 1. i = (n-1)/m and l = [(m-1)n+1]/m

  • 2. n = mi +1 and l = (m-1) x i + 1

  • 3. n = (ml-1) / (m-1) and i = (l-1)/(m-1).

    pf: All are simple results of the following equations:

    (1) n = mi + 1 (*, Theorem 3: mi = |E| = |V|-1 =n-1) and

    (2) n = l + i (every vertex is either a leaf or an internal vertex)

    1: i = (n-1) /m and l = n - i =n – (n-1)/m =[(m-1)n+1]/m

    2,3: Left as exercises.


Example 9

Example 9

Someone starts a chain letter. Each person receiving the letter either sends to 4 other people or stops sending letter. ==>

  • #people seeing the letter = ? if no one receiving the letter more than once and the chain letter ends after 100 people reading it but did not send it out.

  • #people sending out the letter = ?

    Sol: T=(V,E,r) where

    V = all people receiving (and seeing) the letter

    r = the person starting the game

    E = {(u,v) | u sends a letter to v }

  • #leaves = l = 100 =>

  • n = l + #internal vertices(i) and n = 4i + 1

  • n = l + (n-1) /4 => l = n – (n-1)/4 = (3n+1)/4 =100

  • n = 400 –1 /3 = 133. => i = n –l = 33.


The level of nodes and the height of a tree

a

c

b

g

d

f

e

h

i

k

j

l

m

the Level of nodes and the height of a tree

T=(V,E,r) : a tree, a: a vertex.

  • Level(a) = depth(a) = the length of the unique path from root to a.

  • Level(T) = height(T)= max v∈ V level(v)

  • Example 10:

    • level(v) = ? for each vertex

      v in the tree?

    • Height(T) = ?

  • A m-ary tree is a complete

    tree if it is full and all

    leaves have the same height


Balanced trees

Balanced trees

  • A tree isbalanced if all leaves have approximately the same depth.

    Def: A rooted m-ary tree of height h is called balanced if all leaves are at levels h or h-1.

    Example 11: Which of the rooted trees are balanced ?

T2

T1


Chapter 10

T3


Maximum number of leaves in a tree

Maximum number of leaves in a tree.

Theorem 5: There are at most mh leaves in an

m-ary tree T of height h.

Pf: By math ind on h.

Basis: h = 0 => T has only 1 = m0 node, which is a leaf.

Ind. case: Assume every m-ary tree of height h has leaves ≤ mh

=> m-ary tree T of height h+1 has at most m subtrees, each of height h =>

#leaves(T) = St’ is a subtree of T #leaves(T’)

≤ m x mh = m h+1.

Theorem 5’ : Every complete m-ary tree of height h has

mh leaves. Pf: Like Theorem 5.


Corollary 1

Corollary 1

  • Corollary 1.

    1. If an m-ary tree of height h has l leaves, then h ≥ [logm l].

    2. If the m-ary tree is full and balanced, then h =[logm l], where […] is the ceiling function.

    Pf:1. By Theorem 5, l ≤ m h; hence h ≥ log m l. Since h is an integer, h ≥ [log m l].

    2. ∵ balanced => h-1 ≤ level of leaves ≤ h

    ∵ height = h => Some vertex is at level h.

    => mh-1 < l ≤ m h.

    => h-1< log m l ≤ h

    => log m l ≤ h < 1 + log m l

    => h = [log m l].


  • Login