LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.

Slides:



Advertisements
Similar presentations
Chapter 10, Section 10.3 Tree Traversal
Advertisements

Trees Chapter 11.
Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
Today’s topics Decision Trees Reading: Sections 9.1 CompSci 102.
22C:19 Discrete Structures Trees Spring 2014 Sukumar Ghosh.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
1 Tree Traversal Section 9.3 Longin Jan Latecki Temple University Based on slides by Paul Tymann, Andrew Watkins, and J. van Helden.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
1 Copyright M.R.K. Krishna Rao 2003 Ch 9 - Trees Definition: A tree is a connected undirected graph with no simple circuits. Since a tree cannot have a.
1 Section 9.1 Introduction to Trees. 2 Tree terminology Tree: a connected, undirected graph that contains no simple circuits –must be a simple graph:
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Discrete Mathematics Transparency No. 8-1 Chapter 8 Trees.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
4/17/2017 Section 9.3 Tree Traversal ch9.3.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank and Modified By Mingwu Chen Trees.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
KNURE, Software department, Ph , N.V. Bilous Faculty of computer sciences Software department, KNURE The trees.
Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University.
Chapter Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum.
May 5, 2015Applied Discrete Mathematics Week 13: Boolean Algebra 1 Dijkstra’s Algorithm procedure Dijkstra(G: weighted connected simple graph with vertices.
ساختمانهای گسسته دانشگاه صنعتی شاهرود – اردیبهشت 1392.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete Mathematics and Its Applications, 6 th ed., by Kenneth.
Tree A connected graph that contains no simple circuits is called a tree. Because a tree cannot have a simple circuit, a tree cannot contain multiple.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures.
Discrete Structures Lecture 12: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
Trees – Chapter 9 Slides courtesy of Dr. Michael P. Frank University of Florida Dept. of Computer & Information Science & Engineering.
17.1 CompSci 102 Today’s topics Instant InsanityInstant Insanity TreesTrees –Properties –Applications Reading: Sections Reading: Sections
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 1/18 Module.
Discrete Structures Trees (Ch. 11)
Chap 8 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.
5.5.2 M inimum spanning trees  Definition 24: A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible.
Trees 2: Section 4.2 and 4.3 Binary trees. Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
Discrete Mathematics Chapter 5 Trees.
CS 103 Discrete Structures Lecture 23 Trees (1). Second Midterm Exam 1 st Lecture in December (same time as the lecture) 75 minute duration Will cover.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
Discrete Mathematics Chapter 10 Trees. Outline 10.1 Introduction to Trees 10.2 Applications of Trees 10.3 Tree Traversal 10.4 Spanning Trees 10.5 Minimal.
Discrete Mathematics Chapter 10 Trees.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Chapter 11. Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum Spanning.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Section10.1: Introduction to Trees
Discrete Mathematics Trees.
Applied Discrete Mathematics Week 15: Trees
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Chapter 5 : Trees.
Trees Chapter 11.
Introduction to Trees Section 11.1.
Binary Trees, Binary Search Trees
Taibah University College of Computer Science & Engineering Course Title: Discrete Mathematics Code: CS 103 Chapter 10 Trees Slides are adopted from “Discrete.
Trees.
Section 9.3 by Andrew Watkins
Binary Trees, Binary Search Trees
Trees.
And the Final Subject is…
Trees Chapter 11.
Binary Trees, Binary Search Trees
Presentation transcript:

LOGO

 Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2

LOGO 3

4 Introduction to Trees  A tree is a connected undirected graph that contains no circuits.  Theorem: There is a unique simple path between any two of its nodes.

LOGO 5 Introduction to Trees  A tree is a connected undirected graph that contains no circuits.  Theorem: There is a unique simple path between any two of its nodes.  A (not-necessarily-connected) undirected graph without simple circuits is called a forest.  You can think of it as a set of trees having disjoint sets of nodes.

LOGO 6 Introduction to Trees  A tree is a connected undirected graph that contains no circuits.  Theorem: There is a unique simple path between any two of its nodes.  A (not-necessarily-connected) undirected graph without simple circuits is called a forest.  You can think of it as a set of trees having disjoint sets of nodes.  A leaf node in a tree or forest is any pendant or isolated vertex. An internal node is any non-leaf vertex (thus it has degree ≥ ? ).

LOGO 7 Introduction to Trees  A tree is a connected undirected graph that contains no circuits.  Theorem: There is a unique simple path between any two of its nodes.  A (not-necessarily-connected) undirected graph without simple circuits is called a forest.  You can think of it as a set of trees having disjoint sets of nodes.  A leaf node in a tree or forest is any pendant or isolated vertex. An internal node is any non-leaf vertex (thus it has degree ≥ 2 ).

LOGO 8 Tree and Forest Examples  A Tree:  A Forest: Leaves in green, internal nodes in brown.

LOGO 9 Rooted Trees  A rooted tree is a tree in which one node has been designated the root.  Every edge is (implicitly or explicitly) directed away from the root.

LOGO 10 Rooted Trees  A rooted tree is a tree in which one node has been designated the root.  Every edge is (implicitly or explicitly) directed away from the root.  Concepts related to rooted trees:  Parent, child, siblings, ancestors, descendents, leaf, internal node, subtree.

LOGO 11 Rooted Tree Examples  Note that a given unrooted tree with n nodes yields n different rooted trees. root Same tree except for choice of root

LOGO 12 Rooted-Tree Terminology Exercise  Find the parent, children, siblings, ancestors, & descendants of node f. a b c d e f g h i jk l m n o p q r root

LOGO 13 n-ary trees  A rooted tree is called n-ary if every vertex has no more than n children.  It is called full if every internal (non-leaf) vertex has exactly n children.

LOGO 14 n-ary trees  A rooted tree is called n-ary if every vertex has no more than n children.  It is called full if every internal (non-leaf) vertex has exactly n children.  A 2-ary tree is called a binary tree.  These are handy for describing sequences of yes- no decisions. Example: Comparisons in binary search algorithm.

LOGO 15 Which Tree is Binary?  Theorem: A given rooted tree is a binary tree iff every node other than the root has degree ≤ ___, and the root has degree ≤ ___.

LOGO 16 Ordered Rooted Tree  This is just a rooted tree in which the children of each internal node are ordered.

LOGO 17 Ordered Rooted Tree  This is just a rooted tree in which the children of each internal node are ordered.  In ordered binary trees, we can define:  left child, right child  left subtree, right subtree

LOGO 18 Ordered Rooted Tree  This is just a rooted tree in which the children of each internal node are ordered.  In ordered binary trees, we can define:  left child, right child  left subtree, right subtree  For n-ary trees with n>2, can use terms like “leftmost”, “rightmost,” etc.

LOGO 19 Trees as Models  Can use trees to model the following:  Saturated hydrocarbons  Organizational structures  Computer file systems  In each case, would you use a rooted or a non- rooted tree?

LOGO 20 Some Tree Theorems  Any tree with n nodes has e = n−1 edges.  Proof: Consider removing leaves.

LOGO 21 Some Tree Theorems  Any tree with n nodes has e = n−1 edges.  Proof: Consider removing leaves.  A full m-ary tree with i internal nodes has n=mi+1 nodes, and =(m−1)i+1 leaves.  Proof: There are mi children of internal nodes, plus the root. And, = n−i = (m−1)i+1. □

LOGO 22 Some Tree Theorems  Any tree with n nodes has e = n−1 edges.  Proof: Consider removing leaves.  A full m-ary tree with i internal nodes has n=mi+1 nodes, and =(m−1)i+1 leaves.  Proof: There are mi children of internal nodes, plus the root. And, = n−i = (m−1)i+1. □  Thus, when m is known and the tree is full, we can compute all four of the values e, i, n, and, given any one of them.

LOGO 23 Some More Tree Theorems  Definition: The level of a node is the length of the simple path from the root to the node.  The height of a tree is maximum node level.  A rooted m-ary tree with height h is called balanced if all leaves are at levels h or h−1.

LOGO 24 Some More Tree Theorems  Definition: The level of a node is the length of the simple path from the root to the node.  The height of a tree is maximum node level.  A rooted m-ary tree with height h is called balanced if all leaves are at levels h or h−1.  Theorem: There are at most m h leaves in an m-ary tree of height h.  Corollary: An m-ary tree with leaves has height h≥  log m . If m is full and balanced then h=  log m .

LOGO 25

LOGO 26 Applications of Trees  Binary search trees  A simple data structure for sorted lists

LOGO 27 Applications of Trees  Binary search trees  A simple data structure for sorted lists  Decision trees  Minimum comparisons in sorting algorithms

LOGO 28 Applications of Trees  Binary search trees  A simple data structure for sorted lists  Decision trees  Minimum comparisons in sorting algorithms  Prefix codes  Huffman coding

LOGO 29 Applications of Trees  Binary search trees  A simple data structure for sorted lists  Decision trees  Minimum comparisons in sorting algorithms  Prefix codes  Huffman coding  Game trees

LOGO 30 Binary Search Trees  A representation for sorted sets of items.  Supports the following operations in Θ(log n) average-case time: Searching for an existing item. Inserting a new item, if not already present.  Supports printing out all items in Θ(n) time.  Note that inserting into a plain sequence a i would instead take Θ(n) worst- case time.

LOGO 31 Binary Search Tree Format  Items are stored at individual tree nodes.  We arrange for the tree to always obey this invariant:  For every item x, Every node in x’s left subtree is less than x. Every node in x’s right subtree is greater than x Example:

LOGO 32 Recursive Binary Tree Insert procedure insert(T: binary tree, x: item) v := root[T] if v = null then begin root[T] := x; return “Done” end else if v = x return “Already present” else if x v} return insert(rightSubtree[T], x)

