Trees Make Money Fast! Stock Ponzi Bank Robbery Fraud Scheme - - PowerPoint PPT Presentation

trees
SMART_READER_LITE
LIVE PREVIEW

Trees Make Money Fast! Stock Ponzi Bank Robbery Fraud Scheme - - PowerPoint PPT Presentation

Trees Make Money Fast! Stock Ponzi Bank Robbery Fraud Scheme 02/06/2006 Trees 1 Outline and Reading Tree ADT (7.1.2) Preorder and postorder traversals (7.2.2-3) BinaryTree ADT (7.3.1) Inorder traversal (7.3.6) Euler Tour


slide-1
SLIDE 1

02/06/2006 Trees 1

Trees

Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery

slide-2
SLIDE 2

2/6/2006 Trees 2

Outline and Reading

Tree ADT (§7.1.2) Preorder and postorder traversals (§7.2.2-3) BinaryTree ADT (§7.3.1) Inorder traversal (§7.3.6) Euler Tour traversal (§7.3.6) Template method pattern (§7.3.7) Data structures for trees (§7.3.4-5)

slide-3
SLIDE 3

2/6/2006 Trees 3

What is a Tree

In computer science, a tree is an abstract model

  • f a hierarchical

structure A tree consists of nodes with a parent-child relation Applications:

Organization charts File systems Programming

environments

Computers”R”Us Sales R&D Manufacturing Laptops Desktops US International Europe Asia Canada

slide-4
SLIDE 4

2/6/2006 Trees 4

subtree

Tree Terminology

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

  • ne 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, grandparent, grand-grandparent, etc. Depth of a node: number of ancestors Height of a tree: maximum depth

  • f any node (3)

Descendant of a node: child, grandchild, grand-grandchild, etc. Subtree: tree consisting of a node and its descendants

A B D C G H E F I J K

slide-5
SLIDE 5

2/6/2006 Trees 5

Tree ADT

We use positions to abstract nodes Generic methods:

integer size() boolean isEmpty()

  • bjectIterator elements()

positionIterator positions()

Accessor methods:

position root() position parent(p) positionIterator children(p)

Query methods:

boolean isInternal(p) boolean isExternal(p) boolean isRoot(p)

Update methods:

swapElements(p, q)

  • bject replaceElement(p, o)

Additional update methods may be defined by data structures implementing the Tree ADT

slide-6
SLIDE 6

2/6/2006 Trees 6

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 (like the cs16 book)

Make Money Fast!

  • 1. Motivations

References

  • 2. Methods

2.1 Stock Fraud 2.2 Ponzi Scheme 1.1 Greed 1.2 Avidity 2.3 Bank Robbery

1 2 3 5 4 6 7 8 9

Algorithm preOrder(v) visit(v) for each child w of v preorder (w)

slide-7
SLIDE 7

2/6/2006 Trees 7

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

Algorithm postOrder(v) for each child w of v postOrder (w) visit(v)

cs16/ homeworks/ todo.txt 1K programs/ DDR.java 10K Stocks.java 25K h1c.doc 3K h1nc.doc 2K Robot.java 20K

9 3 1 7 2 4 5 6 8

slide-8
SLIDE 8

2/6/2006 Trees 8

Binary Tree

A binary tree is a tree with the following properties:

Each internal node has two

children

The children of a node are an

  • rdered pair

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,

  • r

a tree whose root has an ordered

pair of children, each of which is a binary tree

Applications:

arithmetic expressions decision processes searching

A B C F G D E H I

slide-9
SLIDE 9

2/6/2006 Trees 9

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

slide-10
SLIDE 10

2/6/2006 Trees 10

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? How about coffee? On expense account? Starbucks Spike’s Al Forno Café Paragon

Yes No Yes No Yes No

slide-11
SLIDE 11

2/6/2006 Trees 11

Properties of Binary Trees

Notation

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

Properties:

e = i + 1 n = 2e − 1 h ≤

≤ i

h ≤

≤ (n − 1)/2

e ≤ 2h h ≥ log2 e h ≥ log2 (n + 1) − 1

slide-12
SLIDE 12

2/6/2006 Trees 12

BinaryTree ADT

The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree ADT Additional methods:

position leftChild(p) position rightChild(p) position sibling(p)

Update methods may be defined by data structures implementing the BinaryTree ADT

slide-13
SLIDE 13

2/6/2006 Trees 13

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) = inorder rank of v y(v) = depth of v

Algorithm inOrder(v) if isInternal (v) inOrder (leftChild (v)) visit(v) if isInternal (v) inOrder (rightChild (v))

