c109f04f - CSE 109 Final Examination Tuesday 7 December...

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

CSE 109 Final Examination Tuesday 7 December 2004 >>>>>>>>>>>>>>>>>>>>>>>>>SUGGESTED ANSWERS<<<<<<<<<<<<<<<<<<<<< 1. a. Assume the balance of the balanced 20 binary tree to the right is maintained / \ using the algorithms discussed in class. 15 30 Draw the tree after 28 has been / \ / \ added to the original tree. Now 8 16 26 40 draw the tree after 39 has / \ / \ been added to the ORIGINAL 24 27 38 45 tree (i.e., assuming 28 was not added). n b. Assume the tree to the right is a 4 B-tree. Draw the tree after the numbers / \ 8, 9, 10, and 11 have been added to the tree 2 6 in that order / \ / \ 1 3 5 7 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> (Left to right, a.i, a.ii. b.) 26 30 / \ / \ 20 30 20 40 4 8 / \ / \ / \ / \ / | \ 15 24 27 40 15 26 38 45 2 6 10 /\ \ / \ / \ / \ \ / \ / \ / \ 8 16 28 38 45 8 16 24 27 39 1 3 5 7 9 11 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 2. Write a template for (the definition and code for) a class called List that allows the user to add items to the list (with the method add()), access items in the list (with the operator []), and get the number of items stored in the list (with the method size()). Below is code that should compile, given your template. List<int> x(20); x.add(2).add(20); for(int j=0;j<x.size(); j++) cout<<x[j]<<endl; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> template <class T> class List {public: List(int n=20); ~List(); List& add(const T&t); int size()const; T&operator[](int k); T operator[](int k)const; private: int count,max; T *data; static void check(bool b,char *mess); }; } template <class T> List<T>::List(int n):count(0),max(n) {check(n>0,"Bad list size"); data=new T[n]; check(data!=NULL,"Heap overflow"); }
Image of page 1

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

template <class T> List<T>::~List(){delete [] data;} L template <class T> List<T>& List<T>::add(const T&t) {check(count<max,"List overflow"); data[count]=t; count++; return *this; } template <class T> int List<T>::size()const {return count;} i template <class T> T & List<T>::operator[](int k) {check(k>=0 && k<count,"Index out of range"); return data[k]; } template <class T> T List<T>::operator[](int k)const
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '08
  • Kay
  • Algorithms, Jaime King, int ct

{[ 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