LOGO 33 Decision Trees  A decision tree represents a decision-making process.  Each possible “decision point” or situation is represented by a node.  Each possible choice that could be made at that decision point is represented by an edge to a child node.

LOGO 34 Decision Trees  A decision tree represents a decision-making process.  Each possible “decision point” or situation is represented by a node.  Each possible choice that could be made at that decision point is represented by an edge to a child node.  In the extended decision trees used in decision analysis, we also include nodes that represent random events and their outcomes.

LOGO 35 Coin-Weighing Problem  Imagine you have 8 coins, one of which is a lighter counterfeit, and a free-beam balance.  No scale of weight markings is required for this problem!  How many weighings are needed to guarantee that the counterfeit coin will be found? ?

LOGO 36 As a Decision-Tree Problem  In each situation, we pick two disjoint and equal- size subsets of coins to put on the scale. The balance then “decides” whether to tip left, tip right, or stay balanced. A given sequence of weighings thus yields a decision tree with branching factor 3.

LOGO 37 Applying the Tree Height Theorem  The decision tree must have at least 8 leaf nodes, since there are 8 possible outcomes.  In terms of which coin is the counterfeit one.  Recall the tree-height theorem, h≥  log m .  Thus the decision tree must have height h ≥  log 3 8  =  1.893…  = 2.  Let’s see if we solve the problem with only 2 weighings…