3 1 2 5 6 7 9 8 4

slide-14
SLIDE 14

2/6/2006 Trees 14

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 isInternal (v) print(“(’’) inOrder (leftChild (v)) print(v.element ()) if isInternal (v) inOrder (rightChild (v)) print (“)’’) ((2 × (a − 1)) + (3 × b)) + × × − 2 a 1 3 b

slide-15
SLIDE 15

2/6/2006 Trees 15

Evaluate Arithmetic Expressions

Specialization of a postorder traversal

recursive method returning

the value of a subtree

when visiting an internal

node, combine the values

  • f the subtrees

Algorithm evalExpr(v) if isExternal (v) return v.element () else x ← evalExpr(leftChild (v)) y ← evalExpr(rightChild (v)) ◊ ← operator stored at v return x ◊ y

+ × × − 2 5 1 3 2

slide-16
SLIDE 16

2/6/2006 Trees 16

Euler Tour Traversal

Generic traversal of a binary tree Includes as special cases the preorder, postorder and inorder traversals Walk around the tree and visit each node three times:

  • n the left (preorder)

from below (inorder)

  • n the right (postorder)

+ × − 2 5 1 3 2

L B R

×

slide-17
SLIDE 17

2/6/2006 Trees 17

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 eulerTour

Recursively called on the

left and right children

A Result object with fields

leftResult, rightResult and finalResult keeps track of the output of the recursive calls to eulerTour

public abstract class EulerTour<E,R> { protected BinaryTree<E> tree; public abstract R execute(BinaryTree<E> t); protected void init(BinaryTree<E> t){ tree = t;} protected void visitLeft(Position<E> v, TourResult<R> r) { } protected void visitBelow(Position<E> v, TourResult<R> r){ } protected void visitRight(Position<E> v, TourResult<R> r) { } protected E eulerTour(Position<E> v) { TourResult<R> r = new TourResult<R>( ); visitLeft(v,r) ; if (tree.hasLeft(v)) r.left=eulerTour(tree.left(v)); visitBelow(v, r); if (tree.hasRight(v)) r.left=eulerTour(tree.right(v)); visitRight(v, r); return r.out; } …

slide-18
SLIDE 18

2/6/2006 Trees 18

Specializations of EulerTour

We show how to specialize class EulerTour to evaluate an arithmetic expression Assumptions

External nodes store

Integer objects

Internal nodes store

ExpressionOperator

  • bjects supporting

method setOperands (Integer, Integer) and getValue()

public class EvaluateExpressionTour extends EulerTour<ExpressionTerm,Integer> { public Integer execute(BinaryTree<ExpressionTerm> T){ init(T); return eulerTour(tree.root()); } protected void visitRight(Position<ExpressionTerm> v, TourResult<Integer> r) { ExpressionTerm term = v.element(); if (tree.isInternal(v)){ ExpressionOperator op = (ExpressionOperator) term;

  • p.setOperands(r.left, r.right);

} r.out = term.getValue(); } } …

slide-19
SLIDE 19

2/6/2006 Trees 19

Data Structure for Trees

A node is represented by an object storing

  • Element
  • Parent node
  • Sequence of children

nodes

Node objects implement the Position ADT

B

∅ ∅

A D F

C

E

B D A C E F

slide-20
SLIDE 20

2/6/2006 Trees 20

Data Structure for Binary Trees

A node is represented by an object storing

  • Element
  • Parent node
  • Left child node
  • Right child node

Node objects implement the Position ADT

E

∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ B A D C E ∅

B D A C

slide-21
SLIDE 21

2/6/2006 Trees 21

Java Implementation

Tree interface BinaryTree interface extending Tree Classes implementing Tree and BinaryTree and providing

Constructors Update methods Print methods

Examples of updates for binary trees

expandExternal(v) removeAboveExternal(w)

A ∅ ∅

expandExternal(v)

A C B B

removeAboveExternal(w)

A

v v w

slide-22
SLIDE 22

2/6/2006 Trees 22

Trees in net.datastructures

NDS4 is the Library of Data Structures in Java Tree interfaces in NDS4

CompleteBinaryTree BinaryTree Tree

Tree classes in JDSL

LinkedBinaryTree ArrayListCompleteBinaryTree AVLTree BinarySearchTree LinkedTree RBTree

NDS4 was developed at Brown and UC Irvine See the JDSL documentation and tutorials at http://net3.datastructures.net /doc4/index.html

LinkedTree LinkedBinaryTree Tree BinaryTree