GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Apr-2006, 02:21
bioeng_mtm bioeng_mtm is offline
New Member
 
Join Date: Apr 2006
Posts: 19
bioeng_mtm is on a distinguished road

can anyone help me with my tree :)


hi there
I'm new member here and also I'm kindda new user in C++ it's my 1st course in it. I was asked in the college to do a project that allow us to search a text file, and order words in it in des. order using Binary Search Tree... I'd started with it coding 1st my BST with I'll use, so I didn't compelete my task yet, but I need some help with my BST... after coding it and there was no errors at all I'm not able to insert items to my tree using InsertItem function that I'd built the proggrame runs but no item is added, so can anyone check this up and try to help me, I'd be veryyyyyy thankful
and here's the code(the class header , the class CPP. and the main )
************just concentrate on te function InsertItem and its use
CPP / C++ / C Code:
//Tree.h
////////////////////////////////////////////////////////////////////////////////
//Class Name :Tree 
//Owner :Mohamed Talaat 
//Creation Date : 20 April 2006 
//Function : implementing BST 
////////////////////////////////////////////////////////////////////////////////
#ifndef TREE
#define TREE
//include header files
#include<iostream>
#include<string>
//include namesapce
using namespace std;
////////////////////////////////////////////////////////////////////////////////
struct node
{
string data;
node *left;
node *right;
};
////////////////////////////////////////////////////////////////////////////////
//class defination
class Tree
{
public:
Tree();
~Tree();
bool IsEmpty();
int LengthIs();
void RetrieveItem(string& item, bool found);
void InsertItem(string item);
void DeleteItem(string item);
void print(std::ofstream& outFile);
private:
node *root;
};//end of class Tree
#endif

CPP / C++ / C Code:
//Tree.cpp
////////////////////////////////////////////////////////////////////////////////
//Class Name :Tree 
//Owner :Mohamed Talaat 
//Creation Date : 20 April 2006 
//Function : implementing BST 
////////////////////////////////////////////////////////////////////////////////
//include header files
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include"Tree.h"
//include namespace
using namespace std;
////////////////////////////////////////////////////////////////////////////////
//function Name : Tree()
//purpose : default constructor
//input :
//output :
////////////////////////////////////////////////////////////////////////////////
Tree::Tree()
{
root = NULL;
}
////////////////////////////////////////////////////////////////////////////////
//function Name : Tree()
//purpose : default destructor
//input :
//output :
////////////////////////////////////////////////////////////////////////////////
void Destroy(node* tree);
Tree::~Tree()
{
Destroy(root);
}
void Destroy(node* tree)
{
if( tree != NULL)
{
Destroy(tree->left);
Destroy(tree->right);
delete tree;
}
}
////////////////////////////////////////////////////////////////////////////////
//function Name : IsEmpty()
//purpose : check if there is no item in the linked list
//input :
//output : true if the list is empty , false otherwise
////////////////////////////////////////////////////////////////////////////////
bool Tree::IsEmpty()
{
return root == NULL;
}
////////////////////////////////////////////////////////////////////////////////
//function Name : LengthIs()
//purpose : dtermine the length of a tree
//input : 
//output : number of elements in the tree
////////////////////////////////////////////////////////////////////////////////
int CountNodes(node *tree);
int Tree::LengthIs()
{
return CountNodes(root);
}
int CountNodes(node *tree)
{
if ( tree == NULL)
return 0;
else
return CountNodes(tree->left)+CountNodes(tree->right)+1;
}
////////////////////////////////////////////////////////////////////////////////
//function Name : RetrieveItem()
//purpose : retrieve the data whose key matches item's key ( if present) 
//input : key member of data.
//output : data will be copied to item and found = true if found
// othewise the item will kpet unchanged and found = false
////////////////////////////////////////////////////////////////////////////////
void Retrieve(node *tree, string& item , bool& found);
void Tree::RetrieveItem(string& item, bool found)
{
Retrieve(root,item,found);
}
void Retrieve(node *tree, string& item , bool& found)
{
if ( tree == NULL)
found = false;
else if (item< tree->data)
Retrieve(tree->left,item,found);
else if (item> tree->data)
Retrieve(tree->right,item,found);
else
{
item = tree->data;
found = false;
}
}
////////////////////////////////////////////////////////////////////////////////
//function Name : insertItem()
//purpose : insert an item to the tree
//input : item : the item which needed to be added 
//output : 
////////////////////////////////////////////////////////////////////////////////
void findnode(node* tree, string item, node*& nodeptr, node*& parentptr);
void Tree::InsertItem(string item)
{
node* newnode;
node* nodeptr;
node* parentptr;
newnode = new node;
newnode->data = item;
newnode->left = NULL;
newnode->right = NULL;
findnode(root,item,nodeptr,parentptr);
if ( parentptr==NULL )
root=newnode;
if ( item < parentptr->data )
parentptr->left=newnode;
else
parentptr->right = newnode;
}
void findnode(node* tree, string item, node*& nodeptr, node*& parentptr)
{
nodeptr = tree;
parentptr = NULL;
bool found = false;
while (nodeptr != NULL && !found )
{
if ( item< nodeptr->data)
{
parentptr = nodeptr;
nodeptr = nodeptr->left;
}
else if ( item> nodeptr->data )
{
parentptr = nodeptr;
nodeptr = nodeptr->right;
}
else
{
found = true;
}
}
}
////////////////////////////////////////////////////////////////////////////////
//function Name : DeleteItem()
//purpose : delete an item from the tree
//input : item : the item which needed to be deleted 
//output : 
////////////////////////////////////////////////////////////////////////////////
void DeleteNode(node*& tree);
void Delete(node*& tree, string item);
void Tree::DeleteItem(string item)
{
Delete(root,item);
}
void Delete(node*& tree, string item)
{
if ( item < tree->data)
Delete(tree->left,item);
else if ( item > tree->data)
Delete(tree->right,item);
else
DeleteNode(tree);
}
void GetPredecessor(node* tree, string info);
void DeleteNode(node*& tree)
{
string info;
node* temp;
temp=tree;
if (tree->left==NULL)
{
tree=tree->right;
delete temp;
}
else if (tree->right==NULL)
{
tree=tree->left;
delete temp;
}
else
{
GetPredecessor(tree->left,info);
tree->data=info;
Delete(tree->left,info);
}
}
void GetPredecessor(node* tree, string info)
{
while (tree->right!=NULL)
tree = tree->right;
info = tree->data;
}
////////////////////////////////////////////////////////////////////////////////
//function Name : print()
//purpose : save the Tree's data in a file
//input : outFile : the file where the data will be saved
//output : 
////////////////////////////////////////////////////////////////////////////////
void PrintTree(node* tree, std::ofstream& outFile);
void Tree::print(std::ofstream& outFile)
{
PrintTree(root,outFile);
}
void PrintTree(node* tree, std::ofstream& outFile)
{
if ( tree != NULL )
{
PrintTree(tree->left, outFile);
outFile<<tree->data;
PrintTree(tree->right, outFile);
}
}
and here's the main:
CPP / C++ / C Code:
//main.cpp
#include<iostream>
#include<conio.h>
#include<fstream>
#include<cstdlib>
#include<string>
#include"Tree.h"
using namespace std;
void main()
{
Tree mytree;
mytree.InsertItem("HI");
int n = mytree.LengthIs();
cout<<"number of elements in the tree is:"<<n<<endl;
}

