Árboles Binarios (UQ)
Árboles Binarios (UQ)
Árboles Binarios (UQ)
Trees
Algorithms
and
Data
Structures
COMP3506
/
7505
Binary
Trees
Binary
Tree
Denition
Properties
of
Proper
Binary
Trees
Binary
Tree
ADT
Inorder
Traversal
Applications:
arithmetic
expressions
decision
processes
searching
A
(2 (a - 1) + (3 b))
2 a -
1 3
2 a - 1 + 3 b
(2 a - (1 + 3) b)
2 a - 1 + 3 b
(2 a - (1 + 3) b)
2
a
-
1
+
3
b
-
2 a 1 +
3
b
(2 a - (1 + 3) b)
Decision
Tree
Binary
tree
associated
with
a
decision
process
internal
nodes:
questions
with
yes/no
answer
external
nodes:
decisions
On expense account?
Yes No
Starbucks
Spike s
Al Forno
Caf Paragon
Properties:
e
=
i
+
1
n
=
2e
-
1
h
i
h
(n
-
1)/2
e
2h
h
log2
e
h
log2
(n
+
1)
1
Level 0 1 2
BinaryTree
ADT
The
BinaryTree
ADT
extends
the
Tree
ADT,
i.e.,
it
inherits
all
the
methods
of
the
Tree
ADT
Additional
methods:
position
left(p)
position
right(p)
boolean
hasLeft(p)
boolean
hasRight(p)
Update methods may be dened by data structures implementing the BinaryTree ADT
B A D C E
B A C D E
2
B D
let rank(node) be dened as follows: 4 rank(root) = 1 if node is the left child of parent(node), rank(node) = 2*rank(parent(node)) if node is the right child of parent(node), rank(node) = 2*rank(parent(node))+1
5
E F
6
C J
10
G H
11
Inorder
Traversal
In
an
inorder
traversal
a
node
is
visited
after
its
left
subtree
and
before
its
right
subtree
Application:
Draw
a
binary
tree
x(v)
=
in-order
rank
of
v
y(v)
=
depth
of
v
Algorithm inOrder(v) if hasLeft (v) then inOrder (left (v)) visit(v) if hasRight (v) then inOrder (right (v))
8
2 1 3 4 5 7
What order would you visit the nodes in this binary tree using an inorder traversal?
What order would you visit the nodes in this binary tree using an inorder traversal?
Algorithm printExpression(v) if hasLeft (v) then print( ( ) printExpression (left(v)) print(v.element ()) if hasRight (v) then printExpression (right(v)) print ( ) )
2 a -
1 3
b ((2 (a - 1)) + (3 b))
+
2 5 -
1 3
2
Algorithm evalExpr(v) if isExternal (v) then return v.element () else x evalExpr(leftChild (v)) y evalExpr(rightChild (v)) operator stored at v return x y
+
R
-
5 1 3 2
What order would you visit the nodes in this binary tree using an Euler tour traversal?
What order would you visit the nodes in this binary tree using an Euler tour traversal?
protected Object eulerTour(Position p) { Result r = new Result(); if tree.isExternal(p) { visitExternal(p, r); } else { visitLeft(p, r); r.leftResult = eulerTour(tree.left(p)); visitBelow(p, r); r.rightResult = eulerTour(tree.right(p)); visitRight(p, r); } return r.finalResult; } }
Specializations
of
EulerTour
class EvaluateExpression extends EulerTour Specialize
EulerTour
to
public { evaluate
an
arithmetic
protected void visitExternal(Position p, Result r) expression
{ r.finalResult = (Integer) p.element(); Assumptions
External nodes store Integer objects Internal nodes store Operator objects supporting method operation (Integer, Integer)
protected void visitRight(Position p, Result r) { Operator op = (Operator) p.element(); r.finalResult = op.operation( (Integer) r.leftResult, (Integer) r.rightResult ); } }
Lecture
Summary
Binary
Tree
Denition
Properties
of
Proper
Binary
Trees
Binary
Tree
ADT
Implemented
using
links
or
array
Traversals
Inorder
Euler
tour