0% found this document useful (0 votes)
97 views43 pages

Discrete Mathematics

A binary search tree (BST) is a binary tree where the key in each internal node is greater than or equal to any key in its left subtree and less than or equal to any key in its right subtree. Inserting a node into a BST involves comparing its key to the root node's key and inserting it in the left or right subtree accordingly. Traversing a BST can be done preorder, inorder, or postorder by recursively visiting nodes in a specified order.

Uploaded by

Isif Sethi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views43 pages

Discrete Mathematics

A binary search tree (BST) is a binary tree where the key in each internal node is greater than or equal to any key in its left subtree and less than or equal to any key in its right subtree. Inserting a node into a BST involves comparing its key to the root node's key and inserting it in the left or right subtree accordingly. Traversing a BST can be done preorder, inorder, or postorder by recursively visiting nodes in a specified order.

Uploaded by

Isif Sethi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 43

Discrete Mathematics

Trees
Searching takes time . . .
So the goal in computer programs is to find
any stored item efficiently when all stored
items are ordered.

A Binary Search Tree can be used to store


items in its vertices. It enables efficient
searches.

2
A Binary Search Tree (BST) is . . .
A special kind of binary tree in which:
1. Each vertex contains a distinct key value,
2. The key values in the tree can be compared using
“greater than” and “less than”, and
3. The key value of each vertex in the tree is
less than every key value in its right subtree, and
greater than every key value in its left subtree.
Shape of a binary search tree . . .
Depends on its key values and their order of insertion.
Insert the elements ‘J’ ‘E’ ‘F’ ‘T’ ‘A’ in that order.
The first value to be inserted is put into the root.

‘J’

4
Inserting ‘E’ into the BST
Thereafter, each value to be inserted begins by comparing
itself to the value in the root, moving left it is less, or
moving right if it is greater. This continues at each
level until it can be inserted as a new leaf.

‘J’

‘E’

5
Inserting ‘F’ into the BST

Begin by comparing ‘F’ to the value in the root,


moving left it is less, or moving right if it is greater.
This continues until it can be inserted as a leaf.

‘J’

‘E’
‘F’

6
Inserting ‘T’ into the BST

Begin by comparing ‘T’ to the value in the root, moving left


it is less, or moving right if it is greater. This continues
until it can be inserted as a leaf.

‘J’

‘E’ ‘T’

‘F’

7
Inserting ‘A’ into the BST

Begin by comparing ‘A’ to the value in the root, moving left


it is less, or moving right if it is greater. This continues
until it can be inserted as a leaf.

‘J’

‘E’ ‘T’

‘A’ ‘F’

8
What binary search tree . . .
is obtained by inserting
the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order?

‘A’

9
Binary search tree . . .
obtained by inserting
the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order.

‘A’
‘E’
‘F’
‘J’
‘T’

10
Another binary search tree
‘J’

‘E’ ‘T’

‘A’ ‘H’ ‘M’

‘K’ ‘P’

11
Add nodes containing these values in this order:
‘D’ ‘B’ ‘L’ ‘Q’ ‘S’ ‘V’ ‘Z’

‘J’

‘E’ ‘T’

‘A’ ‘H’ ‘M’ ‘V’

‘D’ ‘K’ ‘P’ ‘Z’

‘B’ ‘L’ ‘Q’

‘S’
12
Is ‘F’ existing in Tree?

‘J’

‘E’ ‘T’

‘A’ ‘H’ ‘M’ ‘V’

‘D’ ‘K’ ‘P’ ‘Z’

‘B’ ‘L’ ‘Q’

‘S’
13
BST for mathematics, physics, geography, zoology,
meteorology,
geology, psychology, and chemistry

14
BST for mathematics, physics, geography, zoology,
meteorology,
geology, psychology, and chemistry

15
BST for mathematics, physics, geography, zoology,
meteorology,
geology, psychology, and chemistry

16
BST for mathematics, physics, geography, zoology,
meteorology,
geology, psychology, and chemistry

17
BST for mathematics, physics, geography, zoology,
meteorology,
geology, psychology, and chemistry

18
BST for mathematics, physics, geography, zoology,
meteorology,
geology, psychology, and chemistry

19
BST for mathematics, physics, geography, zoology,
meteorology,
geology, psychology, and chemistry

