Hi BST question I was wondering why my insert function works

Hi,

BST question


I was wondering why my insert function works perfectly to insert file data in the tree but doesnt when i insert single one

here is my function

int BSTInsert(BST *tree, BSTKey key, BSTValue value)
{
BSTNode *prev = NULL;
BSTNode *cur = tree->Root;

//
// first we search the tree to see if it already contains key:
//
while (cur != NULL)
{
    if (BSTCompareKeys(key, cur->Key) == 0) // already in tree, failed:
      return 0;
    else if (BSTCompareKeys(key, cur->Key) < 0) // smaller, go left:
    {
      prev = cur;
      cur = cur->Left;
    }
    else // larger, go right:
    {
      prev = cur;
      cur = cur->Right;
    }
}

//
// If we get here, tree does not contain key, so insert new node
// where we fell out of tree:
//
BSTNode *T = (BSTNode *)malloc(sizeof(BSTNode));
T->Key = key;
T->Value = value;
T->Left = NULL;
T->Right = NULL;

//
// link T where we fell out of tree -- after prev:
//
if (prev == NULL) // tree is empty, insert @ root:
{
    tree->Root = T;
}
else if (BSTCompareKeys(key, prev->Key) < 0) // smaller, insert to left:
{
    prev->Left = T;
}
else // larger, insert to right:
{
    prev->Right = T;
}

tree->Count++;

return 1; // success:
}

here i insert data from file



    fgets(buf, sizeof(buf), fp);
    buf[strcspn(buf, \"\ \ \")] = \'\\0\';
    token = strtok(buf, \"\\t\");
    y=atoi(token);
    value.Y = y;
    token=strtok(NULL, \"\ \");
    strcpy(x,token);
    value.X = (char *)malloc((strlen(x) + 1) * sizeof(char));
    strcpy(value.X, x);
    BSTInsert(tree, value.X, value);


while (fgets(buf, sizeof(buf), fp) != NULL)
{
     buf[strcspn(buf, \"\ \ \")] = \'\\0\';
    token = strtok(buf, \"\\t\");
    y=atoi(token);
    value.Y = y;
    token=strtok(NULL, \"\ \");
    strcpy(x,token);
    value.X = (char *)malloc((strlen(x) + 1) * sizeof(char));
    strcpy(value.X, x);
    BSTInsert(tree, value.X, value);
}

here i insert data from keyboard

   BSTValue value1;
      long long int weight;
      char      part2[512];
      int       part2size = sizeof(part2) / sizeof(part2[0]);

      //
      // add weight text
      //
      scanf(\"%lld %s\", &weight, phrase);
      fgets(part2, part2size, stdin);
      part2[strcspn(part2, \"\ \ \")] = \'\\0\'; // strip EOL char(s):
      strcat(phrase, part2);
     
     
   
    value1.Y = weight;
  
    value1.X = (char *)malloc((strlen(phrase) + 1) * sizeof(char));
    strcpy(value1.X, phrase);
    BSTInsert(tree, value1.X, value1);
   
    if(BSTInsert(tree, value1.X, value1)==1){
       printf(\"**Added\ \");
    }
       else
       {
           printf(\"**Not dded\ \");
       }

thanks in advance

Solution

int BSTInsert(BST *tree, BSTKey key, BSTValue value)
{
BSTNode *prev = NULL;
BSTNode *cur = tree->Root;

//
// first we search the tree to see if it already contains key:
//
while (cur != NULL)
{
    if (BSTCompareKeys(key, cur->Key) == 0) // already in tree, failed:
      return 0;
    else if (BSTCompareKeys(key, cur->Key) < 0) // smaller, go left:
    {
      prev = cur;
      cur = cur->Left;
    }
    else // larger, go right:
    {
      prev = cur;
      cur = cur->Right;
    }
}

//
// If we get here, tree does not contain key, so insert new node
// where we fell out of tree:
//
BSTNode *T = (BSTNode *)malloc(sizeof(BSTNode));
T->Key = key;
T->Value = value;
T->Left = NULL;
T->Right = NULL;

//
// link T where we fell out of tree -- after prev:
//
if (prev == NULL) // tree is empty, insert @ root:
{
    tree->Root = T;
}
else if (BSTCompareKeys(key, prev->Key) < 0) // smaller, insert to left:
{
    prev->Left = T;
}
else // larger, insert to right:
{
    prev->Right = T;
}

tree->Count++;

return 1; // success:
}

here i insert data from file



    fgets(buf, sizeof(buf), fp);
    buf[strcspn(buf, \"\ \ \")] = \'\\0\';
    token = strtok(buf, \"\\t\");
    y=atoi(token);
    value.Y = y;
    token=strtok(NULL, \"\ \");
    strcpy(x,token);
    value.X = (char *)malloc((strlen(x) + 1) * sizeof(char));
    strcpy(value.X, x);
    BSTInsert(tree, value.X, value);


while (fgets(buf, sizeof(buf), fp) != NULL)
{
     buf[strcspn(buf, \"\ \ \")] = \'\\0\';
    token = strtok(buf, \"\\t\");
    y=atoi(token);
    value.Y = y;
    token=strtok(NULL, \"\ \");
    strcpy(x,token);
    value.X = (char *)malloc((strlen(x) + 1) * sizeof(char));
    strcpy(value.X, x);
    BSTInsert(tree, value.X, value);
}

here i insert data from keyboard

   BSTValue value1;
      long long int weight;
      char      part2[512];
      int       part2size = sizeof(part2) / sizeof(part2[0]);

      //
      // add weight text
      //
      scanf(\"%lld %s\", &weight, phrase);
      fgets(part2, part2size, stdin);
      part2[strcspn(part2, \"\ \ \")] = \'\\0\'; // strip EOL char(s):
      strcat(phrase, part2);
     
     
   
    value1.Y = weight;
  
    value1.X = (char *)malloc((strlen(phrase) + 1) * sizeof(char));
    strcpy(value1.X, phrase);
    BSTInsert(tree, value1.X, value1);
   
    if(BSTInsert(tree, value1.X, value1)==1){
       printf(\"**Added\ \");
    }
       else
       {
           printf(\"**Not dded\ \");
       }

Hi, BST question I was wondering why my insert function works perfectly to insert file data in the tree but doesnt when i insert single one here is my function
Hi, BST question I was wondering why my insert function works perfectly to insert file data in the tree but doesnt when i insert single one here is my function
Hi, BST question I was wondering why my insert function works perfectly to insert file data in the tree but doesnt when i insert single one here is my function
Hi, BST question I was wondering why my insert function works perfectly to insert file data in the tree but doesnt when i insert single one here is my function

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site