trees
play

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


  1. Trees Make Money Fast! Stock Ponzi Bank Robbery Fraud Scheme 02/06/2006 Trees 1

  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) 2/6/2006 Trees 2

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

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

  5. Tree ADT We use positions to abstract Query methods: nodes � boolean isInternal(p) Generic methods: � boolean isExternal(p) � integer size() � boolean isRoot(p) � boolean isEmpty() Update methods: � objectIterator elements() � swapElements(p, q) � positionIterator positions() � object replaceElement(p, o) Accessor methods: Additional update methods may be defined by data � position root() structures implementing the � position parent(p) Tree ADT � positionIterator children(p) 2/6/2006 Trees 5

  6. Preorder Traversal A traversal visits the nodes of a Algorithm preOrder ( v ) tree in a systematic manner visit ( v ) In a preorder traversal, a node is for each child w of v visited before its descendants preorder ( w ) Application: print a structured document (like the cs16 book) 1 Make Money Fast! 2 5 9 1. Motivations 2. Methods References 6 7 8 3 4 2.1 Stock 2.2 Ponzi 2.3 Bank 1.1 Greed 1.2 Avidity Fraud Scheme Robbery 2/6/2006 Trees 6

  7. Postorder Traversal In a postorder traversal, a Algorithm postOrder ( v ) node is visited after its for each child w of v descendants postOrder ( w ) Application: compute space used by files in a directory and visit ( v ) its subdirectories 9 cs16/ 8 3 7 todo.txt homeworks/ programs/ 1K 4 5 6 1 2 h1c.doc h1nc.doc DDR.java Stocks.java Robot.java 3K 2K 10K 25K 20K 2/6/2006 Trees 7

  8. Binary Tree A binary tree is a tree with the Applications: following properties: � arithmetic expressions � Each internal node has two � decision processes children � searching � 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 B C binary tree is either � a tree consisting of a single node, or D E F G � a tree whose root has an ordered pair of children, each of which is a binary tree H I 2/6/2006 Trees 8

  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 3 b a 1 2/6/2006 Trees 9

  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? Yes No How about coffee? On expense account? Yes No Yes No Starbucks Spike’s Al Forno Café Paragon 2/6/2006 Trees 10

  11. Properties of Binary Trees Notation Properties: n number of nodes � e = i + 1 e number of � n = 2 e − 1 external nodes � h ≤ ≤ i number of internal i � h ≤ ≤ ( n − 1) / 2 nodes h height � e ≤ 2 h � h ≥ log 2 e � h ≥ log 2 ( n + 1) − 1 2/6/2006 Trees 11

  12. BinaryTree ADT The BinaryTree ADT Update methods extends the Tree may be defined by ADT, i.e., it inherits data structures all the methods of implementing the the Tree ADT BinaryTree ADT Additional methods: � position leftChild(p) � position rightChild(p) � position sibling(p) 2/6/2006 Trees 12

  13. Inorder Traversal In an inorder traversal a Algorithm inOrder ( v ) node is visited after its left if isInternal ( v ) subtree and before its right inOrder ( leftChild ( v )) subtree Application: draw a binary visit ( v ) tree if isInternal ( v ) � x(v) = inorder rank of v inOrder ( rightChild ( v )) � y(v) = depth of v 6 2 8 1 4 7 9 3 5 2/6/2006 Trees 13

  14. Print Arithmetic Expressions Algorithm printExpression ( v ) Specialization of an inorder traversal if isInternal ( v ) print operand or operator � print ( “(’’ ) when visiting node print “(“ before traversing left inOrder ( leftChild ( v )) � subtree print ( v.element ()) print “)“ after traversing right � subtree if isInternal ( v ) + inOrder ( rightChild ( v )) print ( “)’’ ) × × − 2 3 b ((2 × ( a − 1)) + (3 × b)) a 1 2/6/2006 Trees 14

  15. Evaluate Arithmetic Expressions Specialization of a postorder Algorithm evalExpr ( v ) traversal if isExternal ( v ) � recursive method returning return v.element () the value of a subtree else � when visiting an internal x ← evalExpr ( leftChild ( v )) node, combine the values y ← evalExpr ( rightChild ( v )) of the subtrees ◊ ← operator stored at v return x ◊ y + × × − 2 3 2 5 1 2/6/2006 Trees 15

  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: � on the left (preorder) � from below (inorder) � on the right (postorder) + × × L R B − 2 3 2 5 1 2/6/2006 Trees 16

  17. Template Method Pattern public abstract class EulerTour<E,R> { Generic algorithm that can protected BinaryTree<E> tree; be specialized by public abstract R execute(BinaryTree<E> t); redefining certain steps protected void init(BinaryTree<E> t){ tree = t;} protected void visitLeft(Position<E> v, TourResult<R> r) { } Implemented by means of protected void visitBelow(Position<E> v, TourResult<R> r){ } an abstract Java class protected void visitRight(Position<E> v, TourResult<R> r) { } Visit methods that can be protected E eulerTour(Position<E> v) { redefined by subclasses TourResult<R> r = new TourResult<R>( ); visitLeft(v,r) ; Template method eulerTour if (tree.hasLeft(v)) � Recursively called on the r.left=eulerTour(tree.left(v)); visitBelow(v, r); left and right children if (tree.hasRight(v)) � A Result object with fields r.left=eulerTour(tree.right(v)); leftResult , rightResult and visitRight(v, r); finalResult keeps track of return r.out; the output of the } … recursive calls to eulerTour 2/6/2006 Trees 17

  18. Specializations of EulerTour public class EvaluateExpressionTour We show how to extends EulerTour<ExpressionTerm,Integer> { specialize class public Integer execute(BinaryTree<ExpressionTerm> T){ EulerTour to evaluate init(T); an arithmetic return eulerTour(tree.root()); expression } Assumptions protected void visitRight(Position<ExpressionTerm> v, TourResult<Integer> r) { � External nodes store ExpressionTerm term = v.element(); Integer objects if (tree.isInternal(v)){ � Internal nodes store ExpressionOperator op = (ExpressionOperator) term; ExpressionOperator op.setOperands(r.left, r.right); objects supporting } method setOperands r.out = term.getValue(); (Integer, Integer) and } getValue() } … 2/6/2006 Trees 18

  19. Data Structure for Trees A node is represented by an object storing ∅ Element � Parent node � B Sequence of children � nodes ∅ ∅ Node objects implement the Position ADT A D F B D A F ∅ ∅ C E C E 2/6/2006 Trees 19

  20. Data Structure for Binary Trees A node is represented by ∅ an object storing Element � Parent node � B Left child node � Right child node � Node objects implement ∅ ∅ the Position ADT B A D A D ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ C E C E 2/6/2006 Trees 20

  21. Java Implementation Tree interface expandExternal( v ) BinaryTree interface v v extending Tree A A Classes implementing Tree and BinaryTree and ∅ ∅ providing � Constructors � Update methods removeAboveExternal( w ) � Print methods Examples of updates for A binary trees B w � expandExternal( v ) B C � removeAboveExternal( w ) 2/6/2006 Trees 21

  22. Trees in net.datastructures NDS4 is the Library of Data NDS4 was developed at Structures in Java Brown and UC Irvine Tree interfaces in NDS4 See the JDSL documentation and tutorials at � CompleteBinaryTree http://net3.datastructures.net � BinaryTree /doc4/index.html � Tree Tree classes in JDSL LinkedTree � LinkedBinaryTree � ArrayListCompleteBinaryTree � AVLTree Tree � BinarySearchTree � LinkedTree LinkedBinaryTree � RBTree BinaryTree 2/6/2006 Trees 22

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend