Binary Tree - Expression Tree
Binary Tree - Expression Tree
Example Tree
root
Sami’s Home Page
3
Definitions
any two vertices must have one and only
one path between them else its not a tree
4
Definitions
leaves (terminal nodes) - have no children
level - the number of edges between this
node and the root
ordered tree - where children’s order is
significant
5
Definitions
Depth of a node - the length of the path from
the root to that node
• root: depth 0
Height of a node - the length of the longest
path from that node to a leaf
• any leaf: height 0
Height of a tree: The length of the longest path
from the root to a leaf
6
Balanced Trees
the difference between the height of the left
sub-tree and the height of the right sub-tree is
not more than 1.
7
Trees - Example
Level
root
0 E
1 A R E
Child (of
root)
2 A S T
Leaves or
terminal nodes
3 M P L E
Depth of T: 2
Height of T: 1 8
Binary Trees
A special class of trees: max degree for each node is 2
9
Example
A
E C
D
K F G
H
L
M I
10
Height of a Complete Binary Tree
L0
L1
L2
L3
12
Sequential / Implicit Array
Representation [1] A
[2] B
A [1] A A [3] C
[2] B [4] D
[3] -- [5] E
B [4] C B C [6] F
[5] -- [7] G
[6] -- [8] H
C [7] D E F G [9] I
--
[8] D
[9] --
D . . H I
[16] E
(1) waste space
E (2) insertion/deletion
problem
13
Dynamic/ Linked
Representation
typedef struct node
{ left data right
Int data;
Struct node *left;
Struct node *right; data
} bin;
left right
14
Binary Tree Operation
• Creation of binary tree
• Insertion of a node in the tree
• Searching
• Deleting a node
15
Binary Tree Operations (create)
• The basis of our binary tree node is the following
struct declaration:
struct TreeNode
{
int value;
TreeNode *left;
TreeNode *right;
};
16
Binary Tree Operations (Insert)
• Proceed down the tree as you would with a find
• If X is found, do nothing (or update something)
• Otherwise, insert X at the last spot on the path traversed
17
Binary Search Tree (BST)
• A binary tree is useful data structure when
two way decisions must be made at each
point in a process.
• Suppose, we want to find duplicates in a list of
numbers.
• One way is to go on comparing each number with all
those that precede it. Number of comparisons
increases.
19
(2) the node has one child
– Adjust a pointer from the parent to bypass that node
20
(3) the node has 2 children
– replace the key of that node with the minimum element at the
right subtree
– delete the minimum element
• Has either no child or only right child because if it has a left child, that
left child would be smaller and would have been chosen. So invoke
case 1 or 2.
21
Binary Search Trees
23
Binary Tree - Traversal
There are six possible orderings of traversal.
By convention, we always traverse the left subtree before the
right one.
That reduces the choices to three:
• Preorder - Visit the root node first
• Inorder - Visit the left subtree, then the root
• Postorder - Visit the root node last
Preorder Traversal
Visit first
tree
‘J’
‘E’ ‘T’
B C
D E F G
H I
Result: A
Traversing a Tree Preorder
B C
D E F G
H I
Result: AB
Traversing a Tree Preorder
B C
D E F G
H I
Result: ABD
Traversing a Tree Preorder
B C
D E F G
H I
Result: ABDE
Traversing a Tree Preorder
B C
D E F G
H I
Result: ABDEH
Traversing a Tree Preorder
B C
D E F G
H I
Result: ABDEHC
Traversing a Tree Preorder
B C
D E F G
H I
Result: ABDEHCF
Traversing a Tree Preorder
B C
D E F G
H I
Result: ABDEHCFG
Traversing a Tree Preorder
B C
D E F G
H I
Result: ABDEHCFGI
Preorder Example (Visit = print)
a
b c
abc
Preorder Example (Visit = print)
a
b c
f
d e
g h i j
abdghei cf j
Preorder Traversal
Void preOrder(BinaryTreeNode
t)
{
if (t != null)
{
visit(t);
preOrder(t.leftChild);
preOrder(t.rightChild);
}
}
Inorder Traversal
• Visit the nodes in the left subtree,
then visit the root of the tree,
then visit the nodes in the right subtree
Inorder(tree)
If tree is not NULL
Inorder(Left(tree))
Visit Info(tree)
Inorder(Right(tree))
B C
D E F G
H I
Traversing a Tree Inorder
B C
D E F G
H I
Traversing a Tree Inorder
B C
D E F G
H I
Traversing a Tree Inorder
B C
D E F G
H I
Result: D
Traversing a Tree Inorder
B C
D E F G
H I
Result: DB
Traversing a Tree Inorder
B C
D E F G
H I
Result: DB
Traversing a Tree Inorder
B C
D E F G
H I
Result: DBH
Traversing a Tree Inorder
B C
D E F G
H I
Result: DBHE
Inorder Example (Visit = print)
a
b c
bac
Inorder Example (Visit = print)
a
b c
f
d e
g h i j
gdhbei af j c
Inorder Traversal
Void inOrder(BinaryTreeNode t)
{
if (t != null)
{
inOrder(t.leftChild);
visit(t);
inOrder(t.rightChild);
}
}
Tree Traversals Example
Construct Tree from given Inorder and Postorder
traversals
Search for 7 in Inorder sequence. Once we
Inorder is: 2 3 5 7 9 10 11 12 In a Postorder sequence, rightmost
know position of 7 (or index of 7) in Inorder
Postorder is: 2 5 3 9 11 12 10 7 element is the root of the tree. So
sequence, we also know that all elements on
we know 7 is root.
left side of 7 are in left subtree and elements
on right are in right subtree.
52
Construct Tree from given Preorder and Postorder
traversals
preorder = {1, 2, 4, 8, 9, 5, 3, 6, 7} Now
In
So pre[],
we know
the1 leftmost
is1 root
is root,element
elements
leftischild
{8,
root9, of
4,
Allwe know
nodes before 2 inand 2 ismust
post[] be in left
postorder = {8, 9, 4, 5, 2, 6, 7, 3, 1}; tree.
5, 2}Since
are inthe
lefttree
subtree,
is full and
and the
arrayelements
size is
subtree
more
{6, 7, than
3} are1.inThe
right
value
subtree
next to 1 in pre[],
must be left child of root.
53
Expression tree
An expression tree for an arithmetic, relational, or logical
expression is a binary tree in which:
54
Example: An Expression Tree
+
* /
-
4 9 2
5 3
(5 – 3) * 4 + 9 / 2
10-55
Expression Tree Examples
Expression Expression Tree Inorder Traversal Result
+
(a+3) a+3
a 3
+
3 -
3+(4*5-(9+6)) 3+4*5-9+6
* +
4 5 9 6
log
log(x) log x
x
!
n! n!
n
56
Why Expression trees?
Expression trees are used to remove ambiguity in
expressions.
Consider the algebraic expression 2 - 3 * 4 + 5.
Without the use of precedence rules or parentheses,
different orders of evaluation are possible:
((2-3)*(4+5)) = -9
((2-(3*4))+5) = -5
(2-((3*4)+5)) = -15
(((2-3)*4)+5) = 1
(2-(3*(4+5))) = -25
58
Expression Expression Tree Infix form
-
+ 5
2-3*4+5 2 * 2-3*4+5
3 4
*
(2 - 3) * (4 + 5) 2-3*4+5
- +
2 3 4 5
-
2 + 2-3*4+5
2 - (3 * 4 + 5) * 5
3 4
59
Constructing Expression Tree Using
Postfix Expression
/
* +
e f
+ -
a b c d
a b +c d - * e f + /