Vous êtes sur la page 1sur 5

What is a Tree

In computer science, a
tree is an abstract model Computers”R”Us
Trees of a hierarchical
structure
A tree consists of nodes Sales Manufacturing R&D
with a parent-child
Make Money Fast!
relation
US International Laptops Desktops
Applications:
„ Organization charts
Stock Ponzi Bank
Fraud Scheme Robbery „ File systems Europe Asia Canada
„ Programming
environments

© 2004 Goodrich, Tamassia Trees 1 © 2004 Goodrich, Tamassia Trees 2

Tree Terminology Tree ADT (§ 6.1.2)


We use positions to abstract Query methods:
Root: node without parent (A) Subtree: tree consisting of
a node and its nodes boolean isInternal(p)
Internal node: node with at least „

one child (A, B, C, F) descendants Generic methods: „ boolean isExternal(p)


External node (a.k.a. leaf ): node „ integer size() „ boolean isRoot(p)
A
without children (E, I, J, K, G, H, D) „ boolean isEmpty() Update method:
Ancestors of a node: parent, „ Iterator elements() „ object replace (p, o)
grandparent, grand-grandparent, B C D „ Iterator positions() Additional update methods
etc. Accessor methods: may be defined by data
Depth of a node: number of „ position root() structures implementing the
ancestors E F G H Tree ADT
„ position parent(p)
Height of a tree: maximum depth
of any node (3) „ positionIterator children(p)
subtree
Descendant of a node: child, I J K
grandchild, grand-grandchild, etc.

© 2004 Goodrich, Tamassia Trees 3 © 2004 Goodrich, Tamassia Trees 4


Preorder Traversal Postorder Traversal
A traversal visits the nodes of a Algorithm preOrder(v) In a postorder traversal, a Algorithm postOrder(v)
tree in a systematic manner node is visited after its
visit(v) descendants for each child w of v
In a preorder traversal, a node is
visited before its descendants for each child w of v Application: compute space postOrder (w)
Application: print a structured preorder (w) used by files in a directory and visit(v)
document its subdirectories
9
1 cs16/
Make Money Fast!
8
3 7
2 5 9 todo.txt
homeworks/ programs/
1. Motivations 2. Methods References 1K

6 7 8 1 2 4 5 6
3 4
2.1 Stock 2.2 Ponzi 2.3 Bank h1c.doc h1nc.doc DDR.java Stocks.java Robot.java
1.1 Greed 1.2 Avidity
Fraud Scheme Robbery 3K 2K 10K 25K 20K

© 2004 Goodrich, Tamassia Trees 5 © 2004 Goodrich, Tamassia Trees 6

Binary Trees (§ 6.3) Arithmetic Expression Tree


A binary tree is a tree with the Applications: Binary tree associated with an arithmetic expression
following properties: „ arithmetic expressions „ internal nodes: operators
„ Each internal node has at most two „ decision processes „ external nodes: operands
children (exactly two for proper
searching
binary trees) „
Example: arithmetic expression tree for the
„ The children of a node are an A expression (2 × (a − 1) + (3 × b))
ordered pair
We call the children of an internal
node left child and right child +
Alternative recursive definition: a B C
binary tree is either × ×
„ a tree consisting of a single node, or
„ a tree whose root has an ordered D E F G 2 − 3 b
pair of children, each of which is a
binary tree
a 1
H I

© 2004 Goodrich, Tamassia Trees 7 © 2004 Goodrich, Tamassia Trees 8


Decision Tree Properties of Proper Binary Trees
Binary tree associated with a decision process Notation Properties:
„ internal nodes: questions with yes/no answer n number of nodes „ e = i + 1
external nodes: decisions e number of
„ n = 2e − 1
„

Example: dining decision external nodes


„ h ≤ i
i number of internal
nodes „ h ≤ (n − 1)/2
Want a fast meal?
h height „ e ≤ 2
h
Yes No
„ h ≥ log2 e
How about coffee? On expense account? „ h ≥ log2 (n + 1) − 1

Yes No Yes No

Starbucks Spike’s Al Forno Café Paragon

© 2004 Goodrich, Tamassia Trees 9 © 2004 Goodrich, Tamassia Trees 10

BinaryTree ADT (§ 6.3.1) Inorder Traversal


