/* Btree.java source : Btree.java compile: javac Btree.java run : java Btree levels) levels=depth; ptrs[depth][cnt[depth]] = p; cnt[depth]++; for (i=0; i<=p->count; i++) DFS(p->branch[i],depth+1); } } */ // Converted static void DFS(Node_type p, int depth) { int i; if (p != null) { if (depth>levels) levels=depth; ptrs[depth][cnt[depth]] = p; cnt[depth]++; for (i=0; i<=p.count; i++) DFS(p.branch[i],depth+1); } } // Converted except to uncomment Delete and PrintAllNodes static void interact() { char key; root = null; for (;;) { io.print("> "); String s = io.getLine(); if (s.length() > 0) { char cmd = s.charAt(0); switch (cmd) { case 'q' : return; case 'h' : io.println("h(elp; q(uit; i(nsert x; d(elete x; p(rint\n"); break; case 'i' : key = s.charAt(2); io.println("INSERT: "+ key); root=Insert(key,root); break; case 'd' : key = s.charAt(2); io.println("DELETE: "+key); // uncomment when Delete is converted // root=Delete(key,root); break; case 'p' : io.println("B-TREE:"); // uncomment when PrinAllNodes is converted // PrintAllNodes(root); break; } // end switch } // end if } // end for } /* BEGIN: ONE GIANT COMMENT // Convert #1 void ListNode(int depth,int cnt,Node_type *p) { int i; printf("%d %d ",depth,cnt); for (i=1; i<=p->count; i++) printf("%c ",p->key[i]); printf("\n"); } // Convert #2 void SetLoc() { int i,depth,pos,base,first,last,dist,x; depth = levels; pos = 0; for (i=0; icount)*2+2; } for (depth=levels-1; depth>=0; depth--){ base = 0; for (i=0; icount); dist = (loc[depth+1][last] - loc[depth+1][first] + 2*ptrs[depth+1][last]->count)/2; loc[depth][i] = loc[depth+1][first]+dist-ptrs[depth][i]->count; base += ptrs[depth][i]->count+1; } } } // Convert #3 void MoveRight(Node_type *p,int k) { int c; Node_type *t; t = p->branch[k]; for (c=t->count; c>0; c--){ t->key[c+1]=t->key[c]; t->branch[c+1]=t->branch[c]; } t->branch[1] = t->branch[0]; t->count++; t->key[1]=p->key[k]; t=p->branch [k-1]; p->key[k]=t->key[t->count]; p->branch[k]->branch[0]=t->branch[t->count]; t->count--; } // Convert #4 void MoveLeft(Node_type *p,int k) { int c; Node_type *t; t = p->branch[k-1]; t->count++; t->key[t->count] = p->key[k]; t->branch[t->count] = p->branch[k]->branch[0]; t = p->branch[k]; p->key[k] = t->key[1]; t->branch[0] = t->branch[1]; t->count--; for (c=1; c<=t->count; c++){ t->key[c] = t->key[c+1]; t->branch[c] = t->branch[c+1]; } } // Convert #5 void Combine(Node_type *p,int k) { int c; Node_type *q; Node_type *l; q = p->branch[k]; l = p->branch[k-1]; l->count++; l->key[l->count] = p->key[k]; l->branch[l->count] = q->branch[0]; for (c=1; c<=q->count; c++){ l->count++; l->key[l->count] = q->key[c]; l->branch[l->count] = q->branch[c]; } for (c=k; ccount; c++){ p->key[c] = p->key[c+1]; p->branch[c] = p->branch[c+1]; } p->count--; free(q); } // Convert #6 void Restore(Node_type *p, int k) { if (k==0) if (p->branch[1]->count > MIN) MoveLeft(p,1); else Combine(p,1); else if (k==p->count) if (p->branch[k-1]->count > MIN) MoveRight(p,k); else Combine(p,k); else if (p->branch[k-1]->count> MIN) MoveRight(p,k); else if (p->branch[k+1]->count> MIN) MoveLeft(p,k+1); else Combine(p,k); } // Convert #7 void Remove(Node_type *p,int k) { int i; for (i=k+1; i<=p->count; i++){ p->key[i-1] = p->key[i]; p->branch[i-1] = p->branch[i]; } p->count--; } // Convert #8 void Successor(Node_type *p, int k) { Node_type *q; for (q=p->branch[k]; q->branch[0]; q=q->branch[0]) ; p->key[k]=q->key[1]; } // Convert #9 void PushIn(Key_type x,Node_type *xr,Node_type *p, int k) { int i; for (i = p->count; i > k; i--) { p->key[i+1] = p->key[i]; p->branch[i+1] = p->branch[i]; } p->key[k+1] = x; p->branch[k+1] = xr; p->count++; } // Convert #10 // How can parameters be call-by-ref in Java? Not allowed to make global. // Try to think of a simple - but good OO - way to handle all of these // call-by-ref params in the source. You do not want to make a mistake // or the program will be impossible to debug. void PrintNode(Node_type *p, int depth, int i, int *pos) // CALL-BY-REF { int j; while ((*pos)count; j++){ printf("%c,",p->key[j]); (*pos) += 2; } printf("%c]",p->key[j]); (*pos)+=3; } // Convert #11 void PrintTop(Node_type *p, int depth, int i, int *pos, int *child) // CALL-BY-REF { int j,strt,mid; mid = loc[depth][i]+p->count; for (j=0; j<=p->count; j++) { strt = loc[depth+1][*child]+1; if (strt>=mid) strt += 2*p->branch[j]->count-2; while ((*pos)count]+p->branch[p->count]->count*2-2; mid = loc[depth][i]+p->count; *child += p->count+1; while ((*pos)key[1]) { *k = 0; return FALSE; } else { *k = p->count; while ((targetkey[*k]) && *k > 1) { (*k)--; step++; } return (target==p->key[*k]); } } // Convert #15 void Split(Key_type x, Node_type *xr, Node_type *p, int k, Key_type *y, Node_type **yr) // CALL-BY-REF { int i; int median; if (k <= MIN) median = MIN; else median = MIN + 1; *yr = (Node_type *)malloc(sizeof(Node_type)); for (i = median+1; i <= MAX; i++) { (*yr)->key[i-median] = p->key[i]; (*yr)->branch[i-median] = p->branch[i]; } (*yr)->count = MAX - median; p->count = median; if (k <= MIN) PushIn(x,xr,p,k); else PushIn(x,xr,*yr,k - median); *y = p->key[p->count]; (*yr)->branch[0] = p->branch[p->count]; p->count--; } // Convert #16 Bool PushDown(Key_type newkey,Node_type *p,Key_type *x, Node_type **xr) // CALL-BY-REF { int k; if (p == NULL) { *x = newkey; *xr = NULL; return TRUE; } else { if (SeqSearch(newkey,p,&k)) Error("inserting duplicate key"); if (PushDown(newkey,p->branch[k],x,xr)) if (p->count < MAX) { PushIn(*x,*xr,p,k); return FALSE; } else { Split(*x,*xr,p,k,x,xr); return TRUE; } return FALSE; } } // Convert #17 Bool RecDelete(Key_type target,Node_type *p) { int k; Bool found; if (p==NULL) return FALSE; else { found=SeqSearch(target,p,&k); if (found) if (p->branch[k-1]) { Successor(p,k); if (!(found=RecDelete(p->key[k],p->branch[k]))) Error("Key not found."); } else Remove(p,k); else found=RecDelete(target,p->branch[k]); if (p->branch[k] != NULL) if (p->branch[k]->countcount == 0) { p = root; root = root->branch[0]; free(p); } return root; } END: ONE GIANT COMMENT */ // C version Node_type *Insert(Key_type newkey, Node_type *root) // Convert #19 static Node_type Insert(char newkey, Node_type root) { /* NEED TO CONVERT THE BODY Key_type x; Node_type *xr; Node_type *p; Bool pushup; pushup = PushDown(newkey,root,&x,&xr); if (pushup) { p = (Node_type *)malloc(sizeof(Node_type)); p->count = 1; p->key[1] = x; p->branch[0] = root; p->branch[1] = xr; return p; } */ return root; } } // end Btree class