1 / 47

Trees

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.

turi
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 Chapter 9

  2. Tree • graph • connected • undirected • no simple circuits (acyclic) • no multiple edges • no loops

  3. Sample Trees? Tree Tree Not Not

  4. Theorem 1 • An undirected graph is a tree iff • A simple path exists in a tree • between any two vertices

  5. Root • A particular tree vertex • from which we assign a direction to each edge • Each edge is directed away from the root

  6. Rooted tree • A tree with a designated root • A directed graph • Direction of all edges is away from root

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

  8. Child • The vertex v • to which a directed edge exists • from parent u in a rooted tree

  9. Siblings • vertices with the same parent

  10. Leaf • a vertex of a tree that has no children

  11. Ancestors of node A • nodes located on the path • from A to the root

  12. Descendants of node A • nodes located on the path • from A to a leaf node

  13. Internal vertices • Vertices with children

  14. Sub-tree • a tree • contained in a larger tree • whose root may be a child node • in the larger tree

  15. m-ary tree • a rooted tree • with no more than m children per vertex

  16. Full m-ary tree • a rooted tree • whose every internal vertex • has exactly m children

  17. Theorem 2 • A tree with n vertices has n - 1 edges. 7 vertices 6 edges

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

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

  20. The maximum number of nodes at any level is mh • h is height of a node at that level of the tree 212223

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

  22. The maximum number of nodes • in a tree of height h is • m(h+1) -1 • 2(3+1) - 1

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

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

  25. Applications of Trees 8.2

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

  27. Binary Search Tree Example

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

  29. NOTE:Input order determines a tree's shape.Tree Animation

  30. Tree Traversal 8.3

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

  32. Inorder Traversal Example LVR Arps, Dietz, Egofske, Fairchild, Garth, Huston, Keith Magillicuddy, Nathan, Perkins, Seliger, Talbot, Underwood,Verkins, Zarda

  33. Preorder Tree Traversal • allows quickest access to the whole tree • VISIT a node • process LEFT subtree in preorder • process RIGHT subtree in preorder

  34. Preorder Traversal Example VLR Magillicuddy, Fairchild, Dietz, Arps, Egofske, Huston, Garth, Keith, Talbot, Perkins, Nathan, Selinger, Verkins, Underwood, Zarda

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

  36. Postorder Traversal Example LRV Arps, Egofske, Dietz, Garth, Keith, Huston, Fairchild, Nathan, Selinger, Perkinds, Underwood, Zarda, Verkins, Talbot, Magillicuddy

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

  38. 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 / - +

  39. Spanning Trees 9.4

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

  41. Spanning Tree • A spanning subgraph that is a tree • connected • acyclic • See p. 581-2

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

  43. 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 isformed • http://www.cs.duke.edu/~wcp/DFSanim.html • http://152.3.140.5/~wcp/DFSanim.html

  44. Perform DFS, BFS search DFS: a, b, c, d, e, f, g BFS: a b c g d e f

  45. Perform DFS, BFS search DFS: a, b, c, d, e, f BFS: a b d e c f

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

More Related