Solved

BST, anyone see whats wrong?

Posted on 2003-11-22
8
320 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

734 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