SlideShare a Scribd company logo
1 of 38
Trees
Nature Lover’s View Of A Tree 
root 
branches 
leaves
Computer Scientist’s View 
root leaves 
branches 
nodes
Linear Lists And Trees 
• Linear lists are useful for serially ordered data. 
 (e0, e1, e2, …, en-1) 
 Days of week. 
 Months in a year. 
 Students in this class. 
• Trees are useful for hierarchically ordered data. 
 Employees of a corporation. 
• President, vice presidents, managers, and so on. 
 Java’s classes. 
• Object is at the top of the hierarchy. 
• Subclasses of Object are next, and so on.
Hierarchical Data And Trees 
• The element at the top of the hierarchy is the 
root. 
• Elements next in the hierarchy are the children 
of the root. 
• Elements next in the hierarchy are the 
grandchildren of the root, and so on. 
• Elements that have no children are leaves.
Java’s Classes (Part Of Figure 1.1) 
children of root 
grand children of root 
root 
great grand child of root 
Object 
Number Throwable OutputStream 
Integer Double Exception FileOutputStream 
RuntimeException
Definition 
• A tree t is a finite nonempty set of elements. 
• One of these elements is called the root. 
• The remaining elements, if any, are 
partitioned into trees, which are called the 
subtrees of t.
Subtrees 
Object 
root 
Number Throwable OutputStream 
Integer Double Exception FileOutputStream 
RuntimeException
Leaves 
Object 
Number Throwable OutputStream 
Integer Double Exception FileOutputStream 
RuntimeException
Parent, Grandparent, Siblings, Ancestors, Descendants 
Object 
Number Throwable OutputStream 
Integer Double Exception FileOutputStream 
RuntimeException
Level 3 
Level 1 
Level 4 
Level 2 
Levels 
Object 
Number Throwable OutputStream 
Integer Double Exception FileOutputStream 
RuntimeException
Caution 
• Some texts start level numbers at 0 rather than 
at 1. 
• Root is at level 0. 
• Its children are at level 1. 
• The grand children of the root are at level 2. 
• And so on. 
• We shall number levels with the root at level 1.
height = depth = number of levels 
Level 3 
Object 
Number Throwable OutputStream 
Integer Double Exception FileOutputStream 
RuntimeException 
Level 4 
Level 2 
Level 1
Node Degree = Number Of Children 
Object 
3 
Number Throwable OutputStream 
2 1 1 
0 0 1 0 
Integer Double Exception FileOutputStream 
RuntimeException 
0
Tree Degree = Max Node Degree 
2 1 1 
0 0 1 0 
Degree of tree = 3. 
Object 
3 
Number Throwable OutputStream 
Integer Double Exception FileOutputStream 
RuntimeException 
0
Binary Tree 
• Finite (possibly empty) collection of elements. 
• A nonempty binary tree has a root element. 
• The remaining elements (if any) are partitioned 
into two binary trees. 
• These are called the left and right subtrees of 
the binary tree.
Differences Between A Tree & A Binary Tree 
• No node in a binary tree may have a degree 
more than 2, whereas there is no limit on 
the degree of a node in a tree. 
• A binary tree may be empty; a tree cannot 
be empty.
Differences Between A Tree & A Binary Tree 
• The subtrees of a binary tree are ordered; 
those of a tree are not ordered. 
a 
b 
a 
b 
• Are different when viewed as binary trees. 
• Are the same when viewed as trees.
Arithmetic Expressions 
• (a + b) * (c + d) + e – f/g*h + 3.25 
• Expressions comprise three kinds of entities. 
 Operators (+, -, /, *). 
 Operands (a, b, c, d, e, f, g, h, 3.25, (a + b), (c + d), 
etc.). 
 Delimiters ((, )).
Operator Degree 
• Number of operands that the operator requires. 
• Binary operator requires two operands. 
 a + b 
 c / d 
 e - f 
• Unary operator requires one operand. 
 + g 
 - h
