Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery © 2010 Goodrich, Tamassia.

Similar presentations


Presentation on theme: "Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery © 2010 Goodrich, Tamassia."— Presentation transcript:

1 Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery © 2010 Goodrich, Tamassia

2 Trees2 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 Computers”R”Us SalesR&DManufacturing LaptopsDesktops US International EuropeAsiaCanada © 2010 Goodrich, Tamassia

3 Trees3 subtree 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, grandparent, grand-grandparent, etc.  Depth of a node: number of ancestors  Height of a tree: maximum depth of an external node (3)  Descendant of a node: child, grandchild, grand-grandchild, etc. A B DC GH E F IJ K  Subtree: tree consisting of a node and its descendants © 2010 Goodrich, Tamassia

4 4 More Terminology Trees (Goodrich, 268)‏ Siblings =two or more nodes that are children of the same parent Subtree rooted at v v v Internal node = node with one or more children External node = node with no children Leaf = external node

5 Trees5 Tree ADT  We use positions to abstract nodes  Generic methods: integer size() boolean isEmpty() Iterator iterator() Iterable positions()  Accessor methods: position root() position parent(p) Iterable children(p) Query methods: boolean isInternal(p) boolean isExternal(p) boolean isRoot(p) Update method: element replace (p, o) Additional update methods may be defined by data structures implementing the Tree ADT © 2010 Goodrich, Tamassia

6 6 Tree Interface public interface Tree { public int size(); public boolean isEmpty(); public Iterator iterator(); public Iterable > positions(); public E replace(Position v, E e) throws InvalidPositionException; public Position root() throws EmptyTreeException; public Position parent(Position v) throws InvalidPositionException, BoundaryViolationException; public Iterable > children(Position v) throws InvalidPositionException; public boolean isInternal(Position v) throws InvalidPositionException; public boolean isExternal(Position v) throws InvalidPositionException; public boolean isRoot(Position v) throws InvalidPositionException; } Trees (Goodrich, 270)‏

7 7 Running Time Assumptions Trees (Goodrich, 272)‏ O(n)‏iterator, positions O(1)‏replace O(1)‏swapElements O(c v ) (c v = no. of children of v)‏children O(1)‏isRoot O(1)‏isInternal, isExternal O(1)‏parent O(1)‏root TimeMethod

8 8 Depth of a Node in a Tree  Depth of v = number of ancestors of v If v is the root, v’s depth = 0 Else, v’s depth = 1 + depth of v’s parent Trees (Goodrich, 273)‏ Depth 0 1 + depth( myParent ) = 1 + 0 = 1 1 + depth( myParent ) = 1 + 1 = 2 1 + depth( myParent ) = 1 + 2 = 3

9 9 Finding the Depth Algorithm depth( T, v )‏ if T.isRoot(v) then return 0 else return 1 + depth( T, T.parent(v) )‏ Trees (Goodrich, 273)‏

10 10 Finding the Depth Algorithm depth( T, v )‏ if T.isRoot(v) then return 0 else return 1 + depth( T, T.parent(v) )‏ Trees (Goodrich, 273)‏ public static int depth(Tree T, Position v){ if( T.isRoot(v) )‏ return 0; else return 1 + depth( T, T.parent(v) ); }

11 11 Height  Height of node v If v is an external node, v’s height = 0 Else, v’s height = 1 + maximum height of v’s children Trees (Goodrich, 274–275)‏ v h v = 1 + max( h of myChildren ) h v = 1 + 1 = 2 h = 0 (External node)‏ h = 1 + max( h of myChildren )‏ h = 1 + 0 = 1 h = 0 (External node)‏

12 12 Height  Height of node v If v is an external node, v’s height = 0 Else, v’s height = 1 + maximum height of v’s children Trees (Goodrich, 274–275)‏ v h v = 1 + max( h of myChildren ) h v = 1 + 1 = 2 h = 0 (External node)‏  Height of tree T T’s height = height of the root of T h = 1 + max( h of myChildren )‏ h = 1 + 0 = 1 h = 0 (External node)‏

13 13 Finding the Height of a Tree Algorithm height( T, root )‏ if T.isExternal(root) then return 0 else h = 0 for each node  T.children(root) do h = max( h, height(T,node) )‏ return 1 + h r u t v h = 0 h = max(h,height(T,t))‏ return 1 + h r 0 0 1 2 T Trees (Goodrich, 274–275)‏

14 14 Finding the Height of a Tree Algorithm height( T, root )‏ if T.isExternal(root) then return 0 else h = 0 for each node  T.children(root) do h = max( h, height(T,node) )‏ return 1 + h r u t v h = 0 h = max(h,height(T,t))‏ return 1 + h r 0 0 1 2 h = 0 h = max(h,height(T,u))‏ h = max(h,height(T,v))‏ return 1 + h t T Trees (Goodrich, 274–275)‏

