/* clean up all space used by a strategy */ void strategyDestroy(Strategy *s){ int i; Card tempCard; for (i = 0; i < s->numpiles; i++){ tempCard = treeDeleteMin(&s->pile[i]); while (tempCard != -1) { //delete until the binary tree is no more tempCard = treeDeleteMin(&(s->pile[i])); } } free(s->pile); free(s); }
/*This function takes in a pointer to a pointer to the root of a tree and a target string and deletes the target string from the tree if it exists there*/ static void treeDelete (struct node **root, const char* target) { struct node *toFree; /*pointer to the node we're deleting/freeing*/ int equality; /*integer to store results of strcmp*/ if (*root) { /*if root exists*/ if((equality = strcmp((*root)->key, target)) == 0) { /*if the root and target are same*/ if ((*root)->child[RIGHT]) { free((*root)->key); /*free key currently there, so we can set key to min of right child*/ (*root)->key = treeDeleteMin(&(*root)->child[RIGHT]); } else { /*if no right child --> just make left child the new root*/ toFree = *root; /*set pointer to free node*/ *root = toFree->child[LEFT]; /*set new root to left child*/ free(toFree->key); /*free string in node we're deleting*/ free(toFree); /*free old root node*/ } } else if (equality > 0) { /*root key > target --> look left*/ treeDelete(&(*root)->child[LEFT], target); } else { /*root key < target --> look right*/ treeDelete(&(*root)->child[RIGHT], target); } /*fix height and size data in tree, then rebalance it after the deletion*/ treeFix(*root); treeRebalance(root); } }
int main(int argc, char **argv) { Strategy *s; Card temp; int i; int j; const int n = 16; struct tree handBuiltroot; struct tree handBuiltLeft; struct tree *root = EMPTY_TREE; handBuiltroot.key = 3; handBuiltroot.child[LEFT] = &handBuiltLeft; handBuiltroot.child[RIGHT] = 0; handBuiltLeft.key = 1; handBuiltLeft.child[LEFT] = handBuiltLeft.child[RIGHT] = 0; treePrint(&handBuiltroot); /* new improved tree using treeInsert */ for(i = 0; i < n; i++) { treeInsert(&root, 5*i % n); treePrint(root); } /* get rid of root */ for(i = 0; i <= n; i++) { treeDeleteMin(&root); treePrint(root); } s = strategyCreate(4); for (j = 0; j < 10; j++) { strategyDeal(s, j); if (j % 2 == 0) { temp = strategyPlay(s, 3); } } treePrint(s->pile[3]); treePrint(s->pile[2]); return 0; }
/*this function deletes the minimum string in a tree and returns its value. Don't call on empty tree*/ static char* treeDeleteMin(struct node **root) { struct node* toFree; /*pointer to node we need to delete/free*/ char* retString; /*string to return*/ assert(*root); /*make sure tree isn't empty*/ if ((*root)->child[LEFT]) { /*if there's a left child --> recurse again, not at minimum yet*/ retString = treeDeleteMin(&(*root)->child[LEFT]); } else {/*if no left child --> reached minimum --> time to delete it*/ retString = (*root)->key; /*store return string value*/ toFree = *root; /*set toFree pointer to root node (node we want to delete)*/ *root = (*root)->child[RIGHT]; /*since removing this root, if it has a right child --> have root point to right child, otherwise, if no right child --> this node will just be set to 0*/ free(toFree); /*free memory for the node we're deleting*/ } /*now that we've removed something --> need to fix data and rebalance*/ treeFix(*root); treeRebalance(root); return retString; }
/* play a card from pile k */ Card strategyPlay(Strategy *s, int k){ Card move; move = treeDeleteMin(&(s->pile[k])); treeInsert(&(s->pile[k-1]), move); return move; }