Note that there are some explanatory texts on larger screens.

plurals
  1. POinsert data into binary tree using BST_Insert ADT function
    primarykey
    data
    text
    <pre><code> #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;ctype.h&gt; #include &lt;string.h&gt; #include "BST_ADT.h" // Structure typedef struct { char* name; char* market; char* initial; float stock; }COMPANY; // Prototype Delarations void addComp (BST_TREE* list); void deleteComp (BST_TREE* list); void findComp (BST_TREE* list); void printList (BST_TREE* list); int compareComp (void* Comp1, void* Comp2); void processComp (void* dataPtr); int main (void) { // Local Definitions BST_TREE* list; // Statements list = BST_Create(compareComp); addComp(list); deleteComp(list); findComp (list); printList(list); return 0; } /*===================== addComp =========================*/ void addComp (BST_TREE* list) { // Local Declarations COMPANY* CompPtr; FILE* fp; char fileName[25]; char buffer [100]; char* tempString; // Statements CompPtr = (COMPANY*)malloc (sizeof (COMPANY)); CompPtr-&gt;name = (char*) malloc(128 * sizeof(char)); CompPtr-&gt;market = (char*) malloc(128 * sizeof(char)); CompPtr-&gt;initial = (char*) malloc(128 * sizeof(char)); tempString = (char*) malloc(128 * sizeof(char)); printf("Enter the file name: "); gets(fileName); fp = fopen(fileName, "r"); if(fp == NULL) { printf("Error cannot open the file!\n"); exit(101); } while(fgets(buffer, 100, fp) != NULL) { if (!CompPtr) printf("MEmory overflow!\n"), exit(101); strcpy(tempString, strchr(buffer, ';') + 2); CompPtr-&gt;name = strtok(buffer, ";"); sscanf(tempString, "%15s %15s %f ", CompPtr-&gt;market, CompPtr-&gt;initial, &amp;(CompPtr-&gt;stock)); BST_Insert(list, CompPtr); } // end while } //addComp /*===================== deleteComp =========================*/ void deleteComp (BST_TREE* list) { // local definitions char name[100]; char* namePtr = (char*) malloc(128 * sizeof(char)); namePtr = name; // statements printf("Enter Company name: "); scanf ("%39s", namePtr); if (!BST_Delete (list, namePtr)) printf("ERROR: No Company: %0\n", *namePtr); else BST_Delete(list, namePtr); } // deleteComp /*===================== findComp =========================*/ void findComp (BST_TREE* list) { // Local Definitions char initial[15]; COMPANY* CompPtr; // Statements printf("Enter Company initial: "); scanf("%s", initial); CompPtr = (COMPANY*)BST_Retrieve (list, &amp;initial); if (CompPtr) { printf("Company name: %s", initial); printf("market name: %s", *CompPtr-&gt;market); printf("company's initial: %s", *CompPtr-&gt;initial); printf("stock price is: %f", CompPtr-&gt;stock); } // if else printf("Company %s not in the file\n", initial); } /*===================== printList =========================*/ void printList (BST_TREE* list) { // Statements printf("\nCOMPANY list:\n"); BST_Traverse(list, processComp); printf("end of COMPANY list\n"); return; } // printList /*===================== compareComp =========================*/ int compareComp (void* Comp1, void* Comp2) { // Local definitions COMPANY s1; COMPANY s2; // Statements s1 = *(COMPANY*)Comp1; s2 = *(COMPANY*)Comp2; if (s1.initial &lt; s2.initial) return -1; if (s1.initial == s2.initial) return 0; return +1; } // compareComp /*===================== processComp =========================*/ void processComp (void* CompPtr) { // Local Definitions COMPANY aComp; // Statements aComp = *(COMPANY*) CompPtr; printf("%20s %20s %20s %4.1f\n", *aComp.name, aComp.market, aComp.initial, aComp.stock); return; } // processComp </code></pre> <p>In this code I'm trying to insert data from text file into binary tree. I was able read data into CompPtr , but when i passed it in to the BST_Insert function nothing was read read into the BST tree. Why doesn't the data add into BST tree? thank in advance for helping.</p> <p>this is my BST_Insert ADT function</p> <pre><code>/*===================== BST_Insert =========================*/ bool BST_Insert (BST_TREE* tree, void* DATA) { NODE* newPtr; newPtr = (NODE*)malloc(sizeof(NODE)); if (!newPtr) return false; newPtr-&gt;right = NULL; newPtr-&gt;left = NULL; newPtr-&gt;dataPtr = DATA; if (tree-&gt;count == 0) tree-&gt;root = newPtr; else _insert(tree, tree-&gt;root, newPtr); (tree-&gt;count)++; return true; } /*===================== _insert =========================*/ NODE* _insert (BST_TREE* tree, NODE* root, NODE* newPtr) { if (!root) return newPtr; if (tree-&gt;compare(newPtr-&gt;dataPtr, root-&gt;dataPtr) &lt; 0) { root-&gt;left = _insert(tree, root-&gt;left, newPtr); return root; } else { root-&gt;right = _insert(tree, root-&gt;right, newPtr); return root; } return root; } </code></pre> <p>after debugging for awhile, I realize that every time, the compare function, run, it return 0, due to the two parameter being equal, thus the _insert function only add data to right of the tree but not the left side.I think what the compare function is doing is compare the same value of input,what I want is for the compare function to compare the root value and the value that I want to add in order to determine which side should the data be added in.How should i fix this problem?sorry if this code is too long, I've posted this on code review as well, but it seems more appropriate to post here since the code is not complete.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload