Trees Make Money Fast Stock Fraud 2004 Goodrich

  • Slides: 20
Download presentation
Trees Make Money Fast! Stock Fraud © 2004 Goodrich, Tamassia Ponzi Scheme Trees Bank

Trees Make Money Fast! Stock Fraud © 2004 Goodrich, Tamassia Ponzi Scheme Trees Bank Robbery 1

What is a Tree In computer science, a tree is an abstract model of

What is a Tree In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child relation US Applications: n n n Computers”R”Us Sales International Organization charts File systems Europe Programming environments © 2004 Goodrich, Tamassia Manufacturing Trees Asia Laptops R&D Desktops Canada 2

Tree Terminology Root: node without parent (A) Internal node: node with at least one

Tree Terminology Root: node without parent (A) Internal node: node with at least one child (A, B, C, F) External node (a. k. a. leaf ): node without children (E, I, J, K, G, H, D) Ancestors of a node: parent, grand-grandparent, etc. Depth of a node: number of ancestors E Height of a tree: maximum depth of any node (3) Descendant of a node: child, grand-grandchild, etc. © 2004 Goodrich, Tamassia Trees Subtree: tree consisting of a node and its descendants A B C F I J G D H subtree K 3

Tree ADT (§ 6. 1. 2) We use positions to abstract nodes Generic methods:

Tree ADT (§ 6. 1. 2) We use positions to abstract nodes Generic methods: n n integer size() boolean is. Empty() Iterator elements() Iterator positions() n n n boolean is. Internal(p) boolean is. External(p) boolean is. Root(p) Update method: n position root() position parent(p) position. Iterator children(p) © 2004 Goodrich, Tamassia n n Accessor methods: n Query methods: Trees object replace (p, o) Additional update methods may be defined by data structures implementing the Tree ADT 4

Preorder Traversal A traversal visits the nodes of a tree in a systematic manner

Preorder Traversal A traversal visits the nodes of a tree in a systematic manner In a preorder traversal, a node is visited before its descendants Application: print a structured document 1 Algorithm pre. Order(v) visit(v) for each child w of v preorder (w) Make Money Fast! 2 5 1. Motivations 2. Methods 3 4 1. 1 Greed 1. 2 Avidity © 2004 Goodrich, Tamassia 9 6 7 2. 1 Stock Fraud Trees 2. 2 Ponzi Scheme References 8 2. 3 Bank Robbery 5

Postorder Traversal In a postorder traversal, a node is visited after its descendants Application:

Postorder Traversal In a postorder traversal, a node is visited after its descendants Application: compute space used by files in a directory and its subdirectories 9 Algorithm post. Order(v) for each child w of v post. Order (w) visit(v) cs 16/ 3 7 homeworks/ todo. txt 1 K programs/ 1 2 h 1 c. doc 3 K h 1 nc. doc 2 K © 2004 Goodrich, Tamassia 8 4 5 DDR. java 10 K Trees Stocks. java 25 K 6 Robot. java 20 K 6

Binary Trees (§ 6. 3) Applications: A binary tree is a tree with the

Binary Trees (§ 6. 3) Applications: A binary tree is a tree with the following properties: n n n Each internal node has at most two children (exactly two for proper binary trees) The children of a node are an ordered pair n n A We call the children of an internal node left child and right child Alternative recursive definition: a binary tree is either n n 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 arithmetic expressions decision processes searching B C D H © 2004 Goodrich, Tamassia Trees F E G I 7

Arithmetic Expression Tree Binary tree associated with an arithmetic expression n n internal nodes:

Arithmetic Expression Tree Binary tree associated with an arithmetic expression n n internal nodes: operators external nodes: operands Example: arithmetic expression tree for the expression (2 (a - 1) + (3 b)) + - 2 a © 2004 Goodrich, Tamassia 3 b 1 Trees 8

Decision Tree Binary tree associated with a decision process n n internal nodes: questions

Decision Tree Binary tree associated with a decision process n n internal nodes: questions with yes/no answer external nodes: decisions Example: dining decision Want a fast meal? No Yes How about coffee? On expense account? Yes No Starbucks Spike’s Al Forno Café Paragon © 2004 Goodrich, Tamassia Trees 9

Properties of Proper Binary Trees Notation Properties: n e = i + 1 n

Properties of Proper Binary Trees Notation Properties: n e = i + 1 n n = 2 e - 1 n h i n h (n - 1)/2 n e 2 h n h log 2 e n h log 2 (n + 1) - 1 n number of nodes e number of external nodes i number of internal nodes h height © 2004 Goodrich, Tamassia Trees 10

Binary. Tree ADT (§ 6. 3. 1) The Binary. Tree ADT extends the Tree

Binary. Tree ADT (§ 6. 3. 1) The Binary. Tree ADT extends the Tree ADT, i. e. , it inherits all the methods of the Tree ADT Additional methods: n n Update methods may be defined by data structures implementing the Binary. Tree ADT position left(p) position right(p) boolean has. Left(p) boolean has. Right(p) © 2004 Goodrich, Tamassia Trees 11

Inorder Traversal In an inorder traversal a node is visited after its left subtree

Inorder Traversal In an inorder traversal a node is visited after its left subtree and before its right subtree Application: draw a binary tree n n Algorithm in. Order(v) if has. Left (v) in. Order (left (v)) visit(v) if has. Right (v) in. Order (right (v)) x(v) = inorder rank of v y(v) = depth of v 6 2 8 1 4 3 © 2004 Goodrich, Tamassia 7 9 5 Trees 12

Print Arithmetic Expressions Specialization of an inorder traversal n n n print operand or

Print Arithmetic Expressions Specialization of an inorder traversal n n n print operand or operator when visiting node print “(“ before traversing left subtree print “)“ after traversing right subtree + - 2 a 3 Algorithm print. Expression(v) if has. Left (v) print(“(’’) in. Order (left(v)) print(v. element ()) if has. Right (v) in. Order (right(v)) print (“)’’) b ((2 (a - 1)) + (3 b)) 1 © 2004 Goodrich, Tamassia Trees 13

Evaluate Arithmetic Expressions Specialization of a postorder traversal n n recursive method returning the

Evaluate Arithmetic Expressions Specialization of a postorder traversal n n recursive method returning the value of a subtree when visiting an internal node, combine the values of the subtrees + Algorithm eval. Expr(v) if is. External (v) return v. element () else x eval. Expr(left. Child (v)) y eval. Expr(right. Child (v)) operator stored at v return x y - 2 5 © 2004 Goodrich, Tamassia 3 2 1 Trees 14

Euler Tour Traversal Generic traversal of a binary tree Includes a special cases the

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: n on the left (preorder) n from below (inorder) n on the right (postorder) + L 2 R B 5 © 2004 Goodrich, Tamassia 3 2 1 Trees 15

Template Method Pattern Generic algorithm that can be specialized by redefining certain steps Implemented

Template Method Pattern Generic algorithm that can be specialized by redefining certain steps Implemented by means of an abstract Java class Visit methods that can be redefined by subclasses Template method euler. Tour public abstract class Euler. Tour { protected Binary. Tree tree; protected void visit. External(Position p, Result r) { } protected void visit. Left(Position p, Result r) { } protected void visit. Below(Position p, Result r) { } protected void visit. Right(Position p, Result r) { } protected Object euler. Tour(Position p) { n Recursively called on the Result r = new Result(); left and right children if tree. is. External(p) { visit. External(p, r); } n A Result object with else { fields left. Result, visit. Left(p, r); right. Result and r. left. Result = euler. Tour(tree. left(p)); final. Result keeps track of visit. Below(p, r); the output of the r. right. Result = recursive calls to euler. Tour(tree. right(p)); euler. Tour Trees 16 © 2004 Goodrich, Tamassia visit. Right(p, r);

Specializations of Euler. Tour We show to specialize class Euler. Tour to evaluate an

Specializations of Euler. Tour We show to specialize class Euler. Tour to evaluate an arithmetic expression Assumptions n n External nodes store Integer objects Internal nodes store Operator objects supporting method operation (Integer, Integer) public class Evaluate. Expression extends Euler. Tour { protected void visit. External(Position p, Result r) { r. final. Result = (Integer) p. element(); } protected void visit. Right(Position p, Result r) { Operator op = (Operator) p. element(); r. final. Result = op. operation( (Integer) r. left. Result, (Integer) r. right. Result ); } … © 2004 Goodrich, Tamassia } Trees 17

Linked Structure for Trees A node is represented by an object storing n n

Linked Structure for Trees A node is represented by an object storing n n n Element Parent node Sequence of children nodes B Node objects implement the Position ADT A B D A C D F E C © 2004 Goodrich, Tamassia F Trees E 18

Linked Structure for Binary Trees A node is represented by an object storing n

Linked Structure for Binary Trees A node is represented by an object storing n n Element Parent node Left child node Right child node B Node objects implement the Position ADT B A A D C © 2004 Goodrich, Tamassia D E C Trees E 19

Array-Based Representation of Binary Trees nodes are stored in an array 1 A …

Array-Based Representation of Binary Trees nodes are stored in an array 1 A … 2 3 B n D let rank(node) be defined as follows: n n n 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 © 2004 Goodrich, Tamassia Trees 5 E 6 7 C F 10 J 11 G H 20