Infix Form 
• Normal way to write an expression. 
• Binary operators come in between their left and 
right operands. 
 a * b 
 a + b * c 
 a * b / c 
 (a + b) * (c + d) + e – f/g*h + 3.25
Operator Priorities 
• How do you figure out the operands of an 
operator? 
 a + b * c 
 a * b + c / d 
• This is done by assigning operator priorities. 
 priority(*) = priority(/) > priority(+) = priority(-) 
• When an operand lies between two operators, 
the operand associates with the operator that 
has higher priority.
Tie Breaker 
• When an operand lies between two operators 
that have the same priority, the operand 
associates with the operator on the left. 
 a + b - c 
 a * b / c / d
Delimiters 
• Subexpression within delimiters is treated 
as a single operand, independent from the 
remainder of the expression. 
 (a + b) * (c – d) / (e – f)
Infix Expression Is Hard To Parse 
• Need operator priorities, tie breaker, and 
delimiters. 
• This makes computer evaluation more 
difficult than is necessary. 
• Postfix and prefix expression forms do not 
rely on operator priorities, a tie breaker, or 
delimiters. 
• So it is easier for a computer to evaluate 
expressions that are in these forms.
Postfix Form 
• The postfix form of a variable or constant is 
the same as its infix form. 
 a, b, 3.25 
• The relative order of operands is the same 
in infix and postfix forms. 
• Operators come immediately after the 
postfix form of their operands. 
 Infix = a + b 
 Postfix = ab+
Postfix Examples 
• Infix = a + b * c 
 Postfix = a b c * + 
• Infix = a * b + c 
 Postfix = a b * c + 
• Infix = (a + b) * (c – d) / (e + f) 
 Postfix = a b + c d - * e f + /
Unary Operators 
• Replace with new symbols. 
 + a => a @ 
 + a + b => a @ b + 
 - a => a ? 
 - a-b => a ? b -
Postfix Evaluation 
• Scan postfix expression from left to right 
pushing operands on to a stack. 
• When an operator is encountered, pop as 
many operands as this operator needs; 
evaluate the operator; push the result on to 
the stack. 
• This works because, in postfix, operators 
come immediately after their operands.
Postfix Evaluation 
• (a + b) * (c – d) / (e + f) 
• a b + c d - * e f + / 
• a b + c d - * e f + / 
a 
stack 
• a b + c d - * e f + / 
b 
• a b + c d - * e f + /
Postfix Evaluation 
• (a + b) * (c – d) / (e + f) 
• a b + c d - * e f + / 
• a b + c d - * e f + / 
• a b + c d - * e f + / 
• a b + c d - * e f + / 
• a b + c d - * e f + / c 
• a b + c d - * e f + / 
d 
(a + b) 
stack 
• a b + c d - * e f + /
Postfix Evaluation 
• (a + b) * (c – d) / (e + f) 
• a b + c d - * e f + / 
(a + b) 
stack 
• a b + c d - * e f + / 
(c – d)
Postfix Evaluation 
• (a + b) * (c – d) / (e + f) 
• a b + c d - * e f + / 
• a b + c d - * e f + / 
• a b + c d - * e f + / f 
• a b + c d - * e f + / 
(a + b)*(c – d) 
stack 
• a b + c d - * e f + / 
e
Postfix Evaluation 
• (a + b) * (c – d) / (e + f) 
• a b + c d - * e f + / 
(a + b)*(c – d) 
stack 
• a b + c d - * e f + / 
(e + f) 
• a b + c d - * e f + / 
• a b + c d - * e f + / 
• a b + c d - * e f + / 
• a b + c d - * e f + /
Prefix Form 
• The prefix form of a variable or constant is 
the same as its infix form. 
 a, b, 3.25 
• The relative order of operands is the same 
in infix and prefix forms. 
• Operators come immediately before the 
prefix form of their operands. 
 Infix = a + b 
 Postfix = ab+ 
 Prefix = +ab