15 15 Finding the Height of a Tree Algorithm height( T, root )‏ if T.isExternal(root) then return 0 else h = 0 for each node  T.children(root) do h = max( h, height(T,node) )‏ return 1 + h r u t v h = 0 h = max(h,height(T,t))‏ return 1 + h r 0 0 1 2 h = 0 h = max(h,height(T,u))‏ h = max(h,height(T,v))‏ return 1 + h t return 0 u T Trees (Goodrich, 274–275)‏

16 16 Finding the Height of a Tree Algorithm height( T, root )‏ if T.isExternal(root) then return 0 else h = 0 for each node  T.children(root) do h = max( h, height(T,node) )‏ return 1 + h r u t v h = 0 h = max(h,height(T,t))‏ return 1 + h r 0 0 1 2 h = 0 h = max(h,height(T,u))‏ h = max(h,height(T,v))‏ return 1 + h t return 0 u v T Trees (Goodrich, 274–275)‏

17 17 Finding the Height of a Tree Algorithm height( T, root )‏ if T.isExternal(root) then return 0 else h = 0 for each node  T.children(root) do h = max( h, height(T,node) )‏ return 1 + h r u t v h = 0 h = max(h,height(T,t))‏ return 1 + h r 0 0 1 2 h = 0 h = max(h,height(T,u))‏ h = max(h,height(T,v))‏ return 1 + h t return 0 u T Trees (Goodrich, 274–275)‏ return 0 v return 1

18 18 Finding the Height of a Tree Algorithm height( T, root )‏ if T.isExternal(root) then return 0 else h = 0 for each node  T.children(root) do h = max( h, height(T,node) )‏ return 1 + h r u t v h = 0 h = max(h,height(T,t))‏ return 1 + h r 0 0 1 2 return 1 return 2 h = 0 h = max(h,height(T,u))‏ h = max(h,height(T,v))‏ return 1 + h t return 0 u v T Trees (Goodrich, 274–275)‏

19 Trees19 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 Make Money Fast! 1. MotivationsReferences2. Methods 2.1 Stock Fraud 2.2 Ponzi Scheme 1.1 Greed1.2 Avidity 2.3 Bank Robbery 1 2 3 5 4 678 9 Algorithm preOrder(v) visit(v) for each child w of v preorder (w) © 2010 Goodrich, Tamassia

20 Trees20 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 456 8 © 2010 Goodrich, Tamassia

21 Trees21 Binary Trees  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  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  Applications: arithmetic expressions decision processes searching A B C FG D E H I © 2010 Goodrich, Tamassia

22 Trees22 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 a1 3b © 2010 Goodrich, Tamassia

23 Trees23 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? StarbucksSpike’sAl FornoCafé Paragon Yes No YesNoYesNo © 2010 Goodrich, Tamassia

24 Trees24 Properties of Proper 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  2 h h  log 2 e h  log 2 (n  1)  1 © 2010 Goodrich, Tamassia

25  Level d has at most 2 d nodes © 2010 Goodrich, TamassiaTrees25

26 Trees26 BinaryTree ADT  The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree ADT  Additional methods: position left(p) position right(p) boolean hasLeft(p) boolean hasRight(p)  Update methods may be defined by data structures implementing the BinaryTree ADT © 2010 Goodrich, Tamassia

27 Trees27 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 hasLeft (v) inOrder (left (v)) visit(v) if hasRight (v) inOrder (right (v)) 3 1 2 5 6 79 8 4 © 2010 Goodrich, Tamassia

28 Trees28 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 hasLeft (v) print( “(’’ ) inOrder (left(v)) print(v.element ()) if hasRight (v) inOrder (right(v)) print ( “)’’ )    2 a1 3b ((2  ( a  1))  (3  b)) © 2010 Goodrich, Tamassia

29 Trees29 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 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 51 32 © 2010 Goodrich, Tamassia

30 Trees30 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)    2 51 32 L B R  © 2010 Goodrich, Tamassia

31 Trees31  Linked 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 D A CE F B  ADF  C  E © 2010 Goodrich, Tamassia

32 Trees32 Linked 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 B D A CE   BADCE  © 2010 Goodrich, Tamassia

33 Array-Based Representation of Binary Trees  Nodes are stored in an array A © 2010 Goodrich, Tamassia33Trees  Node v is stored at A[rank(v)] 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 1 23 6 7 45 1011 A HG FE D C B J ABDGH … … 1231011 0

34 34 Implementing a Binary Tree

35 35 Linked Binary Tree  Natural way to represent a tree is by using linked nodes Trees (Goodrich, 287–295)‏

36 36 Node for a Binary Tree left element parent right Trees (Goodrich, 287–295)‏

37 37 Node for a Binary Tree left element parent right left r null right null s parent null t parent null root Trees (Goodrich, 287–295)‏

