# Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

## Presentation on theme: "Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten."— Presentation transcript:

Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten

Game Trees Definition: A Game Tree models the different outcomes possible in a game. Vertices: positions in a game Edges: legal moves from one position to another Leaves: Final positions of a game

Game Trees Example: Tic-Tac-Toe

10.3 Tree Traversal Rooted trees are used to store information. We often need to do some operation on the vertices in such a tree. A tree traversal is an algorithm designed to “visit” each node in the tree. Traversal Algorithms: – Preorder traversal – Inorder traversal – Postorder traversal These types correspond to Prefix/Infix/Postfix notation.

10.3 Tree Traversal Definition: Let T be a rooted tree with root r. If T consists only of r, then r is the preorder traversal of T. Otherwise, let T 1, T 2, …, T n be the subtrees at r from left to right. The preorder traversal of T will begin by visiting r, then T 1 (in preorder), then T 2 (in preorder), etc, until T n is traversed in preorder.

10.3 Tree Traversal

Preorder Traversal = (a,b,e,j,k,n,o,p, f,c,d,g,l,m,h,i)

10.3 Tree Traversal Definition: Let T be a rooted tree with root r. If T consists only of r, then r is the inorder traversal of T. Otherwise, let T 1, T 2, …, T n be the subtrees at r from left to right. The inorder traversal of T will begin by visiting T 1 (inorder), then the root r, then T 2 (inorder), etc, until T n is traversed inorder.

10.3 Tree Traversal

Inorder Traversal = (j,e,n,k,o,p,b,f,a, c,l,g,m,d,h,i)

10.3 Tree Traversal Definition: Let T be a rooted tree with root r. If T consists only of r, then r is the postorder traversal of T. Otherwise, let T 1, T 2, …, T n be the subtrees at r from left to right. The postorder traversal of T will begin by visiting T 1 (in postorder), then T 2 (in postorder), etc, then T n (in postorder) and finally the root r.

10.3 Tree Traversal

Postorder Traversal = (j,n,o,p,k,e,f,b, c,l,m,g,h,i,d,a)

Expression Trees The expression tree for an arithmetic expression consists of – Vertices: numbers, +, -, *, /, ↑ (↑ represents the power function ) – Edges: linking parts of an expression – Internal vertices represent operations – Leaves represent the variables or numbers

Expression Trees Example: We build the expression tree for ((x+y) ↑ 2)+ ((x-4)/3) from the “bottom” up

Expression Trees Infix form of the expression = inorder traversal Get: x+y ↑ 2+x-4/3 Add parentheses for groups: ((x+y) ↑ 2)+((x-4)/3)

Expression Trees Prefix form of the expression = preorder traversal Get: + ↑ + x y 2 / - x 4 3

Expression Trees What is expression with prefix form + - * 2 3 5 / ↑ 2 3 4 ? Answer: ((2*3)-5)+((2 ↑ 3)/4)

Expression Trees Postfix form of the expression = postorder traversal Get: x y + 2 ↑ x 4 – 3 / +

10.4 Spanning Trees Recall: A tree is an undirected connected graph without cycles Definition: A spanning tree of a connected undirected graph G is a subgraph of G that contains all of G’s vertices and enough of its edges to form a tree To obtain a spanning tree from a connected undirected graph with cycles – Remove edges until there are no cycles

10.4 Spanning Trees

There are two algorithms for constructing spanning trees: Depth-First Search (Back-tracking) Breadth-First Search

Depth-First Search Depth-First Search (DFS) proceeds along a path from a vertex v as deeply into the graph as possible before backing up (back-tracking) To create a depth-first search (DFS) spanning tree – Traverse the graph using a depth-first search and mark the edges that you follow – After the traversal is complete, the graph’s vertices and marked edges form the spanning tree

Depth-First Search Start at f:

Breadth-First Search Breadth-First Search (BFS) visits every vertex adjacent to a vertex v that it can before visiting any other vertex To create a breath-first search (BFS) spanning tree – Traverse the graph using a bread-first search and mark the edges that you follow – When the traversal is complete, the graph’s vertices and marked edges form the spanning tree

Minimum Spanning Trees Definition: A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible sum of weights of edges. – There may be several minimum spanning trees for a particular graph

Prim’s Algorithm Prim’s Algorithm finds a minimal spanning tree that begins at any vertex – Find the least-cost edge (v, u) from a visited vertex v to some unvisited vertex u – Mark u as visited – Add the vertex u and the edge (v, u) to the minimum spanning tree – Repeat the above steps until there are no more unvisited vertices

Prim’s Algorithm Prim’s Algorithm finds a minimal spanning tree that begins at any vertex – Find the least-cost edge (v, u) from a visited vertex v to some unvisited vertex u – Mark u as visited – Add the vertex u and the edge (v, u) to the minimum spanning tree – Repeat the above steps until there are no more unvisited vertices

Prim’s Algorithm

Kruskal’s Algorithm Kruskal’s Algorithm finds a minimal spanning tree that begins at any vertex – Choose a least-cost edge (v, u) as the root – Successively add edges with minimum weight that do not from a circuit with already chosen edges. – Stop after n-1 edges are chosen.

Kruskal’s Algorithm

Ass 14 P723 17(b)(c)(d) Use breadth-first and depth-first search to produce a spanning tree for the simple graphs in Exercises 13 of page 735. Choose e as the root of each spanning tree.( Using the alphabet order) Bonus: P723 23(a)(c)

Download ppt "Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten."

Similar presentations