There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

Similar documents
Programming, Data Structures and Algorithms Prof. Hema A Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

Programming, Data Structures and Algorithms Prof. Hema Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

Binary Trees

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

CE 221 Data Structures and Algorithms

March 20/2003 Jayakanth Srinivasan,

TREES. Trees - Introduction

Binary Trees, Binary Search Trees

Programming, Data Structures and Algorithms Prof. Hema Murthy Department of Computer Science and Engineering Indian Institute Technology, Madras

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Algorithms and Data Structures

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

F453 Module 7: Programming Techniques. 7.2: Methods for defining syntax

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node

syntax tree - * * * * * *

ASTS, GRAMMARS, PARSING, TREE TRAVERSALS. Lecture 14 CS2110 Fall 2018


COMP 250 Fall binary trees Oct. 27, 2017

Formal Languages and Automata Theory, SS Project (due Week 14)

Chapter 20: Binary Trees

Week - 03 Lecture - 18 Recursion. For the last lecture of this week, we will look at recursive functions. (Refer Slide Time: 00:05)

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Introduction to Computers and Programming. Concept Question

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Friday Four Square! 4:15PM, Outside Gates

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

Finish Lec12 TREES, PART 2. Announcements. JavaHyperText topics. Trees, re-implemented. Iterate through data structure 3/7/19

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

INF2220: algorithms and data structures Series 1

Binary Search Trees Treesort

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Why Use Binary Trees? Data Structures - Binary Trees 1. Trees (Contd.) Trees

Tree. Virendra Singh Indian Institute of Science Bangalore Lecture 11. Courtesy: Prof. Sartaj Sahni. Sep 3,2010

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Week 02 Module 06 Lecture - 14 Merge Sort: Analysis

Associate Professor Dr. Raed Ibraheem Hamed

Data Structures - Binary Trees 1


Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Trees. (Trees) Data Structures and Programming Spring / 28

INF2220: algorithms and data structures Series 1

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees

There are functions to handle strings, so we will see the notion of functions itself in little a detail later. (Refer Slide Time: 00:12)

(Refer Slide Time 3:31)

a graph is a data structure made up of nodes in graph theory the links are normally called edges

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

syntax tree - * * * - * * * * * 2 1 * * 2 * (2 * 1) - (1 + 0)

Data Structures. Binary Trees. Root Level = 0. number of leaves:?? leaves Depth (Maximum level of the tree) leaves or nodes. Level=1.

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CSI33 Data Structures

3137 Data Structures and Algorithms in C++

Binary Search Trees. See Section 11.1 of the text.

Data Structures and Algorithms for Engineers

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi.

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

Week - 01 Lecture - 04 Downloading and installing Python

Stacks, Queues and Hierarchical Collections

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

Software Testing Prof. Meenakshi D Souza Department of Computer Science and Engineering International Institute of Information Technology, Bangalore

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

(Refer Slide Time 6:48)

Visit ::: Original Website For Placement Papers. ::: Data Structure

CS61B Lecture #20. Today: Trees. Readings for Today: Data Structures, Chapter 5. Readings for Next Topic: Data Structures, Chapter 6

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS. Regrades 10/6/15. Prelim 1. Prelim 1. Expression trees. Pointers to material

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

(Refer Slide Time: 00:01:30)

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

(Refer Slide Time: 05:25)

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Module 02 Lecture - 45 Memoization

Why Do We Need Trees?

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

(Refer Slide Time: 02.06)

Continuing with whatever we saw in the previous lectures, we are going to discuss or continue to discuss the hardwired logic design.

IX. Binary Trees (Chapter 10) Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

IX. Binary Trees (Chapter 10)

CSC148 Week 6. Larry Zhang

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

CS61B Lecture #21. A Recursive Structure. Fundamental Operation: Traversal. Preorder Traversal and Prefix Expressions. (- (- (* x (+ y 3))) z)

Computer Graphics Prof. Sukhendu Das Dept. of Computer Science and Engineering Indian Institute of Technology, Madras Lecture - 14

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Binary search trees 3. Binary search trees. Binary search trees 2. Reading: Cormen et al, Sections 12.1 to 12.3

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Table of Contents. Chapter 1: Introduction to Data Structures... 1

Data Structure Advanced

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

Transcription:

Programming, Data Structures and Algorithms Prof. Hema Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 49 Module 09 Other applications: expression tree from The post-order expression Traversal of binary trees: preorder, postorder, inorder; Illustration with expression tree and infix, postfix forms There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it. (Refer Slide Time: 00:12) Suppose, I have abc+*, what we do is, when we have symbols, first we have again a forest of trees pointing to a, this pointing to b, this pointing to as you traverse from the left to right of this expression. As you move from left to right of this expression what you do, you just create pointers to trees in a ((refer time: 1:10)). Then, what you do is you come across +, then you create a new node with + and then, you take the top most two, it is like a stack, it is actually like a stack. Then, you it is actually a stack of tress. What we do is now, I put these from left to right, I put the trees a, b and c. And then, as soon as I come across the operator, what do I do, I pop the most recent two trees, then I create a new tree with the + and I make the first tree the left child, the most recently popped out tree be the left child and the previously popped out tree be the right child.