20
BST for mathematics, physics, geography, zoology,
meteorology,
geology, psychology, and chemistry

21
Tree Traversal
 Tree traversal is a procedure for
systematically visiting every vertex of
an ordered binary tree.
 Tree traversals are defined recursively.
 Three traversals are named:
preorder,
inorder,
postorder.
22
PREORDER Traversal
Let T be an ordered binary tree with root r.
The preorder traversal begins:
by visiting root first.
Then traverses left subtree in preorder
Then traverses right subtree in preorder.

23
Preorder Traversal: J E A H T M Y
Visit first.
ROOT

‘J’

‘E’ ‘T’

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

Visit left subtree second Visit right subtree last


24
INORDER Traversal
Let T be an ordered binary tree with root r.

The inorder traversal begins:


By traversing left subtree in inorder.
Then visits root
Then traverses right subtree in inorder.

25
Inorder Traversal: A E H J M T Y
Visit second

ROOT
‘J’

‘E’ ‘T’

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

Visit left subtree first Visit right subtree last


26
POSTORDER Traversal
Let T be an ordered binary tree with root r.
The postorder traversal begins:
By traversing left subtree in Postorder
Then traverses right subtree in Postorder
Then ends by visiting r.

27
Postorder Traversal: A H E M Y T J
Visit last

ROOT
‘J’

‘E’ ‘T’

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

Visit left subtree first Visit right subtree second


28
Do it Yourself

29
Results

Pre-order

In-order

Post-order

30
A Binary Expression Tree is . . .
A special kind of binary tree in which:
1. Each leaf node contains a single operand,
2. Each nonleaf node contains a single binary
operator, and
3. The left and right subtrees of an operator
node represent subexpressions that must be
evaluated before applying the operator at
the root of the subtree.

31
A Binary Expression Tree

ROOT

‘-’

‘8’ ‘5’

INORDER TRAVERSAL: 8 - 5 has value 3

PREORDER TRAVERSAL: - 8 5

POSTORDER TRAVERSAL: 8 5 -
32
A Binary Expression Tree

‘*’

‘+’ ‘3’

‘4’ ‘2’

What value does it have?

( 4 + 2 ) * 3 = 18
33
A Binary Expression Tree

‘*’

‘+’ ‘3’

‘4’ ‘2’

What infix, prefix, postfix expressions does it represent?

34
A Binary Expression Tree

‘*’

‘+’ ‘3’

‘4’ ‘2’

Infix: ((4+2)*3)
Prefix: * + 4 2 3 evaluate from right
Postfix: 4 2 + 3 * evaluate from left
35
Levels Indicate Precedence
When a binary expression tree is used to
represent an expression, the levels of the
nodes in the tree indicate their relative
precedence of evaluation.

Operations at higher levels of the tree are


evaluated later than those below them.
The operation at the root is always the
last operation performed.

36
Evaluate
this binary expression tree
‘*’

‘-’ ‘/’

‘8’ ‘5’ ‘+’ ‘3’

‘4’ ‘2’

What infix, prefix, postfix expressions does it represent?


37
A binary expression tree
‘*’

‘-’ ‘/’

‘8’ ‘5’ ‘+’ ‘3’

‘4’ ‘2’

Infix: ((8-5)*((4+2)/3))
Prefix: *-85 /+423
Postfix: 85- 42+3/*
38
A binary expression tree
‘*’

‘-’ ‘/’

‘8’ ‘5’ ‘+’ ‘3’

‘4’ ‘2’

Infix: ((8-5)*((4+2)/3))
Prefix: *-85 /+423 evaluate from right
Postfix: 85- 42+3/* evaluate from left
39
((8-5)*((4+2)/3))

= ((3) * ((6)/3))
= (3 * (2))
=6

40
*-85 /+423

= * - 8 5 / (4 +2) 3
=*-85/63
= * - 8 5 (6 / 3)
=*-852
= * (8 – 5) 2
=*32
=3*2
=6
41
85- 42+3/*

= (8 – 5) 4 2 + 3 / *
=342+3/*
= 3 (4 + 2) 3 / *
=363/*
= 3 (6 / 3) *
=32*
=3*2
=6
42
References

 Discrete Mathematics’ by Rosen, 7th


Edition
 Section 11.2
 Section 11.3

43

You might also like