Lec 19
Lec 19
Lec 19
branches root
branches nodes
Javas classes.
Object is at the top of the hierarchy. Subclasses of Object are next, and so on.
children of root
Number Throwable OutputStream
Integer
Double
Exception
FileOutputStream
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
Number
Throwable
OutputStream
Integer
Double
Exception
FileOutputStream
RuntimeException
Levels
Object Level 1
Level 2
Number Throwable OutputStream
Integer
Double
Exception
FileOutputStream
Level 3
RuntimeException
Level 4
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.
Level 2
Number Throwable OutputStream
Integer
Double
Exception
FileOutputStream
Level 3
RuntimeException
Level 4
2 0
Number
1 0
Throwable
1 OutputStream 0
FileOutputStream
1
Exception
Integer
Double
RuntimeException
2 0
Number
1 0
Throwable
1 OutputStream 0
FileOutputStream
1
Exception
Integer
Double
RuntimeException
Degree of tree = 3.
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.
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
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
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)
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) ab+cd-*ef+/ ab+cd-*ef+/
ab+cd-*ef+/ ab+cd-*ef+/
b a
stack
Postfix Evaluation
(a + b) * (c d) / (e + f) ab+cd-*ef+/ ab+cd-*ef+/
ab+cd-*ef+/ ab+cd-*ef+/ ab+cd-*ef+/ ab+cd-*ef+/ ab+cd-*ef+/ d c (a + b) stack
Postfix Evaluation
(a + b) * (c d) / (e + f) ab+cd-*ef+/ ab+cd-*ef+/
(c d) (a + b)
stack
Postfix Evaluation
(a + b) * (c d) / (e + f) ab+cd-*ef+/ ab+cd-*ef+/ ab+cd-*ef+/ ab+cd-*ef+/ ab+cd-*ef+/
f e (a + b)*(c d) stack
Postfix Evaluation
(a + b) * (c d) / (e + f) ab+cd-*ef+/ ab+cd-*ef+/ ab+cd-*ef+/ ab+cd-*ef+/ ab+cd-*ef+/ ab+cd-*ef+/
(e + f) (a + b)*(c d)
stack
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
+
b
*
e + -
+
f