Solved

# BST, anyone see whats wrong?

Posted on 2003-11-22
315 Views
Anyone know whats wrong with my code?
Wanting to keep a frequency count on each word.  Its not incrementing right or frequency is not initialized right or something.

Any ideas?

0
Question by:killer455
• 5
• 3

LVL 4

Expert Comment

ID: 9805132
You have a frequency variable for each Node, yet you initialize frequency = 0 in your Tree constructor and did not get a compile error? That's where the error lies.
0

LVL 4

Expert Comment

ID: 9805139
Also, in your InsertItem, I don't know what the purpose of setting current = t is, but you seem to want to point it to the newly created Node. If that's the case, then your line current = t; should be in the same else loop with your allocation.
>>else {
>>   t = new TreeNode(Item,0,0);
>>   current = t;
>>}
and not outside of it. Otherwise your pointer current will be misplaced since there possibly will me many recursive calls before it waiting to finish up.
0

LVL 4

Expert Comment

ID: 9805143
In your findNode, using return would be a safer habit than break.
>>if (current->data == searchItem) {
>>        break; // found match, store in BST current ptr
>>}
0

Author Comment

ID: 9805161
So frequency should not be initialized to anything?
0

LVL 4

Expert Comment

ID: 9805167
The best logic is: your frequency variable belongs to class TreeNode, thus it should be initialized by that class itself. So allow your TreeNode constructor to do frequency = 0 for you. Remember if you have n Nodes that's n frequency variables for you.
0

LVL 4

Accepted Solution

n_fortynine earned 30 total points
ID: 9805169
TreeNode(T newT, TreeNode *l, TreeNode *r)
{
data = newT;
left = l;
right = r;
frequency = 0;
}
0

Author Comment

ID: 9805175
How would i start frequency at 1?
0

Author Comment

ID: 9805176
oh ok nm
0

## Featured Post

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their waâ€¦
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base classâ€¦
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.