Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Árboles Binarios (UQ)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Binary

Trees
Algorithms and Data Structures COMP3506 / 7505

Binary Trees
Binary Tree Denition Properties of Proper Binary Trees Binary Tree ADT Inorder Traversal

Binary Tree Denition


A binary tree is a tree with the following properties:
Each internal node has at most two children (exactly two for proper binary trees) A nodes children are an ordered pair

Applications:
arithmetic expressions decision processes searching
A

An internal node has a left child and a right child


D

Binary Tree Recursive Denition


A binary tree is either:
a tree consisting of a single node, or a tree whose root has an ordered pair of children, each of which is a binary tree
B A

Arithmetic Expression Tree


Binary tree associated with an arithmetic expression
internal nodes: operators external nodes: operands

Example: arithmetic expression tree for the expression (2 (a - 1) + (3 b))


+

2 a -
1 3
b

Exercise: Arithmetic Expression Tree


Generate the binary tree associated with the arithmetic expression (a (4 - b) + (7 c))

Exercise: Arithmetic Expression Tree


Generate the binary tree associated with the arithmetic expression (a (4 - b) + (7 c)) Count the following:
+

a 4 -
b 7
c
Nodes Roots Leaves External nodes Internal nodes Whats the height of the tree? Whats the depth of the tree?

Examples of arithmetic expression trees


+

(2 (a - 1) + (3 b))


2 a -
1 3

2 a - 1 + 3 b

(2 a - (1 + 3) b)

Examples of arithmetic expression trees


(2 (a - 1) + (3 b))
+
-

2 a 1 3
b

2 a - 1 + 3 b

(2 a - (1 + 3) b)

Examples of arithmetic expression trees


(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

Example: dining decision


Want a fast meal?
Yes No

How about coffee?


Yes No

On expense account?
Yes No

Starbucks

Spike s

Al Forno

Caf Paragon

Properties of Proper Binary Trees


Notation
n e i h number of nodes number of external nodes number of internal nodes height

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

Linked Structure for Binary Trees


A node is represented by an object storing
Element Parent node Left child node Right child node

B A D C E

Node objects implement the Position ADT

B A C D E

Array-Based Representation of Binary Trees


Nodes are stored in an array
1
A

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?

Print Arithmetic Expressions


Specialization of an inorder traversal
print operand or operator when visiting node print ( before traversing left subtree print ) after traversing right subtree

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

Evaluate Arithmetic Expressions


Specialization of a postorder traversal
recursive method returning the value of a subtree when visiting an internal node, combine the values of the subtrees

+

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

Euler Tour Traversal


Generic traversal of a binary tree Includes a special cases the preorder, postorder and inorder traversals Walk around the tree and visit each node three times:
on the left (preorder) from below (inorder) on the right (postorder) L

+

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?

Template Method Pattern


Generic algorithm that can be specialized by redening certain steps Implemented by means of an abstract Java class Visit methods that can be redened by subclasses

Template Method Pattern


Visit methods that can be redened by subclasses Template method eulerTour
Recursively called on the left and right children A Result object with elds leftResult, rightResult and nalResult keeps track of the output of the recursive calls to eulerTour
public abstract class EulerTour { protected protected protected protected protected BinaryTree tree; void visitExternal(Position p, Result r) {} void visitLeft(Position p, Result r) {} void visitBelow(Position p, Result r) {} void visitRight(Position p, Result r) {}

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; } }

Evaluating an artihmetic expression

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

You might also like