/*3*/void addName(BST* contactBST) { contactData dataList; int addStatus; printf("Enter name: "); fgets(dataList.name,MAX,stdin); printf("Enter phone: "); fgets(dataList.phoneNum,MAX,stdin); printf("Enter web address or hit <enter> for no address: "); fgets(dataList.webAddr,MAX,stdin); addStatus = insertBST(contactBST,(void*)dataList.name,(void*)dataList.phoneNum,(void*)dataList.webAddr); if(addStatus == -1){ //NOT in the insertBST function yet, need to incorporate printf("Duplicate Record\n"); } else if(addStatus == 1){ printf("Failed inserting, overflow\n"); } else{ printf("%s added",dataList.name); } }
struct dirNode* isValidPath(FileDescriptor *fd,struct dirNode* root,int *flag) { int l,exitStatus; int index=0; struct dirNode *parent; char temp_path[MAX_FULL_PATH_SIZE],path_part[MAX_FULL_PATH_SIZE]; char *tmp; char st[MAX_FULL_PATH_SIZE],prev[MAX_FULL_PATH_SIZE]; char *tk; tmp=(char*)malloc(sizeof(MAX_FULL_PATH_SIZE)); strcpy(temp_path,fd->fullPath); //strcpy(path_part,parsePath(temp_path)); tmp=parsePath(temp_path); //printf("\ntmp:%s\n",tmp); strcpy(path_part,tmp); if(root==NULL) { if(strcmp(path_part,"/")!=0) //for root path *flag=101; //wrong path trying to insert at root } else { exitStatus=FALSE; parent=root; while(exitStatus==FALSE) { if(strstr(path_part,parent->fileDesc->fullPath)==NULL) { if(parent->rightSibling==NULL) { if(strncmp(path_part,parent->fileDesc->fullPath,strlen(path_part)-1)!=0){ // *flag=PATH_NOT_FOUND; if(mhd.noOfFileDescriptors == MAX_NO_OF_FILE_DESCRIPTORS) { printf(ERR_VFS_MAKEDIR_01); *flag=401; return root; } else { FileDescriptor *tempfd1=(FileDescriptor*)malloc(sizeof(FileDescriptor)); strncpy(tempfd1->fullPath,path_part,strlen(path_part)-1); strcpy(st,path_part); tk=strtok(st,"/"); while(tk!=NULL) { strcpy(prev,tk); //printf("\nprev:%s ",prev); tk=strtok(NULL,"/"); } strcpy(tempfd1->fileName,prev); strcpy(tempfd1->fileType,"dir"); tempfd1->fileSize=0; tempfd1->locationBlockNo=-1; mhd.noOfFileDescriptors++; insertNAry(tempfd1,root,flag); insertBST(rootBST,tempfd1,&flag); index = fun_Hash(tempfd1->fileName[0]); array[index] = insertnode(array[index], tempfd1); } } exitStatus=TRUE; } else parent=parent->rightSibling; } else { if(parent->firstChild==NULL) { if(strncmp(path_part,parent->fileDesc->fullPath,strlen(path_part)-1)!=0) { if(mhd.noOfFileDescriptors == MAX_NO_OF_FILE_DESCRIPTORS) { printf(ERR_VFS_MAKEDIR_01); *flag=401; return root; } else { FileDescriptor *tempfd=(FileDescriptor*)malloc(sizeof(FileDescriptor)); strncpy(tempfd->fullPath,path_part,strlen(path_part)-1); strcpy(st,path_part); tk=strtok(st,"/"); while(tk!=NULL) { strcpy(prev,tk); //printf("\nprev:%s ",prev); tk=strtok(NULL,"/"); } strcpy(tempfd->fileName,prev); strcpy(tempfd->fileType,"dir"); tempfd->fileSize=0; tempfd->locationBlockNo=-1; mhd.noOfFileDescriptors++; insertNAry(tempfd,root,flag); insertBST(rootBST,tempfd,&flag); index = fun_Hash(tempfd->fileName[0]); array[index] = insertnode(array[index], tempfd); } } exitStatus=TRUE; } else { parent=parent->firstChild; } } } } return root; }
int main() { /* tree0 */ /* M */ /* / \ */ /* / \ */ /* A K */ /* / \ / \ */ /* C E F G */ /* / \ */ /* N P */ BTN N = {1,'N',(BTNP)0,(BTNP)0}; BTN P = {2,'P',(BTNP)0,(BTNP)0}; BTN C = {3,'C',(BTNP)0,(BTNP)0}; BTN G = {4,'G',(BTNP)0,(BTNP)0}; BTN E = {5,'E',&N,(BTNP)0}; BTN F = {6,'F',(BTNP)0,&P}; BTN A = {7,'A',&C,&E}; BTN K = {8,'K',&F,&G}; BTN M = {9,'M',&A,&K}; BTNP tree0 = &M; printf("The weight and height of tree0 are %d and %d.\n", weightBT(tree0), heightBT(tree0)); printf("tree0 preorder is "); print_preorder(tree0); printf("\ntree0 inorder is "); print_inorder(tree0); printf("\ntree0 postorder is "); print_postorder(tree0); printf("\n"); BTNP tree1=0; insertBST(4,'G',&tree1); insertBST(5,'E',&tree1); insertBST(3,'C',&tree1); insertBST(2,'P',&tree1); insertBST(7,'A',&tree1); insertBST(8,'K',&tree1); insertBST(1,'N',&tree1); insertBST(9,'M',&tree1); insertBST(6,'F',&tree1); // print_preorder(tree1); // printf("\n"); // BTN temp = {4, 'G', (BTNP) 0, (BTNP) 0}; // BTNP tempp = (BTNP) malloc(sizeof(BTN)); // *tempp = temp; // print_preorder(tempp); // BTNP temp2 = make_BTN(4, 'G', (BTNP) 0, (BTNP) 0); // print_preorder(temp2); // printf("\n%d\n", weightBT(temp2)); printf("The weight and height of tree1 are %d and %d.\n", weightBT(tree1), heightBT(tree1)); printf("tree1 preorder is "); print_preorder(tree1); printf("\ntree1 inorder is "); print_inorder(tree1); printf("\ntree1 postorder is "); print_postorder(tree1); printf("\n"); // BTNP tree2 = &N; // BTNP tree3 = findBST(0, tree2); // printf("%d\n", (int) *tree3); // printBTN(tree3); // printf("\n"); BTNP np; int i; for( i=0; i<11; i++){ np = findBST(i,tree1); printf( " key %2d",i) ; if( np ){ printf( " has name "); printBTN(np); printf("\n");; } else printf(" is not in tree1.\n"); } // findBST(4,tree1)->name = 'b'; insertBST(4,'b', &tree1); printf("After changing the name of key 4\n"); for( i=0; i<11; i++){ np = findBST(i,tree1); printf( " key %2d",i) ; if( np ){ printf( " has name "); printBTN(np); printf("\n");; } else printf(" is not in tree1.\n"); } return 0; }
/*6*/void lookupName(BST* contactBST) { char inputStr[MAX]; char changeRec, changeWhich; contactData* dataList; int strlength; int sucStatus = 0; while(sucStatus == 0){ fflush(stdin); printf("Enter name to look up: "); fgets(inputStr,MAX,stdin); strlength = strlen(inputStr); if(inputStr[strlength-1] == '\n'){ inputStr[strlength-1] = 0; } sucStatus = deleteBST(contactBST,(void*)inputStr,(void**)&dataList); //Use deleteBST instead if(sucStatus == 0){ printf("%s cannot be found in list\n\n",inputStr); inOrder(contactBST,printTree); printf("\n"); } else{ printf("%-18s\t%s %s\n", dataList->name, dataList->phoneNum, dataList->webAddr); printf("Change record? [y/n]: "); scanf(" %c",&changeRec); //Not working cause its not initialized if(changeRec == 'y' || changeRec == 'Y'){ printf("n. change name\nc. change contact info\n"); fscanf(stdin," %c",&changeWhich); if(changeWhich == 'n'){ fflush(stdin); printf("Enter name: "); fgets(inputStr,MAX,stdin); strlength = strlen(inputStr); if(inputStr[strlength-1] == '\n'){ inputStr[strlength-1] = 0; } strcpy(dataList->name,inputStr); insertBST(contactBST,(void*)dataList); } else if(changeWhich == 'c'){ fflush(stdin); printf("Enter phone or hit <enter> for no change: "); fgets(inputStr,MAX,stdin); strlength = strlen(inputStr); if(inputStr[strlength-1] == '\n'){ inputStr[strlength-1] = 0; } if(strcmp(inputStr,"\0") != 0){ //Means user wants a change strcpy(dataList->phoneNum,inputStr); } fflush(stdin); printf("Enter web address or hit <enter> for no change: "); fgets(inputStr,MAX,stdin); if(inputStr[strlength-1] == '\n'){ inputStr[strlength-1] = 0; } if(strcmp(inputStr,"\0") != 0){ //Means user wants a change strcpy(dataList->webAddr,inputStr); } insertBST(contactBST,(void*)dataList); } } } } }//end lookupName
//=================================================================== // Reads in the data from a file and inserts into BST and hashed // array. //=================================================================== void readInFile(DATA_HEAD *data) { FILE *dataFile; COMPANY *companyNode; char filename[MAX_CHARS], tmpName[MAX_CHARS]; char ch; int tmpRev, tmpProfit, tmpNumEmployee; int i = 0, isDuplicate, len, size; do { printf("Please enter a filename [enter for default]:"); fgets(filename, MAX_CHARS, stdin); if (filename[0] == '\n') strcpy(filename, default_file); else { //flush new line len = strlen(filename); if (filename[len - 1] == '\n' || filename[len - 1] == '\r') filename[len - 1] = '\0'; // change '\n' to '\0' else // no '\n' read, so flush to '\n' while ((ch = getchar()) != '\n' && ch != '\r'); } dataFile = fopen(filename, "r"); } while(!dataFile); //Creates structures data->pTree = createBST(myStringCompare); data->pStack = createStack(); size = getArrSize(dataFile); //need to reopen connection dataFile = fopen(filename, "r"); data->arraySize = size; data->count = 0; data->pHash = (HASH *)malloc(sizeof(DATA_HEAD) *data->arraySize); //initialize hashed array for (i = 0; i < data->arraySize; i++) { data->pHash[i].status = 0; data->pHash[i].numOfCollisions = 0; data->pHash[i].numOfProbes = 0; data->pHash[i].hashData = NULL; } //Reads in, parses, mallocs, assigns while (fscanf(dataFile, " %[^,],%d,%d,%d[^\n]", tmpName, &tmpRev, &tmpProfit, &tmpNumEmployee) != EOF) { companyNode = (COMPANY *)malloc(sizeof(COMPANY)); //Checks to see if allocated properly if (!companyNode) exit(1); companyNode->companyName = (char *)malloc(strlen(tmpName) + 1); //Checks to see if allocated properly if (!(companyNode->companyName)) exit(1); strcpy(companyNode->companyName, tmpName); companyNode->numberOfEmployees = tmpNumEmployee; companyNode->revenuePerBillion = tmpRev; companyNode->profitPerMillion = tmpProfit; //Inserts company data (name, revenue, profit, employees) into the BST //If duplicate, print an error isDuplicate = searchHash(data, tmpName, companyNode); if (isDuplicate == 1) printf("ERROR: DUPLICATE DATA\n"); else { insertHash(data, companyNode); insertBST(data->pTree, companyNode); data->count++; } } printf("\nNumber Of Data Records read in: %d\n\n", data->count); fclose(dataFile); }
int main(int argc, char** argv) { int i, n; struct TreeNode* bst = NULL; struct TreeNode* tree = makeTestTree(5,1); printf("test tree: "); printTree(tree); printf("tree leaves: "); printLeaves(tree); printf("tree depth = %d\n", maxDepth(tree)); printf("tree balanced = %d\n", isBalanced(tree)); printf("tree isBST = %d\n", isBST(tree)); freeTree(tree); tree = NULL; tree = makeTestTree(6,2); printf("another test tree: "); printTree(tree); printf("tree leaves: "); printLeaves(tree); printf("tree depth = %d\n", maxDepth(tree)); printf("tree balanced = %d\n", isBalanced(tree)); printf("tree isBST = %d\n", isBST(tree)); freeTree(tree); tree = NULL; tree = makeNotBST(); printf("notBST: "); printTree(tree); printf("notBST leaves: "); printLeaves(tree); printf("notBST depth = %d\n", maxDepth(tree)); printf("notBST balanced = %d\n", isBalanced(tree)); printf("notBST isBST = %d\n", isBST(tree)); printf("empty tree: "); printTree(bst); for(i = 0; i < 23; ++i) { n = (i*17+11) % 23; bst = insertBST(bst, n); } printf("filled BST: "); printTree(bst); printf("BST leaves: "); printLeaves(bst); printf("BST depth = %d\n", maxDepth(bst)); printf("BST minimum value = %d\n", minValueBST(bst)); printf("BST balanced = %d\n", isBalanced(bst)); printf("BST isBST = %d\n", isBST(bst)); for(i = -4; i < 25; i+=4) { n = removeBST(&bst, i); if(!n) printf("remove did not find %d\n", i); } printf("BST after removes: "); printTree(bst); printf("BST leaves: "); printLeaves(bst); printf("BST depth = %d\n", maxDepth(bst)); printf("BST minimum value = %d\n", minValueBST(bst)); printf("BST balanced = %d\n", isBalanced(bst)); printf("BST isBST = %d\n", isBST(bst)); freeTree(bst); bst = NULL; freeTree(tree); tree = NULL; return 0; }
void insert(int x, SplayTree tree){ tree_node *p; p = insertBST(x, tree); if(p) splay(p, tree); }