Documente Academic
Documente Profesional
Documente Cultură
Trees
Searching takes time . . .
So the goal in computer programs is to find
any stored item efficiently when all stored
items are ordered.
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
‘J’
‘E’
‘F’
6
Inserting ‘T’ into the BST
‘J’
‘E’ ‘T’
‘F’
7
Inserting ‘A’ into the BST
‘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’
‘K’ ‘P’
11
Add nodes containing these values in this order:
‘D’ ‘B’ ‘L’ ‘Q’ ‘S’ ‘V’ ‘Z’
‘J’
‘E’ ‘T’
‘S’
12
Is ‘F’ existing in Tree?
‘J’
‘E’ ‘T’
‘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’
25
Inorder Traversal: A E H J M T Y
Visit second
ROOT
‘J’
‘E’ ‘T’
27
Postorder Traversal: A H E M Y T J
Visit last
ROOT
‘J’
‘E’ ‘T’
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’
PREORDER TRAVERSAL: - 8 5
POSTORDER TRAVERSAL: 8 5 -
32
A Binary Expression Tree
‘*’
‘+’ ‘3’
‘4’ ‘2’
( 4 + 2 ) * 3 = 18
33
A Binary Expression Tree
‘*’
‘+’ ‘3’
‘4’ ‘2’
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.
36
Evaluate
this binary expression tree
‘*’
‘-’ ‘/’
‘4’ ‘2’
‘-’ ‘/’
‘4’ ‘2’
Infix: ((8-5)*((4+2)/3))
Prefix: *-85 /+423
Postfix: 85- 42+3/*
38
A binary expression tree
‘*’
‘-’ ‘/’
‘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
43