14S-Section-Solution - CS107 Spring 2007 Handout 14S April...

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

View Full Document Right Arrow Icon
CS107 Handout 14S Spring 2007 April 20, 2007 Section Solution This sortedset thing should try to conform as much as possible to the pictures. There should be a slab of memory to store kInitialCapacity nodes in addition to that admittedly annoying int all the way to the left. We need to remember how much space to allocate and how much of that allocated space is in use. We need to know how large client elements are so we can get the node allocations right. We also need to store the comparison function (since only it and not the sortedset implementation knows how to compare the mystery bytes so the set functions can travel left or right accordingly.) Once that is done, the SetNew implementation can more or less allocate everything and initialize the thing to behave as an empty tree. typedef struct { int *root; // points to the offset index of the root. int logicalSize; // number of active client elements currently stored. int allocatedSize; // number of elements needed to saturate memory. int (*cmp)(const void *, const void *); int elemSize; // client element size. } sortedset; Clearly there is an array flavor to this implementation. In particular, we need to manage an array (called root above) that identifies the location of the slate of bytes that store the nodes and the integers. I could have typed this as a void * instead, but I chose int * because the smallest entity that resides at this address is the integer that is initially -1 but becomes 0 the moment that anything is added. Our doubling strategy will often leave us with raw, unused memory, so we'll need to track how many nodes there are and how many are being used. cmp is there so we remember how to maintain the BST property, and elemSize is there so we know how much storage is wedged in between the integer index pairs. /** * Function: SetNew
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/14/2010 for the course CS 107 taught by Professor Cain,g during the Spring '08 term at Stanford.

Page1 / 6

14S-Section-Solution - CS107 Spring 2007 Handout 14S April...

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

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