LOGO 38 General Solution Strategy  The problem is an example of searching for 1 unique particular item, from among a list of n otherwise identical items.  Somewhat analogous to the adage of “searching for a needle in haystack.”  Armed with our balance, we can attack the problem using a divide- and-conquer strategy, like what’s done in binary search.  We want to narrow down the set of possible locations where the desired item (coin) could be found down from n to just 1, in a logarithmic fashion.  Each weighing has 3 possible outcomes.  Thus, we should use it to partition the search space into 3 pieces that are as close to equal-sized as possible.  This strategy will lead to the minimum possible worst-case number of weighings required.

LOGO 39 General Balance Strategy  On each step, put  n/3  of the n coins to be searched on each side of the scale.  If the scale tips to the left, then: The lightweight fake is in the right set of  n/3  ≈ n/3 coins.  If the scale tips to the right, then: The lightweight fake is in the left set of  n/3  ≈ n/3 coins.  If the scale stays balanced, then: The fake is in the remaining set of n − 2  n/3  ≈ n/3 coins that were not weighed!  Except if n mod 3 = 1 then we can do a little better by weighing  n/3  of the coins on each side. You can prove that this strategy always leads to a balanced 3-ary tree.

LOGO 40 Coin Balancing Decision Tree  Here’s what the tree looks like in our case: 123 vs vs. 2 left: 123 balanced: 78 right: vs. 8 4 vs. 5 L:1 R:2B:3 L:4 R:5B:6 L:7 R:8

LOGO 41

LOGO Universal Address Systems To totally order the vertices of on ordered rooted tree: 1.Label the root with the integer 0. 2.Label its k children (at level 1) from left to right with 1, 2, 3, …, k. 3.For each vertex v at level n with label A, label its k v children, from left to right, with A.1, A.2, A.3, …, A.k v. 42

