Download presentation
Presentation is loading. Please wait.
Published byFranklin Jackson Modified over 6 years ago
1
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
07/27/16 07:48 07/27/16 07:48 Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery © 2010 Goodrich, Tamassia Trees 1 1
2
What is a Tree? Abstract model of a hierarchical structure
Trees 07/27/16 07:48 What is a Tree? Abstract model of a hierarchical structure Nodes with a parent- child relation Single root node Applications: Organization charts File systems Programming environments Computers”R”Us Sales R&D Manufacturing Laptops Desktops US International Europe Asia Canada © 2010 Goodrich, Tamassia Trees 2
3
Tree Terminology subtree Root: node without parent (A).
Trees 07/27/16 07:48 Tree Terminology Root: node without parent (A). Internal node: node with at least one child (A, B, C, F). External node (leaf ): node without children (E, I, J, K, G, H, D). Ancestors: parent, grandparent, grand-grandparent, etc. Descendant: child, grandchild, grand-grandchild, etc. Depth of node: maximum number of descendants. Height of tree: depth of root (3). Subtree: tree consisting of a node and its descendants (C, G, H). A B D C G H E F I J K subtree © 2010 Goodrich, Tamassia Trees
4
Preorder Traversal Algorithm preOrder(v) visit(v)
Trees 07/27/16 07:48 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. Algorithm preOrder(v) visit(v) for each child w of v preorder (w) 1 Make Money Fast! 2 5 9 1. Motivations 2. Methods References 6 7 8 3 4 2.1 Stock Fraud 2.2 Ponzi Scheme 2.3 Bank Robbery 1.1 Greed 1.2 Avidity © 2010 Goodrich, Tamassia Trees 4
5
Postorder Traversal Algorithm postOrder(v) for each child w of v
Trees 07/27/16 07:48 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) 9 cs16/ 8 3 7 todo.txt 1K homeworks/ programs/ 1 2 4 5 6 h1c.doc 3K h1nc.doc 2K DDR.cpp 10K Stocks.cpp 25K Robot.cpp 20K © 2010 Goodrich, Tamassia Trees 5
6
Binary Tree A binary tree is a tree with the following properties:
Trees 07/27/16 07:48 Binary Tree 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 D E F G H I © 2010 Goodrich, Tamassia Trees 6
7
Arithmetic Expression Tree
Trees 07/27/16 07:48 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 © 2010 Goodrich, Tamassia Trees 7
8
Decision Tree Binary tree associated with a decision process
Trees 07/27/16 07:48 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 © 2010 Goodrich, Tamassia Trees 8
9
Properties of Proper Binary Trees
07/27/16 07:48 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 2h h log2 e h log2 (n 1) 1 © 2010 Goodrich, Tamassia Trees 9
10
Inorder Traversal Algorithm inOrder(v) if v.isExternal()
Trees 07/27/16 07:48 Inorder Traversal Each node is visited after its left subtree and before its right subtree. Algorithm inOrder(v) if v.isExternal() inOrder(v.left()) visit(v) inOrder(v.right()) 6 2 8 1 4 7 9 3 5 © 2010 Goodrich, Tamassia Trees 10
11
Printing Arithmetic Expressions
Trees 07/27/16 07:48 Printing Arithmetic Expressions Algorithm printExpression(v) if v.isExternal() print(“(’’) inOrder(v.left()) print(v.element()) if v.isExternal() inOrder(v.right()) print (“)’’) Perform inorder traversal print operand or operator when visiting node print “(“ before traversing left subtree print “)“ after traversing right subtree 2 a 1 3 b ((2 (a 1)) (3 b)) © 2010 Goodrich, Tamassia Trees 11
12
Linked Structure for Trees
07/27/16 07:48 Linked Structure for Trees A node is represented by an object storing Element Parent node list of children nodes B A D F B A D F C E C E © 2010 Goodrich, Tamassia Trees 12
13
Linked Structure for Binary Trees
07/27/16 07:48 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 B A D C E C E © 2010 Goodrich, Tamassia Trees 13
14
Array Representation of Binary Trees
07/27/16 07:48 Array Representation of Binary Trees Nodes are stored in an array A 1 A A B D … G H … 2 3 1 2 3 10 11 B D 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 4 5 6 7 E F C J 10 11 G H © 2010 Goodrich, Tamassia Trees 14
15
Trees 07/27/16 07:48 Binary Search Tree A binary search tree is a binary tree storing keys (or key-value entries) at its internal nodes. For nodes u, v, and w with u in the left subtree of v and w in the right subtree of v, key(u) key(v) key(w). External nodes do not store items. Inorder traversal visits the keys in increasing order. 6 9 2 4 1 8 © 2010 Goodrich, Tamassia Binary Search Trees 15
16
Search To search for a key k, trace a downward path from the root.
Dictionaries Trees 07/27/16 09:40 07/27/16 07:48 Search Algorithm TreeSearch(k, v) if v.isExternal () return v if k v.key() return TreeSearch(k, v.left()) else if k v.key() else { k v.key() } return TreeSearch(k, v.right()) To search for a key k, trace a downward path from the root. The next node visited depends on the comparison of k with the key of the current node. If a leaf is reached, the key is not in the tree. Example: TreeSearch(4, root) 6 2 9 1 4 8 © 2010 Goodrich, Tamassia Binary Search Trees 16 16
17
Insertion Search for key k using TreeSearch.
07/27/16 07:48 Insertion 6 Search for key k using TreeSearch. Assume k is not already in the tree, and let w be the leaf reached by the search. Insert k at node w and expand w into an internal node. Example: insert 5. Duplicate keys can be handled with non-strict inequality. 2 9 1 4 8 w 6 2 9 1 4 8 w 5 © 2010 Goodrich, Tamassia Binary Search Trees 17
18
Deletion To delete k, search for key k.
Trees 07/27/16 07:48 Deletion 6 To delete k, search for key k. If key k is in the tree, let v be the node storing k. If node v has a leaf child w, remove v and w from the tree with removeExternal(w), which removes w and its parent. Example: remove 4. 2 9 v 1 4 8 w 5 6 2 9 1 5 8 © 2010 Goodrich, Tamassia Binary Search Trees 18
19
Trees 07/27/16 07:48 Deletion (cont.) 1 v If key k is stored at a node v with two internal children, find the internal node w that follows v in an inorder traversal. Copy key(w) into node v. Remove node w and its left child z (which must be a leaf) by means of operation removeExternal(z). Example: remove 3. 3 2 8 6 9 w 5 z 1 v 5 2 8 6 9 © 2010 Goodrich, Tamassia Binary Search Trees 19
20
Performance A binary tree with n keys takes O(n) space.
Trees 07/27/16 07:48 Performance A binary tree with n keys takes O(n) space. The height h is O(n) in the worst case. The height h is O(log n) in the average case. Find, insert, and delete take O(h) time. © 2010 Goodrich, Tamassia Binary Search Trees 20
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.