Section 9.3 by Andrew Watkins

Slides:



Advertisements
Similar presentations
Chapter 10, Section 10.3 Tree Traversal
Advertisements

Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
Binary Tree Traversals
1 Tree Traversal Section 9.3 Longin Jan Latecki Temple University Based on slides by Paul Tymann, Andrew Watkins, and J. van Helden.
Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
CS 171: Introduction to Computer Science II
Chapter 4: Trees General Tree Concepts Binary Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
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,
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Binary Tree Properties & Representation. Minimum Number Of Nodes Minimum number of nodes in a binary tree whose height is h. At least one node at each.
Transforming Infix to Postfix
Binary Tree Traversal Methods In a traversal of a binary tree, each element of the binary tree is visited exactly once. During the visit of an element,
1 CS308 Data Structures An application of binary trees: Binary Expression Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
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.
Binary Trees. Node structure Data A data field and two pointers, left and right.
Chapter Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum.
Computer Science 112 Fundamentals of Programming II Expression Trees.
10.3 Tree Transversal. Pre/post fix notation and order See handout. a.bc.d e f g h i j k.
(c) University of Washington20d-1 CSC 143 Java Applications of Trees.
ساختمانهای گسسته دانشگاه صنعتی شاهرود – اردیبهشت 1392.
12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA.
BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.
1 Trees 2 Binary trees Section Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children –Left and.
Data Structures : Project 5 Data Structures Project 5 – Expression Trees and Code Generation.
Searching Trees – Page 1CSCI 1900 – Discrete Structures CSCI 1900 Discrete Structures Searching Trees Reading: Kolman, Section 7.3.
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.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
CSCI 115 Chapter 7 Trees. CSCI 115 §7.1 Trees §7.1 – Trees TREE –Let T be a relation on a set A. T is a tree if there exists a vertex v 0 in A s.t. there.
Lecture 8 Tree.
Tree Data Structures.
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.
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
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.
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.
© University of Auckland Trees – (cont.) CS 220 Data Structures & Algorithms Dr. Ian Watson.
Lecture 17: Trees and Networks I Discrete Mathematical Structures: Theory and Applications.
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.
1 Trees 2 Binary trees Section Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children –Left and.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
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 Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum Spanning.
CC 215 DATA STRUCTURES TREES TRAVERSALS Dr. Manal Helal - Fall 2014 Lecture 9 AASTMT Engineering and Technology College 1.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Trees Chapter 15.
Trees Chapter 11.
Paul Tymann and Andrew Watkins
Trees Another Abstract Data Type (ADT)
Section 8.1 Trees.
Binary Tree Traversal Methods
Trees Another Abstract Data Type (ADT)
Binary Tree Traversal Methods
Abstract Data Structures
Paul Tymann, Andrew Watkins,
Binary Tree Traversal Methods
Paul Tymann, Andrew Watkins,
Binary Tree Properties & Representation
Chapter 20: Binary Trees.
Binary Tree Traversal Methods
Trees Chapter 11.
A Binary Tree is a tree in which each node has at most 2 children
Presentation transcript:

Section 9.3 by Andrew Watkins Tree Traversal Section 9.3 by Andrew Watkins CSE 2813 Discrete Structures

CSE 2813 Discrete Structures 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 Three common traversals are: Preorder traversal Inorder traversal Postorder traversal CSE 2813 Discrete Structures

CSE 2813 Discrete Structures Traversal Let T be an ordered rooted tree with root r. Suppose T1, T2, …,Tn are the subtrees at r from left to right in T. r T1 T2 Tn CSE 2813 Discrete Structures

CSE 2813 Discrete Structures Preorder Traversal Step 1: Visit r r T1 T2 Tn Step 2: Visit T1 in preorder Step 3: Visit T2 in preorder Step n+1: Visit Tn in preorder CSE 2813 Discrete Structures

CSE 2813 Discrete Structures Example A R E Y P M H J Q T M A J Y R H P Q T E CSE 2813 Discrete Structures

CSE 2813 Discrete Structures Inorder Traversal Step 1: Visit T1 in inorder r T1 T2 Tn Step 2: Visit r Step 3: Visit T2 in inorder Step n+1: Visit Tn in inorder CSE 2813 Discrete Structures

CSE 2813 Discrete Structures Example A R E Y P M H J Q T J A M R Y P H Q T E CSE 2813 Discrete Structures

CSE 2813 Discrete Structures Postorder Traversal Step 1: Visit T1 in postorder r T1 T2 Tn Step 2: Visit T2 in postorder Step n: Visit Tn in postorder Step n+1: Visit r CSE 2813 Discrete Structures

CSE 2813 Discrete Structures Example A R E Y P M H J Q T J A R P Q T H Y E M CSE 2813 Discrete Structures

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 CSE 2813 Discrete Structures

CSE 2813 Discrete Structures Example (x+y)2 + (x-3)/(y+2) + 2  / + x y – x 3 + y 2 CSE 2813 Discrete Structures

CSE 2813 Discrete Structures Infix Notation Traverse in inorder adding parentheses for each operation +  – / 2 x y 3 ( ) ( ) ( ) x + y  2 + ( ) ( ) x – 3 / ( ) y + 2 CSE 2813 Discrete Structures

Prefix Notation (Polish Notation) Traverse in preorder +  – / 2 x y 3 +  + x y 2 / – x 3 + y 2 CSE 2813 Discrete Structures

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 CSE 2813 Discrete Structures

CSE 2813 Discrete Structures Example + / + 2 2 2 / – 3 2 + 1 0 + / + 2 2 2 / – 3 2 1 + / + 2 2 2 / 1 1 + / + 2 2 2 1 + / 4 2 1 + 2 1 3 CSE 2813 Discrete Structures

Postfix Notation (Reverse Polish) Traverse in postorder +  – / 2 x y 3 x y + 2  x 3 – y 2 + / + CSE 2813 Discrete Structures

Evaluating Postfix Notation 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 CSE 2813 Discrete Structures

CSE 2813 Discrete Structures Example 2 2 + 2 / 3 2 – 1 0 + / + 4 2 / 3 2 – 1 0 + / + 2 3 2 – 1 0 + / + 2 1 1 0 + / + 2 1 1 / + 2 1 + 3 CSE 2813 Discrete Structures

CSE 2813 Discrete Structures Exercises 7, 9, 10, 12, 13, 15, 16, 17, 18, 23, 24 CSE 2813 Discrete Structures