Documente Academic
Documente Profesional
Documente Cultură
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
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
Yes No Yes No
× × 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
} 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
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