1 / 126

Mid-term Answers & Tree traversal

Mid-term Answers & Tree traversal. 10/15/2014. How to Measure Efficiency?. Critical resources: Time, memory, programmer effort, user effort Factors affecting running time: For most algorithms, running time depends on “ size ” of the input .

brett-nolan
Télécharger la présentation

Mid-term Answers & Tree traversal

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Mid-term Answers & Tree traversal 10/15/2014

  2. How to Measure Efficiency? • Critical resources: • Time, memory, programmer effort, user effort • Factors affecting running time: • For most algorithms, running time depends on “size” of the input. • Running timeis expressed as T(n)for some function T on input size n. 4

  3. How do we analyze an algorithm? • Need to define objective measures. (1)Compare execution times? Not good: times are specific to a particular machine. (2)Count the number of statements? Not good: number of statements varies with programming language and style.

  4. How do we analyze an algorithm? (cont.) (3) Express running time T as a function of problem size n (i.e., T=f(n)) Asymptotic Algorithm Analysis • Given two algorithms having running times f(n) and g(n),find which functions grows faster? • Compare “rates of growth”of f(n) and g(n). • Such an analysis is independent of machine time, programming style, etc.

  5. Understanding Rate of Growth (cont’d) • The low order terms of a function are relatively insignificant for largen n4 + 100n2 + 10n + 50 Approximation: n4 • Highest order termdetermines rate of growth!

  6. Names of Orders of Magnitude O(1) bounded (by a constant) time O(log2N) logarithmic time O(N) linear time O(N*log2N) N*log2N time O(N2) quadratic time O(N3)cubic time O(2N ) exponential time

  7. Visualizing Orders of Growth • On a graph, as you go to the right, a faster growing function eventually becomes larger...

  8. Complexity • Let us assume two algorithms A and B that solve the same class of problems. • The time complexity of A is 5,000n, T = f(n) = 5000*n • the one for B is 2nfor an input with n elements, T= g(n) = 2n • For n = 10, • A requires 5*104steps, • but B only 1024, • so B seems to be superior to A. • For n = 1000, • A requires 5*106steps, • while B requires 1.07*10301steps.

  9. Running Time Examples (cont.’d) if (i<j) for ( i=0; i<N; i++ ) X = X+i; else X=0; O(N) O(1) Running time of the entire if-else statement? Max (O(N), O(1)) = O(N)

  10. Some of the wrong solutions: ,

  11. Running Time Examples i = 0; while (i<N) { X=X+Y; // O(1) result = mystery(X); // O(N) i++; // O(1) } • The body of the while loop: O(N) • Loop is executed: N times Running time of the entire iteration? N x O(N) = O(N2)

  12. 17 Main Index Contents C++ Arrays An array is a fixed-size collection of values of the same data type. An array is a container that stores the n (size) elements in a contiguous block of memory. intarr[] = {1,2,3,4,5}; cout << arr[4];

  13. Allow for dynamic resizing Have a way to store the size internally Allow for assignment of one object to another 18 Main Index Contents Vectors • A container is a class that stores a collection of data • It has operations that allow a programmer to insert, • erase, and update elements in the collection

  14. C++ interview questions on “Vector” What do vectors represent?a) Static arraysb) Dynamic arraysc) Stackd) Queue Answer: bExplanation: Vectors are sequence containers representing arrays that can change in size. More questions here

  15. Inserting into a List Container 22 Main Index Contents The List Container

  16. 25 Main Index Contents Map Containers A map is a storage structure that implements a key-value relationship.

  17. 1 2 3 4 5 1 2 3 4 5

  18. Stacks • A stack is a sequence of items that are accessible at only one end of the sequence. Last in, first out, (LIFO)

  19. Pushing/Popping a Stack • Because a pop removes the item that last added to the stack, we say that a stack has LIFO (last-in/first-out) ordering.

  20. What would be the output on screen?

  21. Queues A queue inserts new elements at the back and removes elements from the front of the sequence. Pop Push Queue: First in, first out, (FIFO)

  22. 33 Main Index Contents The Queue A Queue is a FIFO (First in First Out) Data Structure. Elements are inserted in the Rear of the queue and are removed at the Front.

  23. Example Question F B G A D I C E H Pre-order? In-order? Post-order? Level-order ?

  24. WIKI

  25. Computing the Leaf Count Pre-order scan

  26. Computing the Depth of a Tree Post-order scan

  27. Deleting Tree Nodes Post-order scan

  28. Outline of In-Order Traversal • Three principle steps: • Traverse Left • Do work (Current) • Traverse Right • Work can be anything • Separate work from traversal

  29. Traverse the tree “In order”: • Visit the tree’s left sub-tree • Visit the current and do work • Visit right sub-tree

  30. In-Order Traversal Procedure procedure In_Order(cur iot in Ptr toa Tree_Node) // Purpose: perform in-order traversal, call // Do_Something for each node // Preconditions: cur points to a binary tree // Postcondition: Do_Something on each tree // node in “in-order” order • if( cur <> NIL ) then • In_Order( cur^.left_child ) • Do_Something( cur^.data ) • In_Order( cur^.right_child ) • endif endprocedure // In_Order

  31. 22 67 36 3 14 44 7 94 97 1 9 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L P R

  32. 22 67 36 3 14 44 7 94 97 1 9 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L P R

  33. 22 67 36 3 14 44 7 94 97 1 9 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L P R

  34. 7 97 94 44 14 1 36 67 9 22 3 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R

  35. 7 97 94 44 14 1 36 67 9 22 3 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R

  36. 7 97 94 44 14 1 36 67 9 22 3 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L

More Related