Trees Make Money Fast Stock Fraud 2004 Goodrich

  • Slides: 16
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 Applications: Organization charts File systems Programming environments © 2004 Goodrich, Tamassia Trees 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 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 subtree 3

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 4

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 5

Binary Trees Applications: A binary tree is a tree with the following properties: Each

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

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

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)) © 2004 Goodrich, Tamassia Trees 7

Decision Tree Binary tree associated with a decision process internal nodes: questions with yes/no

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? No Yes How about coffee? Yes No Starbucks Quizno’s © 2004 Goodrich, Tamassia On expense account? Yes Tarpy’s Trees No Café Del Monte 8

Properties of Proper Binary Trees Notation Properties: n number of nodes e number of

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

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

Print Arithmetic Expressions Specialization of an inorder traversal print operand or operator when visiting

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 print. Expression(v) if has. Left (v) print(“(’’) print. Expression (left(v)) print(v. element ()) if has. Right (v) print. Expression (right(v)) print (“)’’) ((2 (a 1)) (3 b)) © 2004 Goodrich, Tamassia Trees 11

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

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 © 2004 Goodrich, Tamassia 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 Trees 12

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

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

Linked Structure for Trees A node is represented by an object storing Element Parent node Sequence of children nodes B A B D A C D F E C © 2004 Goodrich, Tamassia F Trees E 14

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

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 © 2004 Goodrich, Tamassia E Trees 15

Array-Based Representation of Binary Trees nodes are stored in an array 1 2 3

Array-Based Representation of Binary Trees nodes are stored in an array 1 2 3 let rank(node) be defined 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), 10 rank(node) = 2*rank(parent(node))+1 © 2004 Goodrich, Tamassia Trees 5 6 7 11 16