LOGO Universal Address Systems To totally order the vertices of on ordered rooted tree: 1.Label the root with the integer 0. 2.Label its k children (at level 1) from left to right with 1, 2, 3, …, k. 3.For each vertex v at level n with label A, label its k v children, from left to right, with A.1, A.2, A.3, …, A.k v. This labeling is called the universal address system of the ordered rooted tree. 43

LOGO Universal Address Systems 44

LOGO Traversal Algorithms  A traversal algorithm is a procedure for systematically visiting every vertex of an ordered rooted tree  An ordered rooted tree is a rooted tree where the children of each internal vertex are ordered 45

LOGO Traversal Algorithms  A traversal algorithm is a procedure for systematically visiting every vertex of an ordered rooted tree  An ordered rooted tree is a rooted tree where the children of each internal vertex are ordered  The three common traversals are:  Preorder traversal  Inorder traversal  Postorder traversal 46

LOGO Traversal  Let T be an ordered rooted tree with root r.  Suppose T 1, T 2, …,T n are the subtrees at r from left to right in T. r T1T1 T2T2 TnTn 47

LOGO Preorder Traversal r T1T1 T2T2 TnTn Step 1: Visit r Step 2: Visit T 1 in preorder Step 3: Visit T 2 in preorder. Step n+1: Visit T n in preorder 48

LOGO Preorder Traversal r T1T1 T2T2 TnTn Step 1: Visit r Step 2: Visit T 1 in preorder Step 3: Visit T 2 in preorder. Step n+1: Visit T n in preorder 49

LOGO Preorder Traversal r T1T1 T2T2 TnTn Step 1: Visit r Step 2: Visit T 1 in preorder Step 3: Visit T 2 in preorder. Step n+1: Visit T n in preorder 50

LOGO Preorder Traversal r T1T1 T2T2 TnTn Step 1: Visit r Step 2: Visit T 1 in preorder Step 3: Visit T 2 in preorder. Step n+1: Visit T n in preorder 51

LOGO Preorder Traversal r T1T1 T2T2 TnTn Step 1: Visit r Step 2: Visit T 1 in preorder Step 3: Visit T 2 in preorder. Step n+1: Visit T n in preorder 52

LOGO Example M A R EY P M HJ QT Tree: Visiting sequence: 53

LOGO Example AM A R EY P M HJ QT Tree: Visiting sequence: 54

LOGO Example AMJ A R EY P M HJ QT Tree: Visiting sequence: 55

LOGO Example AYMJ A R EY P M HJ QT Tree: Visiting sequence: 56

LOGO Example ARYMJ A R EY P M HJ QT Tree: Visiting sequence: 57

LOGO Example ARYMHJ A R EY P M HJ QT Tree: Visiting sequence: 58

LOGO Example ARYPMHJ A R EY P M HJ QT Tree: Visiting sequence: 59

LOGO Example ARYPMHJQ A R EY P M HJ QT Tree: Visiting sequence: 60

LOGO Example ARYPMHJQT A R EY P M HJ QT Tree: Visiting sequence: 61

LOGO Example AREYPMHJQT A R EY P M HJ QT Tree: Visiting sequence: 62

LOGO The Preorder Traversal of T In which order does a preorder traversal visit the vertices in the ordered rooted tree T shown to the left? Remember: Visit root, then visit subtrees left to right. 63

LOGO The Preorder Traversal of T 64

LOGO The Preorder Traversal of T 65

LOGO The Preorder Traversal of T 66

LOGO The Preorder Traversal of T 67

LOGO Inorder Traversal Step 1: Visit T 1 in inorder Step 2: Visit r Step 3: Visit T 2 in inorder. Step n+1: Visit T n in inorder r T1T1 T2T2 TnTn 68

LOGO Inorder Traversal Step 1: Visit T 1 in inorder Step 2: Visit r Step 3: Visit T 2 in inorder. Step n+1: Visit T n in inorder r T1T1 T2T2 TnTn 69

LOGO Inorder Traversal Step 1: Visit T 1 in inorder Step 2: Visit r Step 3: Visit T 2 in inorder. Step n+1: Visit T n in inorder r T1T1 T2T2 TnTn 70

LOGO Inorder Traversal Step 1: Visit T 1 in inorder Step 2: Visit r Step 3: Visit T 2 in inorder. Step n+1: Visit T n in inorder r T1T1 T2T2 TnTn 71

LOGO Inorder Traversal Step 1: Visit T 1 in inorder Step 2: Visit r Step 3: Visit T 2 in inorder. Step n+1: Visit T n in inorder r T1T1 T2T2 TnTn 72

