Trees 二○一八年八月二十六日 Part-C Trees Trees.

Slides:



Advertisements
Similar presentations
Trees and Binary Trees Become Rich Force Others to be Poor Rob Banks
Advertisements

Trees1 More on Trees University Fac. of Sci. & Eng. Bus. School Law School CS Dept. EE Dept. Math. Dept.
Data Structures Lecture 6 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Trees Briana B. Morrison Adapted from Alan Eugenio.
1 prepared from lecture material © 2004 Goodrich & Tamassia COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material.
© 2004 Goodrich, Tamassia Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery.
© 2004 Goodrich, Tamassia Trees1 Chapter 7 Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery.
Trees1 Recursion Recursion is a concept of defining a method that makes a call to itself.
Chapter 7: Trees Objectives:
© 2004 Goodrich, Tamassia Trees1 Lecture 04 Trees Topics Trees Binary Trees Binary Search trees.
Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees.
1 Chapter 7 Trees. 2 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.
© 2004 Goodrich, Tamassia Trees1 this is a tree. © 2004 Goodrich, Tamassia Trees2 What is a Tree? In computer science, a tree is an abstract model of.
Trees COMP53 Oct 31, What is a Tree? A tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child relation.
Trees1 Part-C Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery.
CSC401 – Analysis of Algorithms Lecture Notes 4 Trees and Priority Queues Objectives: General Trees and ADT Properties of Trees Tree Traversals Binary.
General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
Trees Chapter 8.
Trees.
Make Money Fast! Stock Fraud Apply shortcuts Bank Robbery Just For Laughs Trees This one is cool eITnotes.com For more notes and topics visit:
Trees CSCI 3333 Data Structures. Acknowledgement  Dr. Bun Yue  Mr. Charles Moen  Dr. Wei Ding  Ms. Krishani Abeysekera  Dr. Michael Goodrich  Dr.
Chapter 7:Trees Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data Structures.
Saturday, 04 Apr 2010 University of Palestine Computer Science II Trees.
Trees. 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.
Trees 4/23/2017 Trees.
COMP20010: Algorithms and Imperative Programming Lecture 1 Trees.
© 2004 Goodrich, Tamassia Trees1 Text Book: M. T. Goodrich and R. Tamassia, "Data Structures and Algorithms in Java," 4th edition, 2006, Wiley & Sons,
Trees by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures.
1 Binary Trees (7.3) CSE 2011 Winter November 2015.
Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery © 2010 Goodrich, Tamassia.
Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery © 2010 Goodrich, Tamassia.
Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery © 2013 Goodrich, Tamassia, Goldwasser.
Trees trees binary trees traversals of trees template method pattern
CH 7. TREES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
CH 7 : TREE ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY.
Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery © 2010 Goodrich, Tamassia.
© 2004 Goodrich, Tamassia Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery.
TREE Ahsan Ahmed College of Computer and Information Science Majma’ah University 1.
1 COMP9024: Data Structures and Algorithms Week Five: Trees Hui Wu Session 1, 2015
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
Elementary Data Structures
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Trees 5/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Trees 5/10/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Searching (and into Trees)
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery Trees
University of Palestine
Stacks (Background) Trees.
Trees 8/7/2018 2:27 PM Binary Trees © 2010 Goodrich, Tamassia Trees.
Trees Chuan-Ming Liu Computer Science & Information Engineering
© 2013 Goodrich, Tamassia, Goldwasser
Trees 9/21/2018 9:58 PM Trees this is a tree Trees.
COSC2100: Data Structures and Algorithms
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Elementary Data Structures
Week nine-ten: Trees Trees.
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery Trees
Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb CLRS, Section 10.4.
Trees Palestine Gaza West Bank 48 Stolen Land Trees Trees
Lecture 8: Iterators, Trees
Copyright © Aiman Hanna All rights reserved
Lecture 9 CS2013.
CS210- Lecture 9 June 20, 2005 Announcements
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Trees.
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Trees 二○一八年八月二十六日 Part-C Trees Trees

Pyramid Scheme Trees

Infection network of SARS (Singapore) Trees

University Fac. of Sci. & Eng. Bus. School Law School Math. Dept. Trees 二○一八年八月二十六日 University Fac. of Sci. & Eng. Bus. School Law School Math. Dept. CS Dept. EE Dept. Trees

Remark: A tree is a special kind of graph, where there is not circuit. 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 arithmetic expression Computers”R”Us Sales R&D Manufacturing Laptops Desktops US International Europe Asia Canada Remark: A tree is a special kind of graph, where there is not circuit. Trees

File System on Computers H: CS5302 Others CS5301 a.java b.java net source readme Node.java Stack.java Trees

Trees

Tree Terminology subtree Root: node without parent (e.g. A) Internal node: node with at least one child (e.g. A, B, C, F) External node (or 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 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 subtree Trees

Tree Terminology Ordered tree:There is a linear ordering defined for the children of each node. Example: The order among siblings are from left to right. Ch1 Ch2 Ch3; S1.1 S1.2; S2.1 S2.1; 1.2.1 1.2.2 1.2.3; Book Ch1 Ch3 Ch2 S.2.1 S.2.2 S.1.1 S.1.2 1.2.1 1.2.2 1.2.3 Trees

Tree ADT (§ 6.1.2) A set of nodes with parent-child relationship. Generic methods: integer size() boolean isEmpty() Accessor methods: root() returns the tree’s root. parent(p) returns p’s parent children(p) returns an iterator of the children of node v. Element() return the object stored on this node. Query methods: boolean isInternal(p) boolean isExternal(p) boolean isRoot(p) Update method: object replace (p, o): replace the content of node p with o. Additional update methods may be defined by data structures implementing the Tree ADT Trees

Binary Trees (§ 6.3) Applications: arithmetic expressions decision processes searching 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 A B C D E F G H I Trees

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 Trees

Decision Tree Binary tree associated with a decision process internal nodes: questions with yes/no answer external nodes: decisions Example: drinking decision Want to drink ? No Yes Bye-Bye How about coffee? No Yes Here it is How about tee No No Yes Here it is Here is your water Trees

BinaryTree ADT (§ 6.3.1) 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 Trees

Inorder Traversal (just for binary tree) In an inorder traversal a node is visited after its left subtree and before its right subtree 6 Algorithm inOrder(v) if hasLeft (v) inOrder (left (v)) visit(v) if hasRight (v) inOrder (right (v)) 2 8 1 4 7 9 3 5 Trees

Inorder Traversal (Another example) The number on a node is smaller than the numbers on its Right sub-tree and larger than the numbers on it left sub-tree. 7 Trees

InOrder Traversal (Another example) 8 4 12 14 2 6 10 1 3 5 9 11 15 7 13 Trees

Print Arithmetic Expressions Algorithm printExpression(v) if hasLeft (v) print(“(’’) printExpression (left(v)) print(v.element ()) if hasRight (v) printExpression (right(v)) print (“)’’) Specialization of an 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)) Trees

Print Arithmetic Expressions - + 10 +  - 2 a 1 3 b x ((x+((2  (a - 1)) + (3  b)))-10) Trees

Preorder Traversal Algorithm preOrder(v) visit(v) 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 Trees

Preorder Traversal (Another example) Visit the node. Visit the sub-trees rooted by its children one by one. Algorithm preOrder(v) visit(v) for each child w of v preorder (w) 1 2 17 9 14 3 6 10 13 4 5 7 11 12 16 Trees 8 15

Postorder Traversal Algorithm postOrder(v) for each child w of v 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.java 10K Stocks.java 25K Robot.java 20K Trees

Postorder Traversal (Another example) . My explanation: If the reached node is a leaf, then visit it. When a node is visited, visit the sub-tree rooted by its sibling on the right. When the rightmost child is visited, visit its parent. Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) 17 7 16 15 14 3 6 10 11 1 2 4 8 9 13 Trees 5 12

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 5 1 3 Trees

Array-Based Representation of Binary Trees nodes are stored in an array 1 A H G F E D C B J … 2 3 let rank(node) be defined as follows: 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 10 11 Trees

Full Binary Tree 1 3 2 4 6 7 5 A full binary tree: All the leaves are at the bottom level All nodes which are not at the bottom level have two children. A full binary tree of height h has 2h leaves and 2h-1 internal nodes. 1 3 2 4 6 7 5 This is not a full binary tree. A full binary tree of height 2 Trees

Properties of Proper Binary Trees Notation n number of nodes e number of external nodes i number of internal nodes h height Properties for proper binary tree: e = i + 1 n = 2e - 1 h  i e  2h h  log2 e 1 3 2 7 6 No need to remember. 14 15 Trees

Depth(v): no. of ancestors of v Algorithm depth(T,v) If T.isRoot(v) then return 0; else return 1+depth(T, T.parent(v)) Make Money Fast! 1 1 1 1. Motivations 2. Methods References 2 2 2 2 2 2.1 Stock Fraud 2.2 Ponzi Scheme 2.3 Bank Robbery 1.1 Greed 1.2 Avidity Trees

Height(T,v): If v is an external node, then height of v is 0. Otherwise, the height of v is one +max height of a child of v. Algorithm height2(T,v) if T.isExternal(v) then return 0 else h=0 for each wT.children(v) do h=max(h, height2(T, w)) return 1+h Trees

Height(T,v): 2 1 1 Algorithm height2(T,v) if T.isExternal(v) then return 0 else h=0 for each wT.children(v) do h=max(h, height2(T, w)) return 1+h 2 Make Money Fast! 1 1 1. Motivations 2. Methods References 2.1 Stock Fraud 2.2 Ponzi Scheme 2.3 Bank Robbery 1.1 Greed 1.2 Avidity Trees