I've forgot something that I built another InsertItem function but it doesn't work aslo :S
here it is :
CPP / C++ / C Code:
void Insert(node* tree , string item);
void Tree::InsertItem(string item)
{
Insert(root,item);
}
void Insert(node* tree , string item)
{
if ( tree == NULL)
{
tree = new node;
tree->data = item;
tree->left = NULL;
tree->right = NULL;
}
else if ( item< tree->data)
Insert(tree->left,item);
else 
Insert(tree->right,item);
}
  #2  
Old 22-Apr-2006, 08:51
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,702
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: can anyone help me with my tree :)


Quote:
Originally Posted by bioeng_mtm
new user in C++

Well, for a new user, you have done a lot of work, and the first sign of success is very encouraging: getting the blasted stuff to compile with no errors. That's a Big Step.

Now, for the good stuff: Debugging.

Since you have a program that executes (even though it does the "wrong thing"), make the program tell you exactly what's happening: where is it going and what is it seeing at each step.

The first thing I would do in main() would be to try to find how far it gets:

So, my main() might look like this:
CPP / C++ / C Code:
//main.cpp
#include<iostream>
#include"Tree.h"
using namespace std;

int main()
{

  cout << "Instantiating mytree" << endl;
  Tree mytree;

  cout << "Calling InsertItem(\"HI\")" << endl;
  mytree.InsertItem("HI");

  cout << "Calling LengthIs()" << endl;

  int n = mytree.LengthIs();
  cout<<"number of elements in the tree is:"<<n<<endl;
  return 0;
}

