trees chapter 9. tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges...

47
Trees Chapter 9

Upload: clifford-floyd

Post on 11-Jan-2016

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Trees

Chapter 9

Page 2: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Tree

graph connected undirected no simple circuits (acyclic)

no multiple edges no loops

Page 3: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Sample Trees?

Tree Tree Not Not

Page 4: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Theorem 1

An undirected graph is a tree iff A simple path exists in a tree between any two vertices

Page 5: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Root

A particular tree vertex from which we assign a

direction to each edge

Each edge is directed away from the root

Page 6: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Rooted tree

A tree with a designated rootA directed graphDirection of all edges is away from

root

Page 7: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Parent

In a rooted tree, a parent of vertex v is

the unique vertex u such that there is a directed edge from

u to v

Page 8: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Child

The vertex v to which a directed edge exists from parent u in a rooted tree

Page 9: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Siblings

vertices with the same parent

Page 10: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Leaf

a vertex of a tree that has no children

Page 11: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Ancestors of node A

nodes located on the path from A to the root

Page 12: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Descendants of node A

nodes located on the path from A to a leaf node

Page 13: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Internal vertices

Vertices with children

Page 14: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Sub-tree

a tree contained in a larger tree whose root may be a child node in the larger tree

Page 15: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

m-ary tree

a rooted tree with no more than m children per

vertex

Page 16: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Full m-ary tree

a rooted tree whose every internal vertex has exactly m children

Page 17: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Theorem 2

A tree with n vertices has n - 1 edges.

7 vertices

6 edges

Page 18: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Theorem 3

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

m = 2

i = 7

15 vertices

Page 19: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Tree Height

height (level) of a node the length of the path from the root to a

node

height of a tree the length of the longest path in a tree

Page 20: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

The maximum number of nodes at any level is mh

h is height of a node at that level of the tree

21

22

23

Page 21: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

The minimum number of nodes of a tree of height h is h+1

Page 22: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

The maximum number of nodes in a tree of height h is m(h+1) -1

2(3+1) - 1

Page 23: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Balanced tree

A rooted m-ary tree of height h is called balanced if all leaves are at level h or h - 1

NOYES

YES

Page 24: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

If an m-ary tree of height h has l leaves, and the tree is full and balanced,

h = ceil(log m l)

h = ceil (log28)h = 3

What does this imply about access speed if a tree is used as a data structure?

Page 25: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Applications of Trees

8.2

Page 26: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Binary search tree

A binary tree where key value in any node is greater than key of its left child and any of its children

(the nodes in the left subtree)

less than key of its right child and any of its children

(the nodes in the right subtree)http://math.nemcc.edu/bst/

Page 27: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Binary Search Tree Example

Page 28: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Form a BST with the words Mathematics, Physics, Geography, Zoology, Meteorology, Geology, Psychology, Chemistry

Page 29: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

NOTE:Input order determines a tree's shape.

Tree Animation

Page 30: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Tree Traversal

8.3

Page 31: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Inorder Tree Traversal

process Left subtree inorder Visit a node (or process node) Process Right subtree inorder

Processes BST vertices in ascending sequence

http://nova.umuc.edu/~jarc/idsv/lesson1.html

Page 32: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Inorder Traversal Example LVR

Arps, Dietz, Egofske, Fairchild, Garth, Huston, KeithMagillicuddy, Nathan, Perkins, Seliger, Talbot, Underwood,Verkins, Zarda

Page 33: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Preorder Tree Traversal

allows quickest access to the whole tree

VISIT a node process LEFT subtree in preorder process RIGHT subtree in preorder

Page 34: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Preorder Traversal Example VLR

Magillicuddy, Fairchild, Dietz, Arps, Egofske, Huston, Garth, Keith, Talbot, Perkins, Nathan, Selinger, Verkins, Underwood, Zarda

Page 35: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Postorder Tree Traversal

good for deletion of nodes; postfix notation

process LEFT subtree in postorder process RIGHT subtree in postorder VISIT a node

Page 36: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Postorder Traversal Example LRV

Arps, Egofske, Dietz, Garth, Keith, Huston, Fairchild,Nathan, Selinger, Perkinds, Underwood, Zarda, Verkins,Talbot, Magillicuddy

Page 37: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Expression Tree

An ordered rooted tree

associates operands & operators in a uniform way

+

Page 38: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Give Pre, In, Postorder

PreOrder: - + + * 6 2 7 * 8 3 / 6 7

InOrder: 6 * 2 + 7 + 8 * 3 - 6 / 7

PostOrder: 6 2 * 7 + 8 3 * + 6 7 / -

+

Page 39: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Spanning Trees

9.4

Page 40: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Spanning Subgraph

A spanning subgraph of G is G’ = (V, E’) where E’ is a subset of E

Note every vertex of G is included

Page 41: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Spanning Tree

A spanning subgraph that is a tree connected acyclic

See p. 581-2

Page 42: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Depth First Search

A procedure for constructing a spanning tree by adding edges that form a path until this is not possible then moving back up the tree until a vertex is found where a

new path can be formed

http://www.cs.sunysb.edu/~skiena/combinatorica/animations/search.html

Page 43: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Breadth First Search

A procedure for constructing a spanning tree that successively adds all edges incident to the last set of

edges added unless a simple circuit is

formed

http://www.cs.duke.edu/~wcp/DFSanim.html http://152.3.140.5/~wcp/DFSanim.html

Page 44: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Perform DFS, BFS search

DFS: a, b, c, d, e, f, g BFS: a b c g d e f

Page 45: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Perform DFS, BFS search

DFS: a, b, c, d, e, f BFS: a b d e c f

Page 46: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops

Minimum Spanning Tree

A connected weighted graph is a spanning tree that has the smallest possible sum of weights

of its edges.

http://study.haifa.ac.il/~hvaiderm/sem3.html

Page 47: Trees Chapter 9. Tree zgraph yconnected yundirected yno simple circuits (acyclic) yno multiple edges yno loops