Trees

1 / 47

# Trees - PowerPoint PPT Presentation

Trees. Chapter 9. Tree. graph connected undirected no simple circuits (acyclic) no multiple edges no loops. Sample Trees?. Tree Tree Not Not. Theorem 1. An undirected graph is a tree iff A simple path exists in a tree between any two vertices. Root.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## Trees

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

### Trees

Chapter 9

Tree
• graph
• connected
• undirected
• no simple circuits (acyclic)
• no multiple edges
• no loops
Sample Trees?

Tree Tree Not Not

Theorem 1
• An undirected graph is a tree iff
• A simple path exists in a tree
• between any two vertices
Root
• A particular tree vertex
• from which we assign a direction to each edge
• Each edge is directed away from the root
Rooted tree
• A tree with a designated root
• A directed graph
• Direction of all edges is away from root
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
Child
• The vertex v
• to which a directed edge exists
• from parent u in a rooted tree
Siblings
• vertices with the same parent
Leaf
• a vertex of a tree that has no children
Ancestors of node A
• nodes located on the path
• from A to the root
Descendants of node A
• nodes located on the path
• from A to a leaf node
Internal vertices
• Vertices with children
Sub-tree
• a tree
• contained in a larger tree
• whose root may be a child node
• in the larger tree
m-ary tree
• a rooted tree
• with no more than m children per vertex
Full m-ary tree
• a rooted tree
• whose every internal vertex
• has exactly m children
Theorem 2
• A tree with n vertices has n - 1 edges.

7 vertices

6 edges

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

m = 2

i = 7

15 vertices

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
The maximum number of nodes at any level is mh
• h is height of a node at that level of the tree

212223

The minimum number of nodes
• of a tree of height h is
• h+1
The maximum number of nodes
• in a tree of height h is
• m(h+1) -1
• 2(3+1) - 1
Balanced tree
• A rooted m-ary tree of height h
• is called balanced
• if all leaves are at level h or h - 1

YES

NO

YES

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?

### Applications of Trees

8.2

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/
Form a BST with the words Mathematics, Physics, Geography, Zoology, Meteorology, Geology, Psychology, Chemistry

### Tree Traversal

8.3

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
Inorder Traversal Example LVR

Arps, Dietz, Egofske, Fairchild, Garth, Huston, Keith

Magillicuddy, Nathan, Perkins, Seliger, Talbot,

Underwood,Verkins, Zarda

Preorder Tree Traversal
• VISIT a node
• process LEFT subtree in preorder
• process RIGHT subtree in preorder
Preorder Traversal Example VLR

Magillicuddy, Fairchild, Dietz, Arps, Egofske, Huston,

Garth, Keith, Talbot, Perkins, Nathan, Selinger, Verkins,

Underwood, Zarda

Postorder Tree Traversal
• good for deletion of nodes; postfix notation
• process LEFT subtree in postorder
• process RIGHT subtree in postorder
• VISIT a node
Postorder Traversal Example LRV

Arps, Egofske, Dietz, Garth, Keith, Huston, Fairchild,

Nathan, Selinger, Perkinds, Underwood, Zarda, Verkins,

Talbot, Magillicuddy

Expression Tree
• An ordered rooted tree
• associates operands & operators in a uniform way

+

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

+

### Spanning Trees

9.4

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
Spanning Tree
• A spanning subgraph that is a tree
• connected
• acyclic
• See p. 581-2
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
• A procedure for constructing a spanning tree
• that successively adds all edges incident to the last set of edges added
• unless a simple circuit isformed
• http://www.cs.duke.edu/~wcp/DFSanim.html
• http://152.3.140.5/~wcp/DFSanim.html
Perform DFS, BFS search

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

Perform DFS, BFS search

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

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