CS210- Lecture 9 June 20, 2005 Announcements

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.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
© 2004 Goodrich, Tamassia Trees1 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.
© 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.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
Trees CS /02/05 L7: Trees Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Definition.
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.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
COMP20010: Algorithms and Imperative Programming Lecture 1 Trees.
Compiled by: Dr. Mohammad Omar Alhawarat
Trees by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures.
Data Structures TREES.
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
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
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.
© 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)
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery Trees
University of Palestine
Trees 二○一八年八月二十六日 Part-C Trees Trees.
Trees Chuan-Ming Liu Computer Science & Information Engineering
© 2013 Goodrich, Tamassia, Goldwasser
Binary Trees, Binary Search Trees
Trees 9/21/2018 9:58 PM Trees this is a tree Trees.
CS223 Advanced Data Structures and Algorithms
COSC2100: Data Structures and Algorithms
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Trees and Binary Trees.
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
COMP26120: Algorithms and Imperative Programming
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
Copyright © Aiman Hanna All rights reserved
Binary Trees, Binary Search Trees
Lecture 9 CS2013.
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Binary Trees, Binary Search Trees
Trees.
Cs212: Data Structures Lecture 7: Tree_Part1
Presentation transcript:

CS210- Lecture 9 June 20, 2005 Announcements Assignment 3 has been posted Part 1 is due next Monday Part 2 is due next Tuesday Midterm is on June 30 Section 3.1 - 3.4 Chapter 4 Section 5.1 - 5.3, Section 5.5 Part of Chapter 6 that I will be able to cover till this Thursday (06/23). I will return Assignment 1 on Thursday (06/23)  4/23/2019 CS210-Summer 2005, Lecture 9

Agenda Assignment 3 Tree ADT Tree Terminology Tree Methods Tree Interface Tree Traversals 4/23/2019 CS210-Summer 2005, Lecture 9

Assignment 3 A linked list does not truly provide opportunity to use unbounded memory. In reality, memory for a system can be viewed as one large, consecutive array of memory cells. When programming in Java, or most other high-level languages, the systems takes care of most of the details of managing this memory. For example, when you, as a programmer, want memory for creating an additional node in a list, you might give an instruction such as, Node extra = new Node(); When interpreting this instruction, Java finds available cells of memory which can be used by you for an object of class Node. 4/23/2019 CS210-Summer 2005, Lecture 9

Singly Linked List embedded in Array 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Q 8 P -1 X 12 S 14 G 10 N 4 Head Index 2 Q X G S N P 4/23/2019 CS210-Summer 2005, Lecture 9

Doubly Linked List embedded in Array Index  0   1   2    3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  Contents 6 -23 9 4 15 12 17 3 -1 -57 589 Header Index: 12 Header    17   -23    0    4    Trailer 4/23/2019 CS210-Summer 2005, Lecture 9

Tree ADT Tree is an abstract data type that stores elements hierarchically. Except top element, each element in a tree has a parent element and zero or more children elements. The top element is called the root of the tree. 4/23/2019 CS210-Summer 2005, Lecture 9

What is a Tree A tree T consists of nodes with a parent-child relationship that satisfies following properties: If T is nonempty, it has a special node, called the root of T, that has no parent. Each node v of T different from the root has a unique parent node w; every node with a parent w is a child of w. Applications: Organization charts File systems Programming environments A B D C G H E F I J K 4/23/2019 CS210-Summer 2005, Lecture 9

Tree Terminology subtree 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 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 4/23/2019 CS210-Summer 2005, Lecture 9

Tree Terminology Two nodes that are children of the same parent are siblings. A node u is an ancestor of a node v if u = v or u is an ancestor of the parent of v. The inheritance relation between classes in a java program forms a tree. 4/23/2019 CS210-Summer 2005, Lecture 9

Ordered Tree A Tree is ordered if there is a linear ordering defined for the children of each node; that is, we can identify children of a node as being the first, second, third and so on. A binary tree is an ordered tree with the following properties: Each node has at most two children. Each child node is labeled as being either a left child or a right child. The left child precedes the right child in the ordering of the children of a node. 4/23/2019 CS210-Summer 2005, Lecture 9

Recursive def. of Binary Tree A node r, called the root of T and storing an element. A binary tree, called the left subtree of T. A binary tree, called the right subtree of T. Edge An edge of tree T is a pair of nodes (u, v) such that u is the parent of v. Path A path of T is a sequence of nodes such that any two consecutive nodes in the sequence form an edge. 4/23/2019 CS210-Summer 2005, Lecture 9

Applications of Binary tree An arithmetic expression can be represented by a binary tree whose external nodes are associated with variables or constants, and whose internal nodes are associated with one of the operators +, - , *, and /. Each node in a tree has a value associated with it: If a node is external, then its value is that of its variable or constant. If a node is internal, then its value is defined by applying its operation to the value of its children. 4/23/2019 CS210-Summer 2005, Lecture 9

Tree ADT We use positions to abstract nodes Generic methods: integer size() boolean isEmpty() Iterator elements() Iterator positions() Accessor methods: position root() position parent(p) positionIterator children(p) Query methods: boolean isInternal(p) boolean isExternal(p) boolean isRoot(p) Update method: object replace (p, o) Additional update methods may be defined by data structures implementing the Tree ADT 4/23/2019 CS210-Summer 2005, Lecture 9

Performance Assumptions The accessor methods root() and parent (v) take O(1) time. Query methods isInternal(v), isExternal(v) and isRoot(v) takes O(1) time as well. The accessor method children(v) takes O(ci) time, where ci is the number of children of v. The generic methods size() and isEmpty() take O(1) time. The generic methods replace(v, e) runs in O(1) time. The generic methods elements() and positions() take O(n) time where n is the number of nodes. 4/23/2019 CS210-Summer 2005, Lecture 9

Basic Algorithms on Trees Depth The depth of a node v can be recursively defined as follows: If v is the root, then the depth of v is 0. Otherwise, the depth of v is one plus the depth of the parent v. Try writing depth (Tree T, Position v) Draw recursive trace. Worst case running time is O(n) because in the worst case the depth might be n – 1. 4/23/2019 CS210-Summer 2005, Lecture 9

Basic Algorithms on Trees Height The height of a node v can be recursively defined as follows: If v is an external node, then the height of v is 0. Otherwise, the height of v is one plus the maximum height of a child of v. The height of a nonempty tree T is equal to the maximum depth an external node of T. Try writing height1 (Tree T) non recursively by calling depth. Worst case running time is O(n2). 4/23/2019 CS210-Summer 2005, Lecture 9

Height of a Tree Try writing height2 (Tree T, Position v) using recursive definition of height of a node. The height of the tree is computed by calling Height2(T, T.root()) This is more efficient than non recursive method: O(n) 4/23/2019 CS210-Summer 2005, Lecture 9

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 A 2 5 9 B E I 6 8 3 4 7 G C D F H 4/23/2019 CS210-Summer 2005, Lecture 9

Preorder Traversal public static String preorderPrint (Tree T, Position v) { String s = v.element().toString(); Iterator children = T.children(v); while(children.hasNext()) S+= “ ” + preorderPrint (T, (Position) children.next()); return s; } Preorder Traversal runs in O(n) time. 4/23/2019 CS210-Summer 2005, Lecture 9

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 Try writing postorderPrint. Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) 9 A 8 3 7 B E I 1 2 4 5 6 C D F G H 4/23/2019 CS210-Summer 2005, Lecture 9