LOGO Example J A R EY P M HJ QT Tree: Visiting sequence: 73

LOGO Example AJ A R EY P M HJ QT Tree: Visiting sequence: 74

LOGO Example AMJ A R EY P M HJ QT Tree: Visiting sequence: 75

LOGO Example ARMJ A R EY P M HJ QT Tree: Visiting sequence: 76

LOGO Example ARYMJ A R EY P M HJ QT Tree: Visiting sequence: 77

LOGO Example ARYPMJ A R EY P M HJ QT Tree: Visiting sequence: 78

LOGO Example ARYPMHJ A R EY P M HJ QT Tree: Visiting sequence: 79

LOGO Example ARYPMHJQ A R EY P M HJ QT Tree: Visiting sequence: 80

LOGO Example ARYPMHJQT A R EY P M HJ QT Tree: Visiting sequence: 81

LOGO Example AREYPMHJQT A R EY P M HJ QT Tree: Visiting sequence: 82

LOGO The Inorder Traversal of T In which order does an inorder traversal visit the vertices in the ordered rooted tree T shown to the left? Remember: Visit leftmost tree, visit root, visit other subtrees left to right. 83

LOGO The Inorder Traversal of T 84

LOGO The Inorder Traversal of T 85

LOGO The Inorder Traversal of T 86

LOGO The Inorder Traversal of T 87

LOGO Postorder Traversal Step 1: Visit T 1 in postorder Step 2: Visit T 2 in postorder. Step n: Visit T n in postorder Step n+1: Visit r r T1T1 T2T2 TnTn 88

LOGO Postorder Traversal Step 1: Visit T 1 in postorder Step 2: Visit T 2 in postorder. Step n: Visit T n in postorder Step n+1: Visit r r T1T1 T2T2 TnTn 89

LOGO Postorder Traversal Step 1: Visit T 1 in postorder Step 2: Visit T 2 in postorder. Step n: Visit T n in postorder Step n+1: Visit r r T1T1 T2T2 TnTn 90

LOGO Postorder Traversal Step 1: Visit T 1 in postorder Step 2: Visit T 2 in postorder. Step n: Visit T n in postorder Step n+1: Visit r r T1T1 T2T2 TnTn 91

LOGO Postorder Traversal Step 1: Visit T 1 in postorder Step 2: Visit T 2 in postorder. Step n: Visit T n in postorder Step n+1: Visit r r T1T1 T2T2 TnTn 92

LOGO Example J A R EY P M HJ QT Tree: Visiting sequence: 93

LOGO Example AJ A R EY P M HJ QT Tree: Visiting sequence: 94

LOGO Example ARJ A R EY P M HJ QT Tree: Visiting sequence: 95

LOGO Example ARPJ A R EY P M HJ QT Tree: Visiting sequence: 96

LOGO Example ARPJQ A R EY P M HJ QT Tree: Visiting sequence: 97

LOGO Example ARPJQT A R EY P M HJ QT Tree: Visiting sequence: 98

LOGO Example ARPHJQT A R EY P M HJ QT Tree: Visiting sequence: 99

LOGO Example ARYPHJQT A R EY P M HJ QT Tree: Visiting sequence: 100

LOGO Example AREYPHJQT A R EY P M HJ QT Tree: Visiting sequence: 101

LOGO Example AREYPMHJQT A R EY P M HJ QT Tree: Visiting sequence: 102

LOGO The Postorder Traversal of T In which order does a postorder traversal visit the vertices in the ordered rooted tree T shown to the left? Remember: Visit subtrees left to right, then visit root. 103

LOGO The Postorder Traversal of T 104

LOGO The Postorder Traversal of T 105

LOGO The Postorder Traversal of T 106

LOGO The Postorder Traversal of T 107

LOGO Representing Arithmetic Expressions  Complicated arithmetic expressions can be represented by an ordered rooted tree  Internal vertices represent operators  Leaves represent operands 108

LOGO Representing Arithmetic Expressions  Complicated arithmetic expressions can be represented by an ordered rooted tree  Internal vertices represent operators  Leaves represent operands  Build the tree bottom-up  Construct smaller subtrees  Incorporate the smaller subtrees as part of larger subtrees 109

LOGO Example (x+y) 2 + (x-3)/(y+2) 110

LOGO Example (x+y) 2 + (x-3)/(y+2) + x y 111

LOGO Example (x+y) 2 + (x-3)/(y+2) + x y 2  112

