Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

BST, anyone see whats wrong?

Posted on 2003-11-22
8
Medium Priority
?
323 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 120 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

705 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