CH 7-Trees
CH 7-Trees
• Properties:
o There is a unique simple path between any 2
of its vertices.
o No loops.
o No multiple edges.
Example 1
●a
●b
c● ●d
e● ●f
a b
d
c
e
f
b
c d
f g
e
y
• Root: Vertex with in-degree 0
[Node a is the root]
Definitions
y
Definitions
b c
a
Left subtree of g Right subtree of g
b g
h j
c e h j
m
k n
d f k n m
• Balanced Tree
A rooted m-ary tree of height h is balanced if all
leaves are at levels h or h - 1.
• Traversal algorithms
o Pre-order traversal
o In-order traversal
o Post-order traversal
• In-order traversal
• Post-order traversal
Tree Traversals
Pre-order: a b e j k n o p f c d g l m h i
In-order: j e n k o p b f a c l g m d h i
Post-order: j n o p k e f b c l m g h i d a
Infix, Prefix, and Postfix Notation
• Prefix Notation :
In this notation, operator is prefixed to operands, i.e.
operator is written ahead of operands.
• For example, +ab. This is equivalent to its infix
notation a + b.
• Prefix notation is also known as Polish Notation.
3/15/2022 27
Infix, Prefix, and Postfix Notation
• Postfix Notation:
This notation style is known as Reversed Polish
Notation.
• In this notation style, the operator is postfixed to
the operands i.e., the operator is written after the
operands.
For example, ab+. This is equivalent to its infix
notation a + b.
3/15/2022 28
Infix, Prefix, and Postfix Notation
The following table briefly tries to show the difference in all three notations −
2 (a + b) ∗ c ∗+abc ab+c∗
3 a ∗ (b + c) ∗a+bc abc+∗
5 (a + b) ∗ (c + d) ∗+ab+cd ab+cd+∗
3/15/2022 29
Infix, Prefix, and Postfix Notation
Prefix: + ^ + x y 2 / – x 4 3
Postfix: x y + 2 ^ x 4 – 3 / +
3/15/2022
Infix, Prefix, and Postfix Notation
3/15/2022 34