1 CS 162 Introduction to Computer Science Chapter 9 Binary Trees Herbert G. Mayer, PSU Status 11/23/2014.

Slides:



Advertisements
Similar presentations
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Advertisements

Chapter 6 Structures By C. Shing ITEC Dept Radford University.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Generation I Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
1 CS 410 Mastery in Programming Chapter 8 Printing Binary Trees Herbert G. Mayer, PSU CS Status 5/22/2013.
Expression Trees What is an Expression tree? Expression tree implementation Why expression trees? Evaluating an expression tree (pseudo code) Prefix, Infix,
Stacks Example: Stack of plates in cafeteria.
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
1 CS 162 Introduction to Computer Science Chapter 8 Pointers Herbert G. Mayer, PSU Status 11/20/2014.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
1 Nell Dale Chapter 9 Trees Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 CS 410 Mastery in Programming Chapter 8 Printing Binary Trees Herbert G. Mayer, PSU CS status 7/30/2011.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
1 CS 410 Mastery in Programming Chapter 7 Hints for: Symbolic Differentiation Herbert G. Mayer, PSU CS status 7/26/2011.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
1 CS308 Data Structures An application of binary trees: Binary Expression Trees.
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
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.
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
ECE 103 Engineering Programming Chapter 61 Abstract Data Types Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Data Structures : Project 5 Data Structures Project 5 – Expression Trees and Code Generation.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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.
Tree Data Structures.
1 CS 163 Data Structures Chapter 9 Building, Printing Binary Trees Herbert G. Mayer, PSU Status 5/21/2015.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.
Data Structures – Week #3 Stacks. 9.Mart.2012Borahan Tümer, Ph.D.2 Outline Stacks Operations on Stacks Array Implementation of Stacks Linked List Implementation.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
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.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
CHP-3 STACKS.
Trees. What is a tree? You know…  tall  green  leafy  possibly fruit  branches  roots  I’m sure you’ve seen them.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Binary Tree Course No.:
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.
PHY 201 (Blum)1 Stacks Based in part on material from Chapters 4 & 5 in Computer Architecture by Nicholas Carter.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
1 CS 201 Computer Systems Programming Chapter 7 “Printing Binary Trees” Herbert G. Mayer, PSU CS Status 7/9/2014.
Review Use of Stack Introduction Stack in our life Stack Operations
Data Structure By Amee Trivedi.
CS 163 Data Structures Chapter 10 Symbolic Differentiation
CS 201 Data Structures and Algorithms
Herbert G. Mayer, PSU CS status 7/29/2013
Data Structures – Week #3
CS 201 Computer Systems Programming Chapter 7 “Printing Binary Trees”
Data Structures – Week #5
Section 9.3 by Andrew Watkins
Languages and Compilers (SProg og Oversættere)
Trees, part 2 Lecture 13 CS2110 – Spring 2019
ECE 103 Engineering Programming Chapter 64 Tree Implementation
Data Structures – Week #3
C++ Plus Data Structures
CS 201 Computer Systems Programming Chapter 7 “Printing Binary Trees”
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Presentation transcript:

1 CS 162 Introduction to Computer Science Chapter 9 Binary Trees Herbert G. Mayer, PSU Status 11/23/2014

2 Syllabus Goal Goal Arithmetic Expressions and Trees Arithmetic Expressions and Trees Infix Without Parentheses Infix Without Parentheses Infix With Parentheses Infix With Parentheses Postfix Without Parentheses Postfix Without Parentheses Prefix Without Parentheses Prefix Without Parentheses Interesting Examples Interesting Examples Use of Postfix Use of Postfix

3 Goal Refine understanding C++ pointers by building trees Refine understanding C++ pointers by building trees Tree is a data structure of 0 or more connected nodes, each of which may have up to 2 subtrees Tree is a data structure of 0 or more connected nodes, each of which may have up to 2 subtrees A tree has so called end-nodes and root-nodes A tree has so called end-nodes and root-nodes End-nodes, AKA leaves, have no successor End-nodes, AKA leaves, have no successor Internal nodes, i.e. root-nodes, have 1 or 2 successors, but no more than 2 in a binary tree Internal nodes, i.e. root-nodes, have 1 or 2 successors, but no more than 2 in a binary tree Each node has only one incident pointer, different from a graph! Each node has only one incident pointer, different from a graph! Here we build trees that represent arithmetic expression with dyadic operators, such as: * + - / Here we build trees that represent arithmetic expression with dyadic operators, such as: * + - /

