{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

cs32w09final

cs32w09final - CS32 Introduction to Computer Science II...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Discussion 3A Notes (Week 10, March 12) TA: Brian Choi ([email protected]) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Final Practice Big-O Provide the complexity of each algorithm below. 1. Assume N >> 5. for (int i = 0; i < N; i++) { for(int j = 0; j < i; j++) { for(int k = 0; k < 5; k++) { for(int l = 0; l < N; l += 2) { cout << arr[l] << endl; } } } } 2. Depth-First Traversal on a binary tree 3. Breadth-First Traversal on a binary tree 4. Suppose there is an array of n (~ 1 million) elements in a random order. You want to run a search and look for a certain item. What is the best you can do if: 1) you run a search once (i.e. “Is there 5 in the array?”) 2) you run search million times (i.e. “Is there 5? 16? 73? ...) Discuss the algorithm you would use for each case. You’re free to construct/destruct other data structures as you need them. 5. Which one takes longer? O(log n) O(n) O(n log n) O(n 2 ) O(n log n) O(n) O(n!) O(2 n ) CS32: Introduction to Computer Science II Winter 2009 Week 10, Page 1/6
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Binary Trees 6. Write a recursive function that counts the number of nodes in a binary tree. Use the following structure. struct Node { int val; Node* left; Node* right; }; int nodeCount(Node* root) { } 7. Write a one-line function that returns you the number of edges in a binary tree, using the function you defined above.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern