Postfix (and prefix) notation

Similar documents
Stacks, Queues and Hierarchical Collections

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

An Introduction to Trees

12 Abstract Data Types

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

CS 206 Introduction to Computer Science II

CS W3134: Data Structures in Java

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

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

Stack Abstract Data Type

The Stack and Queue Types

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

TREES. Trees - Introduction

STACKS AND QUEUES. Problem Solving with Computers-II

CSE 230 Intermediate Programming in C and C++

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

Recursive Data Structures and Grammars

DEEPIKA KAMBOJ UNIT 2. What is Stack?

CSI33 Data Structures

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

3137 Data Structures and Algorithms in C++

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

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

Lecture 4 Stack and Queue

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1

Motivation for Queues

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

PA3 Design Specification

CS301 - Data Structures Glossary By

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Algorithms and Data Structures

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Programming Abstractions

March 20/2003 Jayakanth Srinivasan,

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

CS 206 Introduction to Computer Science II

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu.

March 13/2003 Jayakanth Srinivasan,

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Introduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Stacks and Queues. CSE Data Structures April 12, 2002

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

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

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

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms

Binary Tree Application Expression Tree. Revised based on textbook author s notes.

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

Data Structures Question Bank Multiple Choice

Chapter 20: Binary Trees

Solution: The examples of stack application are reverse a string, post fix evaluation, infix to postfix conversion.

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

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

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

Binary Trees, Binary Search Trees

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

CSI33 Data Structures

Chapter 10: Trees. A tree is a connected simple undirected graph with no simple circuits.

Data Structures. Chapter 06. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU

Upcoming ACM Events Linux Crash Course Date: Time: Location: Weekly Crack the Coding Interview Date:

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

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

Separate Compilation and Namespaces Week Fall. Computer Programming for Engineers

Topic Binary Trees (Non-Linear Data Structures)

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

Lecture Data Structure Stack

Final Exam Data Structure course. No. of Branches (5)

COMP 250 Fall binary trees Oct. 27, 2017

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

DATA STRUCTURE UNIT I

infix expressions (review)

DATA STRUCTURES AND ALGORITHMS

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

Prepared By: Ms. Nidhi Solanki (Assist. Prof.) Page 1

Objective Questions for Online Practical Exams under CBCS Scheme Subject: Data Structure-I (CS-113)

15. Stacks and Queues

Programming Abstractions

Data Structure. Recitation VII

List, Stack, and Queues

Binary Trees

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

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

Trees. A tree is a directed graph with the property

Thus, it is reasonable to compare binary search trees and binary heaps as is shown in Table 1.

Stacks, Queues (cont d)

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

Linear Data Structure

1 P age DS & OOPS / UNIT II

Advanced Tree Data Structures

CS 8391 DATA STRUCTURES

Trees. Manolis Koubarakis. Data Structures and Programming Techniques

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

Arrays and Linked Lists


MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

INSTITUTE OF AERONAUTICAL ENGINEERING

ITI Introduction to Computing II

CE 221 Data Structures and Algorithms

Transcription:

Postfix (and prefix) notation Also called reverse Polish reversed form of notation devised by mathematician named Jan Łukasiewicz (so really lü-kä-sha-vech notation) Infix notation is: operand operator operand Like 4 + 22 Requires parentheses sometimes: 5 * (2 + 19) Postfix form is: operand operand operator So 4 22 + No parentheses required: 5 2 19 + * Prefix is operator operand operand: + 4 22

Evaluating postfix expressions Algorithm (start with an empty stack): while expression has tokens { } if next token is operand /* e.g., number */ push it on the stack; else /* next token should be an operator */ pop two operands from stack; perform operation; push result of operation on stack; pop the result; /* should be only thing left on stack */

Postfix evaluation example Expression: 5 4 + 8 * Step 1: push 5 Step 2: push 4 Step 3: pop 4, pop 5, add, push 9 Step 4: push 8 Step 5: pop 8, pop 9, multiply, push 72 Step 6: pop 72 the result A bad postfix expression is indicated by: Less than two operands to pop when operator occurs More than one value on stack at end

