Solved

BST, anyone see whats wrong?

Posted on 2003-11-22
8
318 Views
Last Modified: 2012-05-04
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?

Code here: <dead link removed by CetusMOD>
0
Comment
Question by:killer455
  • 5
  • 3
8 Comments
 
LVL 4

Expert Comment

by:n_fortynine
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

by:n_fortynine
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

by:n_fortynine
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:killer455
ID: 9805161
So frequency should not be initialized to anything?
0
 
LVL 4

Expert Comment

by:n_fortynine
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

by:
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

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

Author Comment

by:killer455
ID: 9805176
oh ok nm
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question