(Note that I changed it to comply with the C++ standard: main() is an int type. I eliminated extra #include files, just because I don't like clutter. You do whatever works for you, but I suggest that you get out of the "void main()" habit. Some compilers will simply not accept that, so why do it?)

Now, I put a print statement before each function call. That way, I can see if it gets back from one function and calls the next. When the program bombs (and it does for me), I know how far it got. Note that I don't necessarily know that everything before the bomb works OK, but at least I know how far it got.

Let's say you get an output like this:

Code:
Instantiating mytree Calling InsertItem("HI")

And then the program bombs (or maybe it doesn't bomb, but it doesn't print anything else). Then I feel that the problem is probably in the InsertItem() function. Now what?

Well, I instrument the function to see how far it gets there. So, I might change the function to look something like this:

CPP / C++ / C Code:
void Tree::InsertItem(string item)
{
  cout << "In InsertItem() item <" << item << ">" << endl;
  node* newnode;
  node* nodeptr;
  node* parentptr;
  newnode = new node;
  newnode->data = item;
  newnode->left = NULL;
  newnode->right = NULL;
  cout << "Calling findnode()" << endl;
  findnode(root,item,nodeptr,parentptr);
  cout << "Back from findnode()" << endl;
     
  if ( parentptr==NULL ) {
    cout << "parentptr == NULL" << endl;
    root=newnode;
  }

  if ( item < parentptr->data ) {
    cout << "item < parentptr->data" << endl;
    parentptr->left=newnode;
  }
  else {
    cout << "item not less than parentptr->data" << endl;
    parentptr->right = newnode;
  }

}

Now, I have put a "progress report" at almost every stinkin' line of the function. In general the function might be too big to put something at every line, so I might choose a strategic subset (before every function call; before statements that dereference pointers).

Not everyone works this way, but I suggest that it is very valuable to be able to see how use the program itself to debug the program.

I suggest that you can try something like this to see exactly what your program is seeing at each step.

Maybe the actual problem was in some previous statement (in some other function) that didn't leave things in correct state for this function to work. If you suspect that then what? Instrument the other stuff to make the program tell you what the heck is going on.

Regards,

Dave
  #3  
Old 22-Apr-2006, 09:35
bioeng_mtm bioeng_mtm is offline
New Member
 
Join Date: Apr 2006
Posts: 19
bioeng_mtm is on a distinguished road

Re: can anyone help me with my tree :)


thanks very much dave for helping me with my code...
well, I tryed the way that you told me to trace my code and here's the output that I got:

Quote:
Instantiating mytree
calling InsertItem("HI")
in InsertItem() item(HI)
calling findnode()
back from findnode()
parentptr == NULL

then it bombs

so I am not able figure out where the problem is, because it doesn't even call lengthIs() function so I don't think that the problem is in this function and at the same time it compeletes the calling of InsertItem() so I'm stuck :s
  #4  
Old 22-Apr-2006, 11:32
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,702
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: can anyone help me with my tree :)


Quote:
Originally Posted by bioeng_mtm
thanks very much dave for helping me with my code...
well, I tryed the way that you told me to trace my code and here's the output that I got:



then it bombs

so I am not able figure out where the problem is, because it doesn't even call lengthIs() function so I don't think that the problem is in this function and at the same time it compeletes the calling of InsertItem() so I'm stuck :s

What's the thing that the program does next:

CPP / C++ / C Code:
  if ( parentptr==NULL ) {
    cout << "parentptr == NULL" << endl;
    root=newnode;
    cout << "set root equal to newnode" << endl;
  }

  cout << "parentptr: " << parentptr << endl;
  if ( item < parentptr->data ) {
    cout << "item < parentptr->data" << endl;
    parentptr->left=newnode;
  }

If parentptr is equal to NULL, then dereferencing it is illegal (the statement that uses parentptr->). So, that's surely where it bombs. The question is: how can parentptr be NULL? The program surely expects it to be a pointer to something that can be used here. What part of the program is supposed to set parentptr to something valid.

It's like a detective story: you have found a major clue; now you have to collar the murderer.

Dave
  #5  
Old 22-Apr-2006, 12:15
bioeng_mtm bioeng_mtm is offline
New Member
 
Join Date: Apr 2006
Posts: 19
bioeng_mtm is on a distinguished road

Re: can anyone help me with my tree :)


I figured out where the problem is , and I'm soooooooo sorry for bothering you , and thanks very much once again for helping me
  #6  
Old 22-Apr-2006, 12:50
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,702
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: can anyone help me with my tree :)


Quote:
Originally Posted by bioeng_mtm
I figured out where the problem is
Was this one of those "***Aha!*** moments? It's funny that some people really hate that, since they feel that they should have seen it before. I, on the other hand really, really like it when I can locate the exact place where the bad boy is screwing up the works and I can mark "problem solved" in my log. (Now, it's time to back everything up so that if further tinkering causes problems I can fall back to something that works.)

Quote:
Originally Posted by bioeng_mtm
..I'm soooooooo sorry for bothering..

As a person who just solved a problem you have absolutely no need to be sorry. Rejoice!

Furthermore, if I considered it a bother, I wouldn't keep coming back. Look at it another way: I'm hoping that, by spelling out (in almost painful detail) a logical sequence of tracking down bad behavior, others may also be able to make out a small light at the end of some tunnel. (That's my little fantasy, anyhow.)

Regards,

Dave
 
 

Recent GIDBlogMeeting the populace by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Binary Search Tree in C++ rpg3 C++ Forum 5 02-Apr-2006 09:53
Binary Search Tree.. triples1488 C++ Forum 7 21-Mar-2006 07:16
printing binary search tree nkhambal C Programming Language 2 26-Mar-2005 03:01
Displaying node attributes in an XML tree display njp01u MS Visual C++ / MFC Forum 2 07-Feb-2005 17:42

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:12.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.