38 38 Node for a Binary Tree public class BTNode implements BTPosition { private E element; public BTNode() { } public BTNode(E element, BTPosition parent, BTPosition left, BTPosition right) { setElement(element); setParent(parent); setLeft(left); setRight(right); } public E element() { return element; } public void setElement(E o) { element=o; } public BTPosition getLeft() { return left; } public void setLeft(BTPosition v) { left=v; } public BTPosition getRight() { return right; } public void setRight(BTPosition v) { right=v; } public BTPosition getParent() { return parent; } public void setParent(BTPosition v) { parent=v; } } Trees (Goodrich, 287–295)‏

39 39 Position for a Binary Tree public interface BTPosition extends Position { // inherits element()‏ public void setElement(E o); public BTPosition getLeft(); public void setLeft(BTPosition v); public BTPosition getRight(); public void setRight(BTPosition v); public BTPosition getParent(); public void setParent(BTPosition v); } Trees (Goodrich, 287–295)‏

40 40 A Position Interface public interface Position { /** Return the element stored at this position. */ E element(); } A Position object, has only one operation that it can use. This operation returns the element stored inside the object. Positions (Goodrich, 232, 234)‏

41 41 Some Operations of a Binary Tree Return to pages 304 - 308 public class LinkedBinaryTree implements BinaryTree { public int size() {return size; } public boolean isEmpty() { /*...*/ } public boolean isInternal(Position v) throws InvalidPositionException { checkPosition(v); return (hasLeft(v) || hasRight(v)); } public boolean isExternal(Position v) throws InvalidPositionException { /*...*/ } public boolean isRoot(Position v) throws InvalidPositionException { checkPosition(v); return (v == root()); } public boolean hasLeft(Position v) throws InvalidPositionException { BTPosition vv = checkPosition(v); return (vv.getLeft() != null); } public boolean hasRight(Position v) throws InvalidPositionException { /*...*/ } public Position root() throws EmptyTreeException { /*...*/ } public Position left(Position v) throws InvalidPositionException, BoundaryViolationException { BTPosition vv = checkPosition(v); Position leftPos = vv.getLeft(); if (leftPos == null) throw new BoundaryViolationException(); return leftPos;} public Position right(Position v) throws InvalidPositionException, BoundaryViolationException { /*...*/ } Trees (Goodrich, 287–295)‏

42 42 Instance Variables of a Binary Tree public class LinkedBinaryTree implements BinaryTree { protected BTPosition root; // reference to the root protected int size; // number of nodes //... Trees (Goodrich, 287–295)‏

43 43 Constructor of a Binary Tree public class LinkedBinaryTree implements BinaryTree { protected BTPosition root; // reference to the root protected int size; // number of nodes public LinkedBinaryTree() { root = null; // start with an empty tree size = 0; } //... Trees (Goodrich, 287–295)‏

44 44 Operations of the LinkedBinaryTree  Tell us about the positions posroot()‏ posparent( pos )‏ iterchildren( pos )‏ boolisInternal( pos )‏ boolisExternal( pos )‏ boolisRoot( pos )‏ posleft( pos )‏ posright( pos )‏ poshasLeft( pos )‏ poshasRight( pos )‏ possibling( pos )‏  Tell us about the collection intsize()‏ boolisEmpty()‏ iteriterator()‏ iterpositions()‏  Update the data void replace( pos, e )‏ addRoot( e )‏ insertLeft( pos, e )‏ insertRight( pos, e )‏ remove( pos )‏ Trees (Goodrich, 289–295)‏ Tree methods Binary Tree methods Additional methods

45 45 Adding an Element to a LinkedBinaryTree Object LinkedBinaryTree T = new LinkedBinaryTree ; Creates an empty tree. Trees (Goodrich, 287–295)‏ root A T.addRoot(new Character('A')); Adds a node as root and fills it with an element. root

46 46 Adding an Element to a LinkedBinaryTree Object LinkedBinaryTree T = new LinkedBinaryTree ; Creates an empty tree. Trees (Goodrich, 287–295)‏ root A T.addRoot(new Character('A')); Adds a node as root and fills it with an element. root A T.insertLeft( T.root(), new Character('B')); Inserts the node with its element. B root

47 47 An Alternative Binary Tree Implementation ArrayList Implementation

48 48 Binary Tree Implemented with an ArrayList  While it’s natural to think of implementing a tree with linked Nodes, a binary tree can also be implemented with an ArrayList  Advantage: better performance Trees (Goodrich, 296–297)‏

49 49 Binary Tree Implemented as an ArrayList Based on a way of numbering the nodes If v is the root of T, then p(v) = 1 If v is the left child of node u, then p(v) = 2 * p(u)‏ If v is the right child of node u, then p(v) = 2 * p(u) + 1 Trees (Goodrich, 289–291)‏ D E F GAC B 1 2 3 4 5 6 7 DBFACEG 0123456789 A


Download ppt "Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery © 2010 Goodrich, Tamassia."

Similar presentations


Ads by Google