void SortAll(struct YelpDataBST *root) { //Sort arrays int index1 = 0, index2 = 0; int ength1 = 0, Length2 = 0; if (root == NULL) { return } // Sorting of all arrays in current leaf while (root->Locations[Length2] != NULL) { while (tree->Locations[index1]->Reviews[Length1] != NULL) // gets review length { Length1++; } qsort(tree->Locations[index]->Reviews[0],Length1, sizeof(ReviewInfo),cmpfunc1); // sorts reviews Length2++; } qsort(tree->Locations[0],Length2,sizeof(LocInfo),cmpfunc2); // sorts locations // Go left SortAll(root->left); // Go right SortAll(root->right); }
int main(){ printf("\nLets create heap\n"); getch(); Heap* heap=CreateHeap(1); int n; printf("\nEnter the number of elements you want to create array with : "); scanf("%d",&n); int* Array=(int*)malloc(sizeof(int)*n); int i; printf("\nEnter elements of array\n"); for(i=0;i<n;i++){ scanf("%d",&Array[i]); } printf("\nEnter the value of k : "); int k; scanf("%d",&k); BuildHeap(heap,Array,k+1); printf("\nPress enter to get sort elements\n"); getch(); SortAll(heap,Array,n,k); printf("\nSorted\n\n"); //In place sorting in Array itself for(i=0;i<n;i++){ printf("%d ",Array[i]); } getch(); return 0; }
/******************* Functions *******************/ struct YelpDataBST *create_business_bst(const char* businesses_path, const char* reviews_path) {// Loads all businesss and reviews into YelpDataBST structure FILE *fp, *fp2; char LineBuffer[300], c, *BusID = "0", *Name, *Address,*City, *State; struct YelpDataBST *Root = NULL; int index = 0, LineStart = 0, BusIDL, NameL, AddressL, CityL, StateL, ZipL; int LocNumber = 0; char *BusIDRev = "0", *Stars, *Funny, *Useful, *Cool, LineBuffer2[6000]; int BusIDRevL, StarsL, FunnyL, UsefulL, CoolL, TextL, index2 = 0, LineStart2 = 0, index3 = 0; // Ensures file can open if (((fp = fopen(businesses_path,"r")) == NULL) || ((fp2 = fopen(reviews_path,"r")) == NULL)) { printf("Unable to open file\n"); return NULL; } // Reads file while (!feof(fp)) { LineStart = ftell(fp); // gets location at start of reading line while (((c = fgetc(fp)) != '\n') && !feof(fp)) // Gets one line of the file { LineBuffer[index++] = c; } LineBuffer[index] = '\0'; // Terminates string index = 0; // Resets index if (strlen(LineBuffer) > 0) // ensures line read isn't blank { // Gets individual strings BusID = strtok(LineBuffer,"\t"); Name = strtok(NULL,"\t"); Address = strtok(NULL,"\t"); City = strtok(NULL,"\t"); State = strtok(NULL,"\t"); //Zip = strtok(NULL,"\t"); // Converts strings to offsets (line offset + string lengths + null character) BusIDL = LineStart; NameL = BusIDL + strlen(BusID) + 1; AddressL = NameL + strlen(Name) + 1; CityL = AddressL + strlen(Address) + 1; StateL = CityL + strlen(City) + 1; ZipL = StateL + strlen(State) + 1; Root = Insert(Root, BusIDL, strdup(Name), AddressL, CityL, StateL, ZipL, &LocNumber); index3 = 0; // Get review data from seperate file CHANGE 5 TO 0 TO GO THROUGH LOOP while (strcasecmp(BusIDRev,BusID) == 0) // add reviews when the business ID's are the same { LineStart2 = ftell(fp2); // gets location at start of reading line while (((c = fgetc(fp2)) != '\n') && !feof(fp2)) // Gets one line of the file { LineBuffer2[index2++] = c; } LineBuffer2[index2] = '\0'; // Terminates string index2 = 0; // Resets index if (strlen(LineBuffer2) > 0) // ensures line read isn't blank { // Gets individual strings BusIDRev = strtok(LineBuffer2,"\t"); Stars = strtok(NULL,"\t"); Funny = strtok(NULL,"\t"); Useful = strtok(NULL,"\t"); Cool = strtok(NULL,"\t"); //Text = strtok(NULL,"\t"); // Converts strings to offsets (line offset + string lengths + null character) BusIDRevL = LineStart2; StarsL = BusIDRevL + strlen(BusIDRev) + 1; FunnyL = StarsL + strlen(Stars) + 1; UsefulL = FunnyL + strlen(Funny) + 1; CoolL = UsefulL + strlen(Useful) + 1; TextL = CoolL + strlen(Cool) + 1; if (strcasecmp(BusIDRev,BusID) != 0)// Check if BusIDs don't match, if so move pointer to beginning of the line { fseek(fp2,LineStart2,SEEK_SET); } else // else find business location and add reviews { struct YelpDataBST *TempBusID = FindBusiness(Name,Root); TempBusID->Locations[LocNumber]->num_reviews++; TempBusID->Locations[LocNumber]->Reviews[index3++] = CreateReviewInfo(TextL,StarsL); } } } } } SortAll(Root); fclose(fp); fclose(fp2); return Root; }