Binary Tree Form 
• a + b + 
a b 
• - a - 
a
Binary Tree Form 
• (a + b) * (c – d) / (e + f) 
/ 
+ 
a b 
- 
c d 
+ 
e f 
* 
/
Merits Of Binary Tree Form 
• Left and right operands are easy to visualize. 
• Code optimization algorithms work with the 
binary tree form of an expression. 
• Simple recursive evaluation of expression. 
+ 
a b 
- 
c d 
+ 
e f 
* 
/

More Related Content

What's hot

DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURESbca2010
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler DesignAkhil Kaushik
 
Core csharp and net quick reference
Core csharp and net quick referenceCore csharp and net quick reference
Core csharp and net quick referenceilesh raval
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up ParsingGerwin Ocsena
 
Python - Regular Expressions
Python - Regular ExpressionsPython - Regular Expressions
Python - Regular ExpressionsMukesh Tekwani
 
Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Iffat Anjum
 
Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4Iffat Anjum
 
Category Theory for Programmers
Category Theory for ProgrammersCategory Theory for Programmers
Category Theory for ProgrammersSantosh Rajan
 
C# quick ref (bruce 2016)
C# quick ref (bruce 2016)C# quick ref (bruce 2016)
C# quick ref (bruce 2016)Bruce Hantover
 
Finaal application on regular expression
Finaal application on regular expressionFinaal application on regular expression
Finaal application on regular expressionGagan019
 
BTree, Data Structures
BTree, Data StructuresBTree, Data Structures
BTree, Data StructuresJibrael Jos
 
Top down parsing(sid) (1)
Top down parsing(sid) (1)Top down parsing(sid) (1)
Top down parsing(sid) (1)Siddhesh Pange
 
types, types, types
types, types, typestypes, types, types
types, types, typesFronx Wurmus
 

What's hot (20)

DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
Core csharp and net quick reference
Core csharp and net quick referenceCore csharp and net quick reference
Core csharp and net quick reference
 
Left factor put
Left factor putLeft factor put
Left factor put
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up Parsing
 
Module 11
Module 11Module 11
Module 11
 
Python - Regular Expressions
Python - Regular ExpressionsPython - Regular Expressions
Python - Regular Expressions
 
Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
 
Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4
 
Category Theory for Programmers
Category Theory for ProgrammersCategory Theory for Programmers
Category Theory for Programmers
 
C# quick ref (bruce 2016)
C# quick ref (bruce 2016)C# quick ref (bruce 2016)
C# quick ref (bruce 2016)
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
Finaal application on regular expression
Finaal application on regular expressionFinaal application on regular expression
Finaal application on regular expression
 
Antlr V3
Antlr V3Antlr V3
Antlr V3
 
BTree, Data Structures
BTree, Data StructuresBTree, Data Structures
BTree, Data Structures
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Top down parsing(sid) (1)
Top down parsing(sid) (1)Top down parsing(sid) (1)
Top down parsing(sid) (1)
 
types, types, types
types, types, typestypes, types, types
types, types, types
 

Similar to Lec19

Unit I - Evaluation of expression
Unit I - Evaluation of expressionUnit I - Evaluation of expression
Unit I - Evaluation of expressionDrkhanchanaR
 
Normalization.pdf
Normalization.pdfNormalization.pdf
Normalization.pdfabhijose1
 
Stack application
Stack applicationStack application
Stack applicationStudent
 
LEC 7-DS ALGO(expression and huffman).pdf
LEC 7-DS  ALGO(expression and huffman).pdfLEC 7-DS  ALGO(expression and huffman).pdf
LEC 7-DS ALGO(expression and huffman).pdfMuhammadUmerIhtisham
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming LanguageSteve Johnson
 
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)Brendan Eich
 
M.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C LanguageM.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C LanguageDr.Florence Dayana
 
