Solved

BST, anyone see whats wrong?

Posted on 2003-11-22
8
315 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
 

Author Comment

by:killer455
ID: 9805161
So frequency should not be initialized to anything?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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++.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now