struct node* insert( struct node* root, int studentId, struct item* item ) { // returns root node struct nodeIndex* nodeIndex = (struct nodeIndex*) nodeIndexForKey( root, studentId ); if ( nodeIndex->wasFound ) { // quick search to determine if studentId is already in tree nodeIndex->node->courseList[ nodeIndex->index ] = InsertItem( nodeIndex->node->courseList[ nodeIndex->index ], item ); } else { int i = root->numChildren; if ( i == 4 ) { // if node has no free slots struct node* node = (struct node*) malloc( sizeof(struct node)+1 ); // allocate new node node->isLeafNode = NO; node->numChildren = 1; node->children[0] = root; // new node's first child is root node->keys[0] = root->keys[3]; // first child of new node is greatest child of root root = node; node = split( node, 0 ); if ( studentId > node->keys[1] ) { // if studentId > max of newly created root node = insertMax( node, studentId, item ); } else { node = insertNonfull( node, studentId, item ); } } else { int j = i != 0 ? i-1 : 0; if ( studentId > root->keys[j] ) { // if studentId > the the node's greatest child root = insertMax( root, studentId, item ); // then studentId is the max for this node } else { root = insertNonfull( root, studentId, item ); // else insert amidst the children nodes } } } return root; }// insert
void insertMed(int *H,int n,int& N1,int& N2,int x) // inserts an element in the med heap { int m=findMedian(H,n,N1,N2); if(m==-1){H[n-1]=x;N2++;return;} if(x<=m) { if(N2==N1+1)insertMax(H,N1,x); else { int max=findMax(H,N1);deleteMax(H,N1); insertMin(H+n-N2,N2,max);insertMax(H,N1,x); } } else { if(N2==N1)insertMin(H+n-N2,N2,x); else { int min=findMin(H+n-N2,N2);deleteMin(H+n-N2,N2); insertMax(H,N1,min);insertMin(H+n-N2,N2,x); } } }
struct node* insertMax( struct node* node, int studentId, struct item* item ) { int i = node->numChildren; node->keys[i] = studentId; // node's greatest key is studentId if ( node->isLeafNode == YES ) { node->courseList[i] = InsertItem( node->courseList[i], item ); node->numChildren = node->numChildren + 1; } else { if ( node->children[i-1]->numChildren == 4 ) { // if slotting in studentId made 4 children node = split( node, i ); i++; } node = insertMax( node->children[i-1], studentId, item ); } return node; } // insertMax