Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lists and Trees (continued) CS-2301, B-Term 20091 Lists and Trees (continued) CS-2301, System Programming for Non-Majors (Slides include materials from.

Similar presentations


Presentation on theme: "Lists and Trees (continued) CS-2301, B-Term 20091 Lists and Trees (continued) CS-2301, System Programming for Non-Majors (Slides include materials from."— Presentation transcript:

1 Lists and Trees (continued) CS-2301, B-Term 20091 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)

2 Lists and Trees (continued) CS-2301, B-Term 20092 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

3 Lists and Trees (continued) CS-2301, B-Term 20093 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

4 Lists and Trees (continued) CS-2301, B-Term 20094 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

5 Lists and Trees (continued) CS-2301, B-Term 20095 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

6 Lists and Trees (continued) CS-2301, B-Term 20096 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

7 Lists and Trees (continued) CS-2301, B-Term 20097 Question What order should you traverses the tree for Programming Assignment #5?

8 Lists and Trees (continued) CS-2301, B-Term 20098 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 - …

9 Lists and Trees (continued) CS-2301, B-Term 20099 Question What kind of traversal order is required for this expression? In-order? Pre-order? Post-order?

10 Lists and Trees (continued) CS-2301, B-Term 200910 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

11 Lists and Trees (continued) CS-2301, B-Term 200911 Questions about Trees? or about Programming Assignment 6?

12 Lists and Trees (continued) CS-2301, B-Term 200912 “Big O” notation New Topic

13 Lists and Trees (continued) CS-2301, B-Term 200913 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

14 Lists and Trees (continued) CS-2301, B-Term 200914 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

15 Lists and Trees (continued) CS-2301, B-Term 200915 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

16 Lists and Trees (continued) CS-2301, B-Term 200916 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

17 Lists and Trees (continued) CS-2301, B-Term 200917 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?

18 Lists and Trees (continued) CS-2301, B-Term 200918 Questions on Big-O?


Download ppt "Lists and Trees (continued) CS-2301, B-Term 20091 Lists and Trees (continued) CS-2301, System Programming for Non-Majors (Slides include materials from."

Similar presentations


Ads by Google