4 Arithmetic Expressions and Trees Three typical notations for dyadic operations: Three typical notations for dyadic operations: Infix notation: write as the first the left operand, reading left-to- right, then list the dyadic operator, finally list the right operand Infix notation: write as the first the left operand, reading left-to- right, then list the dyadic operator, finally list the right operand For CPU: Order will not work for code emission, as the CPU needs both operands for processing the operator For humans: requires parentheses for proper operator precedence Note exception: programming language APL Postfix notation: write left operand first, then list the right operand, finally the operator Postfix notation: write left operand first, then list the right operand, finally the operator This order will work for code emission, as operator has both operands available at processing time Needs no parentheses, and still obeys operator precedence Postfix notation AKA Polish Postfix, after Jan Łukasiewicz, 1920 Prefix notation: First list the operator, next the first (left) operand, finally the second (right) operand Prefix notation: First list the operator, next the first (left) operand, finally the second (right) operand

5 Arithmetic Expressions and Trees a + x ^ c + a^ xc Infix:( a + ( x ^ c ) ) Postfix:a x c ^ + Prefix:+ a ^ x c ^ stands for exponentiation operator, with highest precedence: higher than * or / which in turn have higher priority than + or - which in turn have higher priority than + or -

6 Arithmetic Expressions and Trees ( x – a ) / b / - b ax Infix:( ( x – a ) / b ) Postfix:x a – b / Prefix:/ – x a b / stands for division operator, with higher precedence than, say, –

7 Arithmetic Expressions and Trees a ^ ( b – c ) / d / ^ d -a Infix:( ( a ^ ( b – c ) ) / d ) Postfix:a b c - ^ d / Prefix:/ ^ a – b c d Wrong: a ^ ( ( b – c ) / d ) bc

8 Data Structure to Traverse Trees Implement tree in C++ and traverse to print Implement tree in C++ and traverse to print To do so, define a NodeType data structure To do so, define a NodeType data structure Thus, a node must have operand-operator information (classification), and 1 or 2 subtrees Thus, a node must have operand-operator information (classification), and 1 or 2 subtrees For practical purposes, distinguish literals from variables (i.e. symbolic names) For practical purposes, distinguish literals from variables (i.e. symbolic names) Represent an arithmetic expression as a binary tree of such nodes with operators and operands Represent an arithmetic expression as a binary tree of such nodes with operators and operands And define functions that traverse the tree and prints operands and operators in the right order And define functions that traverse the tree and prints operands and operators in the right order

9 Tree Data Structure // node has class: literal, identifier, or operator. // Parenthesized expressions are reduced in tree: no ( ) typedef enum { Literal, Identifier, Operator } NodeClass; typedef struct NodeType * NodePtr; // forward // actual node structure; using the forward pointers typedef struct NodeType { NodeClass Class; // 3 classes, not C++ “class” NodeClass Class; // 3 classes, not C++ “class” char Symbol; // stores ident or small literal char Symbol; // stores ident or small literal int LitVal; // if Class == Literal: its value int LitVal; // if Class == Literal: its value NodePtr Left; // left subtree NodePtr Left; // left subtree NodePtr Right; // right subtree NodePtr Right; // right subtree } s_node_tp;

10 Make a New Node // malloc() new node from heap. Return pointer to node NodePtr Make( NodeClass Class, char Symbol, int value, NodePtr Left, NodePtr Right ) NodePtr Left, NodePtr Right ) { // Make NodePtr Node = (NodePtr)malloc( sizeof( struct NodeType ) ); if( Node ) { // is node’s space is really there? Node->Class = Class; Node->Symbol = Symbol; Node->LitVal = value; Node->Left = Left; Node->Right = Right; }else{ tree_error( ”No heap space for node”, Class ); } //end if return Node; } //end Make

11 Make a New Node // Example: for a * 8 we create the following tree... Node_ptr left = Make( Identifier, ‘a’, 0, NULL, NULL ); Node_ptr right = Make( Literal, ‘8’, 8, NULL, NULL ); Node_ptr root = Make( Operator, ‘*’, 0, left, right ); return root; } //end building tree