In an inorder traversal a Algorithm inOrder(v)
The BinaryTree ADT Update methods node is visited after its left
if hasLeft (v)
subtree and before its right
extends the Tree may be defined by subtree inOrder (left (v))
ADT, i.e., it inherits data structures Application: draw a binary visit(v)
tree
all the methods of implementing the „ x(v) = inorder rank of v if hasRight (v)
the Tree ADT BinaryTree ADT „ y(v) = depth of v 6 inOrder (right (v))
Additional methods: 2 8
„ position left(p)
1 4 7 9
„ position right(p)
„ boolean hasLeft(p) 3 5
„ boolean hasRight(p)
© 2004 Goodrich, Tamassia Trees 11 © 2004 Goodrich, Tamassia Trees 12
Print Arithmetic Expressions Evaluate Arithmetic Expressions
Specialization of an inorder Algorithm printExpression(v) Specialization of a postorder Algorithm evalExpr(v)
traversal traversal if isExternal (v)
print operand or operator
if hasLeft (v)
„
print(“(’’) „ recursive method returning return v.element ()
when visiting node
the value of a subtree else
print “(“ before traversing left
„
subtree
inOrder (left(v)) when visiting an internal
„
x ← evalExpr(leftChild (v))
„ print “)“ after traversing right print(v.element ()) node, combine the values
y ← evalExpr(rightChild (v))
subtree of the subtrees
if hasRight (v) ◊ ← operator stored at v
+ inOrder (right(v)) + return x ◊ y

× × print (“)’’)
× ×
2 − 3 b −
2 3 2
((2 × (a − 1)) + (3 × b))
a 1
5 1
© 2004 Goodrich, Tamassia Trees 13 © 2004 Goodrich, Tamassia Trees 14

Euler Tour Traversal Template Method Pattern


Generic traversal of a binary tree Generic algorithm that public abstract class EulerTour {
Includes a special cases the preorder, postorder and inorder traversals can be specialized by protected BinaryTree tree;
redefining certain steps protected void visitExternal(Position p, Result r) { }
Walk around the tree and visit each node three times: protected void visitLeft(Position p, Result r) { }
Implemented by means of
„ on the left (preorder) protected void visitBelow(Position p, Result r) { }
an abstract Java class
„ from below (inorder) protected void visitRight(Position p, Result r) { }
Visit methods that can be protected Object eulerTour(Position p) {
„ on the right (postorder)
redefined by subclasses Result r = new Result();
+ Template method eulerTour if tree.isExternal(p) { visitExternal(p, r); }
„ Recursively called on the else {
L × R × left and right children visitLeft(p, r);
B „ A Result object with fields r.leftResult = eulerTour(tree.left(p));
2 − 3 2 leftResult, rightResult and visitBelow(p, r);
finalResult keeps track of r.rightResult = eulerTour(tree.right(p));
the output of the visitRight(p, r);
5 1 recursive calls to eulerTour
return r.finalResult;
}…

© 2004 Goodrich, Tamassia Trees 15 © 2004 Goodrich, Tamassia Trees 16


Specializations of EulerTour Linked Structure for Trees
A node is represented by
We show how to public class EvaluateExpression an object storing
extends EulerTour { ∅
specialize class „ Element
EulerTour to evaluate protected void visitExternal(Position p, Result r) { „ Parent node
Sequence of children B
an arithmetic r.finalResult = (Integer) p.element(); „

} nodes
expression
Node objects implement ∅ ∅
Assumptions protected void visitRight(Position p, Result r) { the Position ADT
External nodes store Operator op = (Operator) p.element();
„
r.finalResult = op.operation( A D F
Integer objects B
(Integer) r.leftResult,
„ Internal nodes store
(Integer) r.rightResult
Operator objects
); D
supporting method
}
A F
operation (Integer, Integer)

C E ∅ ∅
}
C E
© 2004 Goodrich, Tamassia Trees 17 © 2004 Goodrich, Tamassia Trees 18

Array-Based Representation of
Linked Structure for Binary Trees Binary Trees
A node is represented
by an object storing ∅ nodes are stored in an array
„ Element 1
„ Parent node A

„ Left child node B …


„ Right child node
Node objects implement 2 3
∅ ∅
the Position ADT B D

B A D „ let rank(node) be defined as follows:


„ rank(root) = 1 4 5 6 7
A D ∅ ∅ ∅ ∅ „ if node is the left child of parent(node), E F C J

rank(node) = 2*rank(parent(node))
„ if node is the right child of parent(node),
C E C E rank(node) = 2*rank(parent(node))+1
10 11
G H
© 2004 Goodrich, Tamassia Trees 19 © 2004 Goodrich, Tamassia Trees 20

Vous aimerez peut-être aussi