Trees 120507234509 Phpapp01
Trees 120507234509 Phpapp01
TREES
1
Tree
2
Which graphs are trees?
a) b)
c)
3
Is it a tree?
NO!
yes! All the nodes are yes! (but not
not connected a binary tree)
NO!
There is a cycle yes! (it’s actually
and an extra the same graph as
edge (5 nodes the blue one) – but
and 5 edges) usually we draw
tree by its “levels”4
Rooted Trees
d e f g
leaf
siblings
h i
7
a
b c
d e f g
ancestors of h and i
h i
The level of a vertex v in a rooted tree is
the length of the unique path from the root
to this vertex.
level 2
level 3
a
b c
d e f g
12
Properties of Trees
B C right child of A
left subtree
of A D E F G
right
subtree of
H C 17
I J
Ordered Binary Tree
18
An Ordered Binary Tree
A
B
E C
K F G D
L H
M I
J
19
Is this binary tree balanced?
A rooted binary tree of height
Lou H is called balanced if all its
leaves are at levels H or H-1.
Hal Max
Ed Ken Sue
Joe Ted
20
Searching takes time . . .
So the goal in computer programs is to find
any stored item efficiently when all stored
items are ordered.
21
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.
A Binary Expression Tree is . . .
A special kind of binary tree in which:
l Each leaf node contains a single operand,
l Each nonleaf node contains a single binary
operator, and
l 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.
23
Expression Tree
● Each node contains an
operator or an operand
● Operands are stored in
leaf nodes
● Parentheses are not stored (x + y)*((a + b)/c)
in the tree because the tree structure
dictates the order of operand evaluation
● Operators in nodes at higher levels are
evaluated after operators in nodes at lower
levels
24
A Binary Expression Tree
‘*’
‘+’ ‘3’
‘4’ ‘2’
( 4 + 2 ) * 3 = 18
25
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
26
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.
27
Evaluate
this binary expression tree
‘*’
‘-’ ‘/’
‘4’ ‘2’
‘-’ ‘/’
‘4’ ‘2’
Infix: ((8-5)*((4+2)/3))
Prefix: *-85 /+423 evaluate from right
Postfix: 85- 42+3/* evaluate from left
29
Binary Tree for Expressions
30
Complete Binary Tree
Also be defined as a full binary tree in which all
leaves are at depth n or n-1 for some n.
B B C
C
D F G D E F G
E
H I J K L M N O
H I 31
Complete binary tree Full binary tree of depth 4
Difference between binary and
complete binary tree
BINARY TREE ISN'T NECESSARY THAT ALL OF
LEAF NODE IN SAME LEVEL BUT
COMPLETE BINARY TREE MUST HAVE ALL LEAF
NODE IN SAME LEVEL.
Binary Tree
32
Complete Binary Tree
SPANNING TREES
We can delete any edge without deleting any vertex (to remove
the cycle), but leave the graph connected.
34
PRIM’S ALGORITHM
35
EXAMPLE
PRIM’S ALGORITHM
Use Prim’s algorithm to find a minimum spanning tree
in the weighted graph below:
a 2 b 3 c 1 d
3 1 2 5
e 4 f 3 g 3 h
4 2 4 3
i 3 j 3 k 1 l 36
SOLUTION
38
Kruskal’s Algorithm
a 2 b 3 c 1 d
3 1 2 5
e 4 f 3 g 3 h
4 2 4 3
i 3 j 3 k 1 l 40
SOLUTION
●
You must visit each city once and only once
●
You must return to the original starting point
42
TSP
TSP is similar to these variations of Hamiltonian Circuit
problems:
●
Find the shortest Hamiltonian cycle in a
weighted graph.
●
Find the Hamiltonian cycle in a weighted
graph with the minimal length of the
longest edge. (bottleneck TSP).
A route returning to the beginning is known as a
Hamiltonian Circuit
44