* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Lists and Trees (continued)
Survey
Document related concepts
Transcript
Lists and Trees (continued) CS-2301 System Programming D-term 2009 (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) CS-2301 D-term 2009 Lists and Trees (continued) 1 Definitions • Linked List • A data structure in which each element is dynamically allocated and in which elements point to each other to define a linear relationship • Singly- or doubly-linked • Stack, queue, circular list • Tree • A data structure in which each element is dynamically allocated and in which each element has more than one potential successor • Defines a partial order CS-2301 D-term 2009 Lists and Trees (continued) 2 Definitions (continued) • Binary Tree • A tree in which each element has two potential successors • Subtree • The set of nodes that are successors to a specific node, either directly or indirectly • Root of a tree • The node of the tree that is not the successor to any other node, all other nodes are (directly or indirectly) successors to it CS-2301 D-term 2009 Lists and Trees (continued) 3 Binary Tree • A linked list but with two links per item struct treeItem { type payload; treeItem *left; treeItem *right; }; payload left payload left payload payload left left right payload right left payload left right right right CS-2301 D-term 2009 Lists and Trees (continued) 4 right payload left right Binary Trees (continued) • Two-dimensional data structure • Easy to grow and shrink • Easy to add and delete items at leaves • More work needed to insert or delete branch nodes • Search time is O(log n) • If tree is reasonably balanced • Degenerates to O(n) in worst case if unbalanced CS-2301 D-term 2009 Lists and Trees (continued) 5 Order of Traversing Binary Trees • In-order • Traverse left sub-tree (in-order) • Visit node itself • Traverse right sub-tree (in-order) • Pre-order • Visit node first • Traverse left sub-tree • Traverse right sub-tree • Post-order • Traverse left sub-tree • Traverse right sub-tree • Visit node last CS-2301 D-term 2009 Lists and Trees (continued) 6 Question • Which order should Programming Assignment #6 traverses its tree? CS-2301 D-term 2009 Lists and Trees (continued) 7 “Big O” notation New Topic CS-2301 D-term 2009 Lists and Trees (continued) 8 Linked Lists Again • Linear data structure • Easy to grow and shrink • Easy to add and delete items • Time to search for an item – O(n) CS-2301 D-term 2009 Lists and Trees (continued) 9 Binary Trees Again • Non-linear data structure • Easy to grow and shrink • Easy to add and delete items • Time to search for an item – O(log n) CS-2301 D-term 2009 Lists and Trees (continued) 10 Definition — Big-O “Of the order of …” • A characterization of the number of operations in an algorithm in terms of a mathematical function of the number of data items involved • O(n) means that the number of operations to complete the algorithm is proportional to n • E.g., searching a list with n items requires, on average, n/2 comparisons with payloads CS-2301 D-term 2009 Lists and Trees (continued) 11 Big-O (continued) • • • • • O(n): proportional to n – i.e., linear O(n2): proportional to n2 – i.e., quadratic O(kn) – proportional to kn – i.e., exponential … O(log n) – proportional to log n – i.e., sublinear • O(n log n) • Worse than O(n), better than O(n2) • O(1) – independent of n; i.e., constant CS-2301 D-term 2009 Lists and Trees (continued) 12 Anecdote & Questions:– • In the design of electronic adders, what is the order of the carry-propagation? • What is the order of floating point divide? • What is the order of floating point square root? • What program have we studied in this course that is O(2n)? i.e., exponential? CS-2301 D-term 2009 Lists and Trees (continued) 13 Questions on Big-O? CS-2301 D-term 2009 Lists and Trees (continued) 14