LOGO Example (x+y) 2 + (x-3)/(y+2) + x y 2  – x 3 113

LOGO Example (x+y) 2 + (x-3)/(y+2) + x y 2  – x 3 + y 2 114

LOGO Example (x+y) 2 + (x-3)/(y+2) + x y 2  – x 3 + y 2 / 115

LOGO Example (x+y) 2 + (x-3)/(y+2) + x y 2  – x 3 + y 2 / + 116

LOGO Evaluating Prefix Notation  In an prefix expression, a binary operator precedes its two operands  The expression is evaluated right-left  Look for the first operator from the right  Evaluate the operator with the two operands immediately to its right 117

LOGO Example + / / –

LOGO Example + / / –

LOGO Example + / / – / / –

LOGO Example + / / – / / –

LOGO Example + / / – / / – / /

LOGO Example + / / – / / – / /

LOGO Example + / / – / / – / / /

LOGO Example + / / – / / – / / /

LOGO Example + / / – / / – / / / /

LOGO Example + / / – / / – / / / /

LOGO Example + / / – / / – / / / /

LOGO Example + / / – / / – / / / /

LOGO Example + / / – / / – / / / /

LOGO Postfix Notation (Reverse Polish) Traverse in postorder +  – + / + 2 x y x 3 y 2 131

LOGO Postfix Notation (Reverse Polish) Traverse in postorder x +  – + / + 2 x y x 3 y 2 132

LOGO Postfix Notation (Reverse Polish) Traverse in postorder x y +  – + / + 2 x y x 3 y 2 133

LOGO Postfix Notation (Reverse Polish) Traverse in postorder x + y +  – + / + 2 x y x 3 y 2 134

LOGO Postfix Notation (Reverse Polish) Traverse in postorder x + y 2 +  – + / + 2 x y x 3 y 2 135

LOGO Postfix Notation (Reverse Polish) Traverse in postorder x + y  2 +  – + / + 2 x y x 3 y 2 136

LOGO Postfix Notation (Reverse Polish) Traverse in postorder x + y  2 x +  – + / + 2 x y x 3 y 2 137

LOGO Postfix Notation (Reverse Polish) Traverse in postorder x + y  2 x 3 +  – + / + 2 x y x 3 y 2 138

LOGO Postfix Notation (Reverse Polish) Traverse in postorder x + y  2 x – 3 +  – + / + 2 x y x 3 y 2 139

LOGO Postfix Notation (Reverse Polish) Traverse in postorder x + y  2 x – 3 y +  – + / + 2 x y x 3 y 2 140

LOGO Postfix Notation (Reverse Polish) Traverse in postorder x + y  2 x – 3 y 2 +  – + / + 2 x y x 3 y 2 141

LOGO Postfix Notation (Reverse Polish) Traverse in postorder x + y  2 x – 3 y  – + / + 2 x y x 3 y 2 142

LOGO Postfix Notation (Reverse Polish) Traverse in postorder x + y  2 x – 3 / y  – + / + 2 x y x 3 y 2 143

LOGO Postfix Notation (Reverse Polish) Traverse in postorder x + y  2 + x – 3 / y  – + / + 2 x y x 3 y 2 144

LOGO  In an postfix expression, a binary operator follows its two operands  The expression is evaluated left-right  Look for the first operator from the left  Evaluate the operator with the two operands immediately to its left Evaluating Postfix Notation 145

LOGO Example / 3 2 – / + 146

LOGO Example / 3 2 – / + 147

LOGO Example / 3 2 – / / 3 2 – / + 148

LOGO Example / 3 2 – / / 3 2 – / + 149

LOGO Example / 3 2 – / / 3 2 – / – / + 150

LOGO Example / 3 2 – / / 3 2 – / – / + 151

LOGO Example / 3 2 – / / 3 2 – / – / / + 152

LOGO Example / 3 2 – / / 3 2 – / – / / + 153

LOGO Example / 3 2 – / / 3 2 – / – / / / + 154

LOGO Example / 3 2 – / / 3 2 – / – / / / + 155

LOGO Example / 3 2 – / / 3 2 – / – / / /

LOGO Example / 3 2 – / / 3 2 – / – / / /

LOGO Example / 3 2 – / / 3 2 – / – / / /

LOGO 159 Spanning Trees  You will learn about the following topics in tree later in other courses:  Spanning Trees  Minimum Spanning Trees

LOGO Good Luck