0% found this document useful (0 votes)
78 views60 pages

Binary Tree - Expression Tree

Notes data structure

Uploaded by

jiyadubla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
78 views60 pages

Binary Tree - Expression Tree

Notes data structure

Uploaded by

jiyadubla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 60

Trees

Example Tree
root
Sami’s Home Page

Teaching Research Activities

CS101 CS211 Papers Presentations


Definitions
root - starting point (top) of the tree
parent (ancestor) - the vertex “above” this
vertex
child (descendent) - the vertices “below”
this vertex

3
Definitions
any two vertices must have one and only
one path between them else its not a tree

a tree with N nodes has N-1 edges

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

• Recursive definition: A binary tree is a finite set of


nodes that is either empty or consists of a root and
two disjoint binary trees called the left subtree and the
right subtree.

9
Example
A

E C

D
K F G

H
L

M I

10
Height of a Complete Binary Tree
L0

L1

L2

L3

At each level the number of the nodes is doubled.


total number of nodes: 1 + 2 + 22 + 23 = 24 - 1 = 15
11
Binary Tree Representations
If a complete binary tree with n nodes is
represented sequentially, then for
any node with index i, 1<=i<=n, we have:
parent(i) is at i/2 if i!=1. If i=1, i is at the root and
has no parent.
leftChild(i) is at 2i if 2i<=n. If 2i>n, then i has no
left child.
rightChild(i) is at 2i+1 if 2i +1 <=n. If 2i +1 >n,
then i has no right child.

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.

• Comparison can be reduced by using BST.


18
Binary Tree Operations
(Delete a node)
• When we delete a node, we need to consider
how we take care of the children of the
deleted node.
• This has to be done such that the property of
the binary search tree is maintained.
• Three cases:
(1) the node is a leaf
» Delete it immediately

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

A binary search tree Not a binary search tree


Binary Tree Traversals
Let L, V, and R stand for moving left, visiting
the node, and moving right.
There are six possible combinations of traversal
lVr, lrV, Vlr, Vrl, rVl, rlV

Adopt convention that we traverse left before


right, only 3 traversals remain
lVr, lrV, Vlr
inorder, postorder, preorder

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 the root of the tree first,


then visit the nodes in the left subtree,
then visit the nodes in the right subtree
Preorder(tree)
If tree is not NULL
Visit Info(tree)
Preorder(Left(tree))
Preorder(Right(tree))

(Warning: "visit" means that the algorithm does something


with the values in the node, e.g., print the value)
Preorder Traversal: J E A H T M Y

Visit first
tree

‘J’

‘E’ ‘T’

‘A’ ‘H’ ‘M’ ‘Y’

Visit left subtree second Visit right subtree last


26
Traversing a Tree Preorder

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))

(Warning: "visit" means that the algorithm does something


with the values in the node, e.g., print the value)
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
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:

• The parentheses in the expression do not appear.


• The leaves are the variables or constants in the expression.
• The non-leaf nodes are the operators in the expression:

 A node for a binary operator has two non-empty subtrees.


 A node for a unary operator has one non-empty subtree.

 The operators, constants, and variables are arranged in such


a way that an inorder traversal of the tree produces the
original expression without parentheses.

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

 The expression is ambiguous because it uses infix notation:


each operator is placed between its operands.
57
• Expression trees can be very useful for:
• Evaluation of the expression.
• Generating correct compiler code to actually compute
the expression's value at execution time.
• Performing symbolic mathematical operations (such as
differentiation) on the expression.

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 + /

You might also like