Oleksii Levzhynskyi "Solving behavioral complexity with FRP"
Oleksii Levzhynskyi "Solving behavioral complexity with FRP"Oleksii Levzhynskyi "Solving behavioral complexity with FRP"
Oleksii Levzhynskyi "Solving behavioral complexity with FRP"Fwdays
 
C sharp part 001
C sharp part 001C sharp part 001
C sharp part 001Ralph Weber
 
Binary expression tree
Binary expression treeBinary expression tree
Binary expression treeShab Bi
 
Optimizing the Catalyst Optimizer for Complex Plans
Optimizing the Catalyst Optimizer for Complex PlansOptimizing the Catalyst Optimizer for Complex Plans
Optimizing the Catalyst Optimizer for Complex PlansDatabricks
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)mailmerk
 

Similar to Lec19 (20)

Team 4
Team 4Team 4
Team 4
 
Unit I - Evaluation of expression
Unit I - Evaluation of expressionUnit I - Evaluation of expression
Unit I - Evaluation of expression
 
Normalization.pdf
Normalization.pdfNormalization.pdf
Normalization.pdf
 
Stack application
Stack applicationStack application
Stack application
 
LEC 7-DS ALGO(expression and huffman).pdf
LEC 7-DS  ALGO(expression and huffman).pdfLEC 7-DS  ALGO(expression and huffman).pdf
LEC 7-DS ALGO(expression and huffman).pdf
 
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
 
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
 
c-programming
c-programmingc-programming
c-programming
 
M.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C LanguageM.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C Language
 
Oleksii Levzhynskyi "Solving behavioral complexity with FRP"
Oleksii Levzhynskyi "Solving behavioral complexity with FRP"Oleksii Levzhynskyi "Solving behavioral complexity with FRP"
Oleksii Levzhynskyi "Solving behavioral complexity with FRP"
 
C language
C languageC language
C language
 
Java chapter 3
Java chapter 3Java chapter 3
Java chapter 3
 
Compiler Design Unit 2
Compiler Design Unit 2Compiler Design Unit 2
Compiler Design Unit 2
 
C sharp part 001
C sharp part 001C sharp part 001
C sharp part 001
 
Binary expression tree
Binary expression treeBinary expression tree
Binary expression tree
 
C Operators
C OperatorsC Operators
C Operators
 
Lec2+lec3
Lec2+lec3Lec2+lec3
Lec2+lec3
 
Optimizing the Catalyst Optimizer for Complex Plans
Optimizing the Catalyst Optimizer for Complex PlansOptimizing the Catalyst Optimizer for Complex Plans
Optimizing the Catalyst Optimizer for Complex Plans
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
 

More from Nikhil Chilwant (20)

The joyless economy
The joyless economyThe joyless economy
The joyless economy
 
I 7
I 7I 7
I 7
 
IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )
 
Lec39
Lec39Lec39
Lec39
 
Lec38
Lec38Lec38
Lec38
 
Lec37
Lec37Lec37
Lec37
 
Lec36
Lec36Lec36
Lec36
 
Lec35
Lec35Lec35
Lec35
 
Lec34
Lec34Lec34
Lec34
 
Lec33
Lec33Lec33
Lec33
 
Lec32
Lec32Lec32
Lec32
 
Lec30
Lec30Lec30
Lec30
 
Lec29
Lec29Lec29
Lec29
 
Lec28
Lec28Lec28
Lec28
 
Lec27
Lec27Lec27
Lec27
 
Lec26
Lec26Lec26
Lec26
 
Lec25
Lec25Lec25
Lec25
 
Lec24
Lec24Lec24
Lec24
 
Lec23
Lec23Lec23
Lec23
 
Lec20
Lec20Lec20
Lec20
 

Lec19

  • 2. Nature Lover’s View Of A Tree root branches leaves
  • 3. Computer Scientist’s View root leaves branches nodes
  • 4. Linear Lists And Trees • Linear lists are useful for serially ordered data.  (e0, e1, e2, …, en-1)  Days of week.  Months in a year.  Students in this class. • Trees are useful for hierarchically ordered data.  Employees of a corporation. • President, vice presidents, managers, and so on.  Java’s classes. • Object is at the top of the hierarchy. • Subclasses of Object are next, and so on.
  • 5. Hierarchical Data And Trees • The element at the top of the hierarchy is the root. • Elements next in the hierarchy are the children of the root. • Elements next in the hierarchy are the grandchildren of the root, and so on. • Elements that have no children are leaves.
  • 6. Java’s Classes (Part Of Figure 1.1) children of root grand children of root root great grand child of root Object Number Throwable OutputStream Integer Double Exception FileOutputStream RuntimeException
  • 7. Definition • A tree t is a finite nonempty set of elements. • One of these elements is called the root. • The remaining elements, if any, are partitioned into trees, which are called the subtrees of t.
  • 8. Subtrees Object root Number Throwable OutputStream Integer Double Exception FileOutputStream RuntimeException
  • 9. Leaves Object Number Throwable OutputStream Integer Double Exception FileOutputStream RuntimeException
  • 10. Parent, Grandparent, Siblings, Ancestors, Descendants Object Number Throwable OutputStream Integer Double Exception FileOutputStream RuntimeException
  • 11. Level 3 Level 1 Level 4 Level 2 Levels Object Number Throwable OutputStream Integer Double Exception FileOutputStream RuntimeException
  • 12. Caution • Some texts start level numbers at 0 rather than at 1. • Root is at level 0. • Its children are at level 1. • The grand children of the root are at level 2. • And so on. • We shall number levels with the root at level 1.
  • 13. height = depth = number of levels Level 3 Object Number Throwable OutputStream Integer Double Exception FileOutputStream RuntimeException Level 4 Level 2 Level 1
  • 14. Node Degree = Number Of Children Object 3 Number Throwable OutputStream 2 1 1 0 0 1 0 Integer Double Exception FileOutputStream RuntimeException 0
  • 15. Tree Degree = Max Node Degree 2 1 1 0 0 1 0 Degree of tree = 3. Object 3 Number Throwable OutputStream Integer Double Exception FileOutputStream RuntimeException 0
  • 16. Binary Tree • Finite (possibly empty) collection of elements. • A nonempty binary tree has a root element. • The remaining elements (if any) are partitioned into two binary trees. • These are called the left and right subtrees of the binary tree.
  • 17. Differences Between A Tree & A Binary Tree • No node in a binary tree may have a degree more than 2, whereas there is no limit on the degree of a node in a tree. • A binary tree may be empty; a tree cannot be empty.
  • 18. Differences Between A Tree & A Binary Tree • The subtrees of a binary tree are ordered; those of a tree are not ordered. a b a b • Are different when viewed as binary trees. • Are the same when viewed as trees.
  • 19. Arithmetic Expressions • (a + b) * (c + d) + e – f/g*h + 3.25 • Expressions comprise three kinds of entities.  Operators (+, -, /, *).  Operands (a, b, c, d, e, f, g, h, 3.25, (a + b), (c + d), etc.).  Delimiters ((, )).
  • 20. Operator Degree • Number of operands that the operator requires. • Binary operator requires two operands.  a + b  c / d  e - f • Unary operator requires one operand.  + g  - h
  • 21. Infix Form • Normal way to write an expression. • Binary operators come in between their left and right operands.  a * b  a + b * c  a * b / c  (a + b) * (c + d) + e – f/g*h + 3.25
  • 22. Operator Priorities • How do you figure out the operands of an operator?  a + b * c  a * b + c / d • This is done by assigning operator priorities.  priority(*) = priority(/) > priority(+) = priority(-) • When an operand lies between two operators, the operand associates with the operator that has higher priority.
  • 23. Tie Breaker • When an operand lies between two operators that have the same priority, the operand associates with the operator on the left.  a + b - c  a * b / c / d
  • 24. Delimiters • Subexpression within delimiters is treated as a single operand, independent from the remainder of the expression.  (a + b) * (c – d) / (e – f)
  • 25. Infix Expression Is Hard To Parse • Need operator priorities, tie breaker, and delimiters. • This makes computer evaluation more difficult than is necessary. • Postfix and prefix expression forms do not rely on operator priorities, a tie breaker, or delimiters. • So it is easier for a computer to evaluate expressions that are in these forms.
  • 26. Postfix Form • The postfix form of a variable or constant is the same as its infix form.  a, b, 3.25 • The relative order of operands is the same in infix and postfix forms. • Operators come immediately after the postfix form of their operands.  Infix = a + b  Postfix = ab+
  • 27. Postfix Examples • Infix = a + b * c  Postfix = a b c * + • Infix = a * b + c  Postfix = a b * c + • Infix = (a + b) * (c – d) / (e + f)  Postfix = a b + c d - * e f + /
  • 28. Unary Operators • Replace with new symbols.  + a => a @  + a + b => a @ b +  - a => a ?  - a-b => a ? b -
  • 29. Postfix Evaluation • Scan postfix expression from left to right pushing operands on to a stack. • When an operator is encountered, pop as many operands as this operator needs; evaluate the operator; push the result on to the stack. • This works because, in postfix, operators come immediately after their operands.
  • 30. Postfix Evaluation • (a + b) * (c – d) / (e + f) • a b + c d - * e f + / • a b + c d - * e f + / a stack • a b + c d - * e f + / b • a b + c d - * e f + /
  • 31. Postfix Evaluation • (a + b) * (c – d) / (e + f) • a b + c d - * e f + / • a b + c d - * e f + / • a b + c d - * e f + / • a b + c d - * e f + / • a b + c d - * e f + / c • a b + c d - * e f + / d (a + b) stack • a b + c d - * e f + /
  • 32. Postfix Evaluation • (a + b) * (c – d) / (e + f) • a b + c d - * e f + / (a + b) stack • a b + c d - * e f + / (c – d)
  • 33. Postfix Evaluation • (a + b) * (c – d) / (e + f) • a b + c d - * e f + / • a b + c d - * e f + / • a b + c d - * e f + / f • a b + c d - * e f + / (a + b)*(c – d) stack • a b + c d - * e f + / e
  • 34. Postfix Evaluation • (a + b) * (c – d) / (e + f) • a b + c d - * e f + / (a + b)*(c – d) stack • a b + c d - * e f + / (e + f) • a b + c d - * e f + / • a b + c d - * e f + / • a b + c d - * e f + / • a b + c d - * e f + /
  • 35. Prefix Form • The prefix form of a variable or constant is the same as its infix form.  a, b, 3.25 • The relative order of operands is the same in infix and prefix forms. • Operators come immediately before the prefix form of their operands.  Infix = a + b  Postfix = ab+  Prefix = +ab
  • 36. Binary Tree Form • a + b + a b • - a - a
  • 37. Binary Tree Form • (a + b) * (c – d) / (e + f) / + a b - c d + e f * /
  • 38. Merits Of Binary Tree Form • Left and right operands are easy to visualize. • Code optimization algorithms work with the binary tree form of an expression. • Simple recursive evaluation of expression. + a b - c d + e f * /

Editor's Notes

  1. Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
  2. Branches meet at nodes.
  3. Note that it is possible to have a tree whose degree is (say) 3 and whose root has a degree that is < 3.
  4. @ and ? are new symbols that are used to denote the unary + and – operators, respectively.
  5. Each leaf represents a variable or constant. Each nonleaf represents an operator. The left operand (if any) of this operator is represented by the left subtree, the right subtree represents the right operand of the operator.
  6. Evaluate an expression– if there is no root, return null; if the root is a leaf, return the value of variable or constant in the leaf; otherwise, evaluate the left and right operands recursively; compute the root operator; return the result.