110 likes | 204 Views
Chap 9 Trees. Def 1: A tree is a connected,undirected, graph with no simple circuits . Ex1. Theorem1: An undirected graph is a tree if and only if there is a unique simple path between any two of its vertices. Chap 9 Trees.
E N D
Chap 9 Trees • Def 1: A tree is a connected,undirected, graph with no simple circuits . Ex1. • Theorem1: An undirected graph is a tree if and only if there is a unique simple path between any two of its vertices.
Chap 9 Trees • Root , rooted tree, parent , child , siblings , ancestors , descendants,leaf ,subtree,internal vertices:have children • Ex2 • Def 2:m-ary tree : every internal vertex has no more than m children;full m-ary tree ; binary tree.
Chap 9 Trees • Trees as models • Properties of trees • Theorem2: A tree with n vertices has n-1 edges. • Theorem3: A full m-ary tree with i internal vertices contains n=mi+1 vertices.
Chap 9 Trees • Level of a vertex :length of the unique path from the root to the vertex • height of a rooted tree:length of the longest path from the root • Ex10 a rooted m-ary tree of height h is balance if all leaves are at levels h or h-1
Chap 9 Trees • Tree traversal ordered rooted tree ; left/right child/subtree • Def1: preorder traversal of an ordered rooted tree Fig 2; Ex2 Def 2: inorder traversal Fig 5, Ex3 Def 3: postorder traversal Fig 7, Ex4
Chap 9 Trees • Fig 9 : preorder : list each vertex the first time this curve passes it . inorder : list a leaf the first time the curve passes it ; list each internal vertex the second time the curve passes it postorder :list a vertex the last time it is passed on the way back up to its parent.
Chap 9 Trees • Represent complicated expressions using ordered rooted trees • Ex5 inorder traversal produces the original expression with the elements and operations in the same order as they originally occurred . infix form ( Fig 11) : need to include parentheses whenever an operation is encountered in the inorder traversal
Chap 9 Trees • Prefix form : no parenthesis are needed (Polish notation) Ex6 We can evaluate an expression in prefix form by working from right to left Ex7 Postfix form (reverse Polish notation) : no parenthesis are needed
Chap 9 Trees • Ex8 evaluate an expression from left to right Ex9 Ex10 Because prefix and postfix expressions are unambiguous and can easily be evaluated , they are extensively used in computers science.
Chap 9 Trees • Spanning Tree Def1 : let G be a simple graph .A spanning tree of G is a subgraph of G that is a tree containing every vertex of G (n vertices with n-1 edges) Ex1. Algorithm for constructing spanning tree depth-first search /backtracking Ex3. breadth-first search Ex4.
Chap 9 Trees Minimum Spanning Tree • Def 1 : A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible sum of weights of its edges Prim’s algorithm Ex2. Kruskal’s algorithm Ex3.