Evaluating infix expressions Simplest type: fully parenthesized e.g., ( ( ( 6 + 9 ) / 3 ) * ( 6-4 ) ) Still need 2 stacks: 1 numbers, 1 operators while tokens available { if (number) push on number stack; if (operator) push on operator stack; if ( ( ) do nothing; else { /* must be ) */ pop two numbers, and one operator; calculate; push result on number stack; } } /* should be one number left on stack at end: the result */

Converting infix to postfix Operator precedence matters e.g., 3+(10 2)*5 3 10 2-5 * + Algorithm uses one stack; prints results (alternatively, could append results to a string) For each token in the expression: if ( number ) print it; if ( ( ) push on stack; if ( ) ) pop and print all operators until ( ; discard ( ; if ( operator ) /* more complicated next slide */

Infix to postfix (cont.) /* call current token the new operator */ while (stack is not empty) { peek at top operator on stack; if (top operator precedence >= new operator precedence) pop and print top operator; else break out of while loop; } push new operator on stack after loop ends; At end, pop and print all remaining operators This algorithm does not account for all bad expressions e.g., does not check for too many operators left at end But can verify that parentheses are balanced

Queues Rear Front last enqueued last dequeued 1st enqueued 1st dequeued FIFO data structure First In, First Out Typical operations: enqueue (an item at rear of queue), dequeue (item at front of queue), peek (at front item), empty, full, size, clear i.e., very similar to a stack limited access to items

Some queue applications Many operating system applications Time-sharing systems rely on process queues Often separate queues for high priority jobs that take little time, and low priority jobs that require more time (see last part of section 7.8 in text) Printer queues and spoolers Printer has its own queue with bounded capacity Spoolers queue up print jobs on disk, waiting for print queue Buffers coordinate processes with varying speeds Simulation experiments Models of queues at traffic signals, in banks, etc., used to see what happens under various conditions

A palindrome checker Palindrome - same forward and backward e.g., Abba, and Able was I ere I saw Elba. Lots of ways to solve, including recursive Can use a queue and a stack together Fill a queue and a stack with copies of letters Then empty both together, verifying equality Reminder we re using an abstraction We still don t know how queues are implemented!!! To use them, it does not matter!

Implementing queues Easy to do with a list: Mostly same as stack implementation Enqueue: insertlast(item, list); Then to dequeue and peek: refer to first item Array implementation is trickier: Must keep track of front and rear indices Increment front/rear using modulus arithmetic Indices cycle past last index to first again idea is to reuse the beginning of the array after dequeues More efficient but can become full Usually okay, but some queues should be unbounded

Linked lists revisited: variations Some are meant to speed up operations e.g., O(n) complexity to access last item Way to make it O(1): maintain pointer to last easy and worth it! Another way: circular, double-linked list not so easy Some are meant to increase usefulness e.g., circular list to solve Josephus problem e.g., generalized lists (lists of lists upcoming topic) Trade-offs: use more space, harder to program

Implementing better lists Using double-linked lists both harder and easier Must keep track of twice as many pointers Additional work required for most special cases But easy insert before, traverse backwards, access last Can use sentinel nodes that are hidden from user e.g., first and last sentinals list is never really empty Eliminates lots of special cases just have to lie to user e.g., n th position sentinels to speed access to i th item Usually trading off: speed space effort

Generalized lists When list items may be sublists May also contain just single items called atoms Usually implement with union in node structure e.g., instead of just info field, have info or sublist: union SubNodeTag{ InfoPointer info; NodePointer sublist; } SubNode; Also need field to identify a node as atom or sublist Lots of applications see text section 8.4

Is <string.h> an ADT? Combined with (char *) data it is! Easy to formalize say String.h: typedef char *String; /* the data type */ int strlen(string); /* length of string */ int strcmp(string,string); /* compare 2 strings */ String strcpy(string,string); /* copy 2 nd to 1 st */ /* and so on */ Note what doesn t matter: How strings are represented internally How these functions are implemented

Trees child of R parent of Y, Z S X R U root T V W internal node Y Z descendants of S leaf

Binary trees Each node can have 0, 1, or 2 children only i.e., a binary tree node is a subtree that is either empty, or has left and right subtrees Notice this is a recursive definition Concept: a leaf s children are two empty subtrees Half (+1) of all nodes in full binary tree are leaves All nodes except leaves have 2 non-empty subtrees Exactly 2 k nodes at each depth k, k < (leaf level) A complete binary tree satisfies two conditions Is full except for leaf level All leaves are stored as far to the left as possible

Representing trees by links Much more flexible than array representation Because most trees are not as regular as heaps (later) Array representation usually wastes space, and does not accommodate changes well Binary tree node has two links, one for each child typedef struct treenode { DataType info; /* some defined data type */ struct treenode *left; /* one child */ struct treenode *right; /* other child */ } TreeNode, *TreeNodePointer; /* types */ Not a binary tree? keep list of children instead

Traversing binary trees Example: an expression tree (a type of parse tree built by advanced recursion techniques discussed in chapter 14) representing this infix expression: 4 + 7 * 11 + 4 * 7 11 Infix is in-order traversal Left subtree node right subtree But can traverse in other orders Pre-order: node left right, gives prefix notation: + 4 * 7 11 Post-order: left right node, gives postfix notation: 4 7 11 * +