Now, again I have one more symbol left, I come across the *. So, I pop out both the trees and I create put on to the stack, the * and the two pop trees become the, most recently popped tree becomes the right child and the most recently popped tree becomes the left child and the previously popped tree becomes the right child and as soon as, until you get a single tree which exists on the stack. So, this is the other application of binary trees and stacks. Both of them can be used quite efficiently to build what are called expression trees. So, this is a very, very useful application of binary trees. (Refer Slide Time: 03:07) Now, another thing we always like to do when we build binary trees. Let us take this expression tree itself, let us look at an expression. Let us say we have something like this, then what we are doing the expression is evaluated,. So, it is evaluated from left. So, let us say I have this here + * b c actually go over this from the right and Let us say I form an expression tree, I am not known how many of you are familiar, we use this kind of a expression parsing to do this expression form or expression and term is form or a factor and let us say factor is the name as it is what it mean, let us say this is a, b, c and. So, on here. Suppose, I use this grammar to generate this expression, we have already done a course on compilers. Let us see, this is completely an unambiguous grammar, we add up here is + or - * or /, let us say. So, how do we parse this given expression? So, we start from the root over here, expression is defined as expression add operator terms, I start it over here.

(Refer Slide Time: 05:48) Plus, then what do we have, then on the left hand side, then the other + again add the +, then I will get a here, I will get * b c and * b + e f, this is how you will get a binary tree. What I have done is, all the exercise that I have done is to just show that the expressions, harmless expressions that I have written like this, it can be written as the binary tree. Now, what we will do is we will talk about other things now. We will talk about traversal of this tree. Let us see what it gives us. There are three types of traversals preorder, postorder and inorder. Now, the definitions are given over here, how to traverse these trees. (Refer Slide Time: 07:06) So, preorder traversal says visit the root, then what you do, you visit the left child in

preorder recursively, visit the right child in preorder recursively. It means what now, let us take the inorder traversal says visit the left child in inorder recursively, visit the root this is the right child in inorder recursively. Then, postorder traversal says visit the left child in postorder. (Refer Slide Time: 07:39) And visit the right child in postorder recursively, visit the root. Let us see what this gives,. So, everything has to be done recursively. What is the meaning of this? (Refer Slide Time: 07:52) Let us look at inorder traversal, visit the left child in inorder recursively. Let us say, I want to do this traversal of the tree, traversal means simply walking around to it. Just the

way the listed node is got changes. Now, what it says let us look at inorder traversal, it says first we look at inorder, because it will become clear to you what we are doing. Visit left child recursively, visit root and visit right child recursively is what here inorder traversal is. So, let us look at the tree what could we do. that means, I am starting from the root here and I am going to recursively keep on going down to this until I reach the leaf node. Then, what do I do, I display the leaf node. So, I keep going down, because I have to visit the left most child. Then, what happens I go up, then I visit the root and get +, then again I go to the right child and again after recursively do the same thing; that means, what preorder, inorder all these are recursive traversals. So, I go to the right child, then again I will do left child recursively, root, then then right child and then go back. Then, what happens here,. So, I come down this + go down here, this becomes b * c and then what happens, this is node as we already been visited,. So, I go back up to the root of the tree, give me +, then again I come to the right node star, again I have to do the recursively left child. So, I do d, I come to the leaf node *, then I have to add e + f, the bracket has to come, because it is one level lower. So, inorder traversal essentially gives me the expression back. So, what is it tell me now, may I look at the inorder traversal of the tree, I get the infix expression of the given tree. This one extra information that we have used here, we use the factor * is done after +, because it is one level lower in the tree,. So, I get the infix expression. So, let us look at what postorder expression, postorder traversal of the tree will give you. What does postorder traversal say, postorder traversal says visit left child postorder in postorder recursively, then visit right child in postorder recursively, visit root and the same thing keeps on getting repeated again and again. So, I take the left child recursively visit in postorder, let me just explain this over here. Actually, what this mean to us, what does it gives you,. So, I am moved here, visit the left child, I cannot came down, there are no more leaf nodes,. So, I display a. Then, I have to go to the right child, go back left child, right child and then root is what it says, go to the right child. Then, right child again I have to recursively go down, do postorder, left child, right child, then root.

(Refer Slide Time: 11:45) So, what will happen, I get a b c *, then, what do I get, now all of them have been visited, I go back up,. So, left child, right child, then root get + here and then, what happens. So, the left child of this root tree is completed now,. So, I go to the right tree over here. Then, what do I get now, what do I get here now, I have get d, then again this is the left child, this is the leaf node, there is nothing else to see, therefore, I display the leaf node. Then, I go to its right child, again I have to look at postorder over there. Therefore, I get e f + e left child postorder, right child postorder, root then I will put the + over here that is how it comes here and then finally, this * and then finally, the *, the last + sign, that we have. So, I am made a mistake here, this + should not be here. See, a b c *, then I have to do the right child, d e f + * +. So, what did we get now, we got a b c * + correct, d e f + * +. How many are there? 1, 2, 3 correct. So, this is what we get as the expression. What is the expression now, this is nothing,, but the postfix expression which all of us have already been done. When we talked about stack, stacks we talked about generating the postfix expression from an infix expression. So, what is interesting here is once it is represented as a binary tree, if I do inorder traversal I get the infix expression, when I do postorder traversal I get the postfix expression and obviously, if I do preorder traversal I will get the prefix expression. So, this is also a very big application of binary trees. Now, once this expression is given like this, I can use this stack of trees to evaluate this particular expression. So, I hope you

are with me here and you have understood what I have said. So, what is it we are doing notice that another we have looking at this various traversals to make it simpler is preorder traversal can be thought of as, you display the node the first time you visit it. In inorder traversal, you display the node the second time you will visit it. Remember, I have drawn this path over here which goes around the tree and in postorder traversal you display the node, the last time you visit it. So, these are simple traversals schemes. So, now the next question that we would like to ask is suppose you are given only the traversals, traversals are given, can we construct the tree exactly? Let us look at only with respect to binary trees and see if I am given the, what is that we have two traversals now, inorder traversal and we have the postorder traversal. We can also do the preorder I encourage you to do the preorder traversal of the tree. Now, given the inorder traversal can I construct the tree uniquely without any other information and given the postorder traversal, can I construct the tree uniquely, let us see how you will do this in the next lecture.