2

I'm working a project for class that requires creating a binary search tree of criminal names with up to 8 attributes per criminal.

I set up a string array att[] that will read in the attributes for each criminal, and then be passed to my BSTInsert class function. Through debugging I can see that the array is correct when it's just in the setupTree function. Once it's passed to BSTInsert, instead of having each string it only has one string, and on top of that nothing is copied from the array to the node in the tree.

Can anyone tell me what I'm doing wrong?

Here's my code for setting up the tree:

void setupTree(BST& criminals)
{
 ifstream fin("criminals.txt");
 string temp;
 fin >> temp;
 //FINISHED means it has all the criminals
 while (temp != "FINISHED")
 {
 //SUSPECT lets it know to read in a new name and new attributes
 if (temp == "SUSPECT")
 {
 string name;
 string att[8];
 int count = 0;
 fin >> temp;
 //if there is a false "suspect" line, quit
 if (temp == "FINISHED") return;
 name = temp;
 fin >> temp;
 while (temp != "SUSPECT" && temp != "FINISHED")
 {
 att[count] = temp;
 count++;
 fin >> temp;
 }
 criminals.BSTInsert(name, att, count);
 }
 }
}

Here's my class function for inserting a node:

bool BST::BSTInsert(treetype name, treetype att[], int count)
{
//gets the memory for the node. If unable, returns fail.
node* newNode = new node;
if (newNode == NULL)
{
 return false;
}
newNode->count = 0;
//initializes the node with the given information to place
for (int i = 0; i < count; i++)
{
 newNode->att[newNode->count] = att[count];
 newNode->count++;
}
newNode->name = name;
newNode->left = newNode->right = NULL;
//if the tree is empty, creates this node as the root
if (root == NULL)
{
 root = newNode;
 root->parent = NULL;
}
else
{
 //the tree is not empty, so it will use the parent to insert the node
 node* current = root;
 node* parent = NULL;
 //finds the insertion spot
 while (current != NULL)
 {
 parent = current;
 if (name <= current->name)
 {
 current = current->left;
 }
 else
 {
 current = current->right;
 }
 }
 //inserts the new node onto the correct side of the parent
 if (name <= parent->name)
 {
 parent->left = newNode;
 }
 else
 {
 parent->right = newNode;
 }
 newNode->parent = parent;
}
return true;
asked Apr 29, 2016 at 2:23
6
  • 2
    while (temp != "SUSPECT" temp != "FINISHED") ? Commented Apr 29, 2016 at 2:26
  • Are you sure it isn't just the debugger only showing one item as it doesn't know the length of the array. What happens if you try to display att[1] in the debugger Commented Apr 29, 2016 at 2:26
  • So, you know how to use a debugger. Great! Now, what does your debugger say happens inside the for loop in BSTInsert()? You know, the loop that attempts to copy the attributes from the att parameter to the att member in the new node? Why don't you try to step through this for loop, and see for yourself if the attributes are being copied correctly. Or not. Commented Apr 29, 2016 at 2:28
  • @TheDark in the setupTree function, the debugger displays the array correctly. However, after it's passed, the debugger shows the array as if it's a c-style string, with one character in each index, and it only does it for the first string that was in the array beforehand. Commented Apr 29, 2016 at 2:37
  • @SamVarshavchik thank you! I realized I was trying to copy from att[count], which was empty. I should've been using att[newNode->count]. Still not sure what the problem was with the debugger showing my array weirdly, but it seems to work now. Thanks again! Commented Apr 29, 2016 at 2:39

1 Answer 1

1

treetype att[] doesn't pass an array, it passes a pointer to an array - it decays to treetype att*.

That said, your problem is here:

for (int i = 0; i < count; i++)
{
 newNode->att[newNode->count] = att[count];
 newNode->count++;
}

This copies the wrong element of att (beyond the end of the array) into every att in newNode. What you meant was

for (int i = 0; i < count; i++)
{
 newNode->att[newNode->count] = att[newNode->count];
 newNode->count++;
}
answered Apr 29, 2016 at 2:37
Sign up to request clarification or add additional context in comments.

1 Comment

"mean't" as in "mean not"? :D

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.