12 Copy Tree // recursively copy tree pointed to by Root. // Pass pointer to the copy to its caller NodePtr Copy( NodePtr Root ) { // Copy if ( Root == NULL ) { return NULL; }else{ return Make( Root->Class, Root->Symbol, Root->LitVal, Copy( Root->Left ), Copy( Root->Right ) ); } //end if } //end Copy

13 Print, Infix, Without Parentheses // Print in infix notation without parentheses ( ) void Print_No_Paren( NodePtr Root ) { // Print_No_Paren if ( Root ) { Print_No_Paren ( Root->Left ); if ( Root->Class == Literal ) { printf( "%d", Root->LitVal ); }else{ printf( "%c", Root->Symbol ); } //end if Print_No_Paren ( Root->Right ); } //end if } //end Print_No_Paren Input: ( a + x ) / b prints as: a + x / b  misleading

14 Print, Infix, With Parentheses // Print in infix notation with parentheses ( and ) // though prints too many ( ) pairs void Print_Infix( NodePtr Root ) { // Print_Infix if ( Root ) { if ( Root->Class == Operator ) printf( "(" ); Print_Infix( Root->Left ); if ( Root->Class == Literal ) { printf( "%d", Root->LitVal ); }else{ printf( "%c", Root->Symbol ); } //end if Print_Infix( Root->Right ); if ( Root->Class == Operator ) printf( ")" ); } //end if } //end Print_Infix Input: ( a + x ) / b prints as: ( ( a + x ) / b ) -- OK

15 Postfix Without Parentheses // Print in Polish Postfix notation, no parentheses void Print_Postfix( NodePtr Root ) { // Print_Postfix if ( Root ) { Print_Postfix( Root->Left ); Print_Postfix( Root->Right ); if ( Root->Class == Literal ) { printf( "%d", Root->LitVal ); }else{ printf( "%c", Root->Symbol ); } //end if } //end Print_Postfix Input: a ^ ( b – c ) / d prints as: a b c - ^ d / -- OK

16 Prefix Without Parentheses // Prefix: operator executes when 2 operands found void Print_Prefix( NodePtr Root ) { // Print_Prefix if ( Root ) { if ( Root->Class == Literal ) { printf( "%d", Root->LitVal ); }else{ printf( "%c", Root->Symbol ); } //end if Print_Prefix ( Root->Left ); Print_Prefix ( Root->Right ); } //end if } //end Print_Prefix Input: ( a + x ) / b prints as: / + a x b -- OK

17 Interesting Examples Input 1:a + b * c ^ ( x – 2 * d ) / ( e – f ) Infix:( a + ( ( b * ( c ^ ( x – ( 2 * d ) ) ) ) / ( e – f ) ) ) Postfix:a b c x 2 d * - ^ * e f - / + Prefix:+ a / * b ^ c – x * 2 d – e f Input 2:4 / x ^ ( k – l / m ) * 8 * x - & 9 + n Infix:( ( ( ( ( 4 / ( x ^ ( k - ( l / m ) ) ) ) * 8 ) * x ) - ( & 9 ) ) + n ) Postfix:4 x k l m / - ^ / 8 * x * 9 & - n + Prefix:+ - * * / 4 ^ x – k / l m 8 x & 9 n

18 Use of Postfix Postfix, AKA Polish Postfix notation is a natural for code generation, not just for stack machines Postfix, AKA Polish Postfix notation is a natural for code generation, not just for stack machines Operands are needed first: Two for dyadic, or one for monadic operations Operands are needed first: Two for dyadic, or one for monadic operations Once generated and available on stack, stack machine can execute the next operation Once generated and available on stack, stack machine can execute the next operation Easy for compiler writer, natural for stack machine Easy for compiler writer, natural for stack machine Stack poor for execution, as all references are through memory: top of stack Stack poor for execution, as all references are through memory: top of stack Even a GPR architecture needs both operands available somewhere (in regs) to execute operator Even a GPR architecture needs both operands available somewhere (in regs) to execute operator

19 References Łukasiewicz: Łukasiewicz: