Lists and Trees (continued) CS-2301, B-Term Lists and Trees (continued) CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel)
Lists and Trees (continued) CS-2301, B-Term Reminder – 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
Lists and Trees (continued) CS-2301, B-Term 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
Lists and Trees (continued) CS-2301, B-Term Binary Tree A linked list but with two links per item struct treeItem { type payload; treeItem *left; treeItem *right; }; leftright payload leftright payload leftright payload leftright payload leftright payload leftright payload leftright payload
Lists and Trees (continued) CS-2301, B-Term 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
Lists and Trees (continued) CS-2301, B-Term 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
Lists and Trees (continued) CS-2301, B-Term Question What order should you traverses the tree for Programming Assignment #5?
Lists and Trees (continued) CS-2301, B-Term Another Example of Binary Tree x = (a.real*b.imag - b.real*a.imag) / sqrt(a.real*b.real – a.imag*b.imag) = x/ sqrt - **.. arealbimag.. brealaimag - …
Lists and Trees (continued) CS-2301, B-Term Question What kind of traversal order is required for this expression? In-order? Pre-order? Post-order?
Lists and Trees (continued) CS-2301, B-Term Binary Trees in Compilers Used to represent the structure of the compiled program Optimizations Common sub-expression detection Code simplification Loop unrolling Parallelization Reductions in strength – e.g., substituting additions for multiplications, etc. Many others
Lists and Trees (continued) CS-2301, B-Term Questions about Trees? or about Programming Assignment 6?
Lists and Trees (continued) CS-2301, B-Term “Big O” notation New Topic
Lists and Trees (continued) CS-2301, B-Term 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) I.e., proportional to n, the number of items in the list
Lists and Trees (continued) CS-2301, B-Term 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) I.e., proportional to log of number of items in the list
Lists and Trees (continued) CS-2301, B-Term 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
Lists and Trees (continued) CS-2301, B-Term Big-O (continued) O(n): proportional to n – i.e., linear O(n 2 ): proportional to n 2 – i.e., quadratic O(k n ) – proportional to k n – i.e., exponential … O(log n) – proportional to log n – i.e., sublinear O(n log n) Worse than O(n), better than O(n 2 ) O(1) – independent of n; i.e., constant
Lists and Trees (continued) CS-2301, B-Term 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(2 n )? i.e., exponential?
Lists and Trees (continued) CS-2301, B-Term Questions on Big-O?