c109f04f

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

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

View Full Document Right Arrow Icon
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"); }
Background image of page 1

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

View Full DocumentRight Arrow Icon
template <class T> List<T>::~List(){delete [] data;} L template <class 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> {check(k>=0 & k return data[k]; } template <class T> T List<T>::operator[](int k)const {check(k>=0 & k
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

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

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online