4/17/2017 Section 9.3 Tree Traversal ch9.3.

Slides:



Advertisements
Similar presentations
Chapter 10, Section 10.3 Tree Traversal
Advertisements

CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
Expression Trees What is an Expression tree? Expression tree implementation Why expression trees? Evaluating an expression tree (pseudo code) Prefix, Infix,
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
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
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Binary Tree B G E D I H F c A Binary tree
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
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.
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.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
10.3 Tree Transversal. Pre/post fix notation and order See handout. a.bc.d e f g h i j k.
2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
ساختمانهای گسسته دانشگاه صنعتی شاهرود – اردیبهشت 1392.
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.
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.
Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; } 
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.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Lecture 8 Tree.
Tree Data Structures.
Compiled by: Dr. Mohammad Omar Alhawarat
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Binary Tree. Contiguous versus Linked List Insertion in Contiguous list needs a lot of move. For big chunks of records it is time consuming. Linked List.
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.
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.
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
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.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
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.
Discrete Mathematics Trees.
Data Structure By Amee Trivedi.
Trees Chapter 11.
Paul Tymann and Andrew Watkins
Source Code for Data Structures and Algorithm Analysis in C (Second Edition) – by Weiss
Section 8.1 Trees.
Abstract Data Structures
Paul Tymann, Andrew Watkins,
Section 9.3 by Andrew Watkins
Paul Tymann, Andrew Watkins,
Chapter 20: Binary Trees.
Trees Chapter 11.
A Binary Tree is a tree in which each node has at most 2 children
Presentation transcript:

4/17/2017 Section 9.3 Tree Traversal ch9.3

Universal Address System In ordered rooted trees, vertices may be labeled according to the following scheme: choose a root node and label it 0 each of root’s k children are labeled, left to right, as 1, 2, … , k for each vertex v at level n with label A, label its kv children left to right as A.1, A.2, … A.k

Universal Address System of an Ordered Rooted Tree

Traversal Algorithms Traversal: procedure for visiting each vertex in an ordered tree for data access Three most commonly used traversal algorithms are: preorder inorder postorder

Preorder Traversal In preorder traversal, the root vertex is visited first Then the left subtree is visited using a preorder traversal Then the right subtree is visited using a preorder traversal Gives same ordering of vertices as the universal address system

Pre-order traversal in action Original tree: Results: K K I K I R W R K W O O O O D D

Inorder traversal From the root vertex, proceed to the left subtree and perform an inorder traversal Return to root and access the data there Traverse the right subtree using inorder traversal

In-order traversal in action Original tree: Results: K K I I R W K R K W O O O O D D

Postorder traversal From root node, proceed to left subtree and perform postorder traversal Perform postorder traversal of right subtree Access data at root vertex

Post-order traversal in action K Original tree: Results: W K I I R O D O K W O R O D K

Infix, prefix and postfix notation Ordered rooted trees (especially ordered binary trees) are useful in representing complicated expressions (e.g. compound propositions, arithmetic expressions) A binary expression tree is a tree used to represent such an expression

Example 1 Create an ordered tree to represent the expression (x+y)2 + (x-4)/3 operands are represented as leaves operators are represented as roots of subtrees

Example 1 Subtrees of binary expression tree for (x+y)2 + (x-4)/3: 4/17/2017 Example 1 Subtrees of binary expression tree for (x+y)2 + (x-4)/3: Complete binary expression tree for (x+y)2 + (x-4)/3: + / \ x y - / \ x 4 + / \ ^  / \ / \ + 2 - 3 / \ / \ x y x 4 ^ / \ + 2 x y  / \ - 3 x 4 ch9.3

Traversing binary expression tree Inorder traversal of binary expression tree produces original expression (without parentheses), in infix order Preorder traversal produces a prefix expression Postorder traversal produces a postfix expression

Prefix expressions The prefix version of the expression (x+y)2 + (x-4)/3 is: + ^ + x y 2 / - x 4 3 Evaluating prefix expressions: Read expression right to left When an operator is encountered, apply it to the previous operand (if unary) or operands (if binary) , placing the result back into the expression where the subexpression had been

Example 2 + * / 4 2 3 9 // original expression + * 2 3 9 // 4/2 evaluated + 6 9 // 2*3 evaluated 15 // 6+9 evaluated

Example 3 * - + 4 3 5 / + 2 4 3 // original expression * - + 4 3 5 / 6 3 // 2+4 evaluated * - + 4 3 5 2 // 6/3 evaluated * - 7 5 2 // 4+3 evaluated * 2 2 // 7-5 evaluated 4 // 2*2 evaluated

Postfix expressions Also known as reverse Polish expressions Like infix, they are evaluated left to right Like prefix, they are unambiguous, not requiring parentheses To evaluate: read expression left to right; as soon as an operator is encountered, perform the operation and place the result back in the expression

Postfix expressions Simple expression: Original Expression: A + B Postfix Equivalent: A B + Compound expression with parentheses: original: (A + B) * (C - D) postfix: A B + C D - * Compound expression without parentheses: original: A + B * C - D postfix: A B C * + D -

Example 4 6 3 / 4 2 * + // original expression 2 4 2 * + // 6/3 evaluated 2 8 + // 4*2 evaluated 10 // 2+8 evaluated

Example 5 5 4 * 10 2 - 2 / + 3 * // original expression 20 10 2 - 2 / + 3 * // 5*4 evaluated 20 8 2 / + 3 * // 10-2 evaluated 20 4 + 3 * // 8/2 evaluated 24 3 * // 20+4 evaluated 72 // 24*3 evaluated

Using rooted trees to represent compound propositions Works exactly the same way as arithmetic expressions Innermost expression is bottom left subtree, with proposition(s) as leaf(s) and operator as root Root vertex is operator of outermost expression Using various traversal methods, can produce infix, prefix and postfix versions of compound proposition

Example 6 Subtrees: Complete binary expression tree: Find the ordered rooted tree representing the compound proposition ((p  q)  (p  q) Subtrees: Complete binary expression tree:  / \ p q  | p  | q  / \   | / \    / \ | | p q p q  / \   | | p q  |  / \ p q

Example 6 Preorder traversal yields the expression:  / \   | / \    / \ | | p q p q Preorder traversal yields the expression:    p q   p  q Postorder traversal yields the expression: p q   p  q   

Section 9.3 Tree Traversal