C/C++ >> My print function is locking this program up?
Posted by Drew37 on 18:53:00 10-11-2001
I am trying to get this print function to work. I think I need something else in my insert function for the print function to work. How would the code go to return a pointer to a node from my insert function?

#include
#include
#include

#include"BSTTreeAndyJackson.cpp"



using namespace std;

int main()
{
btree bt;
int num=0;
char chr;
int count=0;
node leafnode;
node *leaf;

// ifstream fin;
// ofstream fout;


fin.open("input.txt");
fout.open("output1.txt");

fin-->-->num;
fin-->-->chr;

while(!fin.eof())

{
count++;
bt.insert(num);
fout -->num;
fin-->-->chr;

}



return 0;
}

#include"BSTTreeAndyJackson.h"
#include
//#include

//ifstream fin("input.txt");
// ofstream fout("output1.txt");
ifstream fin;
ofstream fout;

//Constructor
///////////////////////////////////////////////////////////////////////
btree::btree()
{
root=NULL;
}


//Insert Tree
//////////////////////////////////////////////////////////////////////
void btree::insert(int key, node *leaf)
{
if(keykey_value)
{
if(leaf--->left!=NULL)
insert(key, leaf--->left);
else
{
leaf--->left=new node;
leaf--->left--->key_value=key;
leaf--->left--->left=NULL; //Sets the left child of the child node to null
leaf--->left--->right=NULL; //Sets the right child of the child node to null
}
}
else if(key-->=leaf--->key_value)
{
if(leaf--->right!=NULL)
insert(key, leaf--->right);
else
{
leaf--->right=new node;
leaf--->right--->key_value=key;
leaf--->right--->left=NULL; //Sets the left child of the child node to null
leaf--->right--->right=NULL; //Sets the right child of the child node to null
}
}
}




///////////////////////////////////////////////////////////////////////

void btree::insert(int key)
{
if(root!=NULL)
insert(key, root);
else
{
root=new node;
root--->key_value=key;
root--->left=NULL;
root--->right=NULL;
}
}



//////////////////////////////////////////////////////////////////////

void btree::InOrderTranverse(node *leaf)
{

if(leaf--->left != NULL)
InOrderTranverse(leaf--->left);

//foutkey_valueright !=NULL)
InOrderTranverse(leaf--->right);
}
//////////////////////////////////////////////////////////////////////
void btree::InOrderTranverse()
{
if(root!=NULL)
InOrderTranverse(root);
else

return;
}
//////////////////////////////////////////////////////////////////////
void btree::preOrderTranverse(node *leaf)
{
if(leaf != NULL){

//foutkey_value;
preOrderTranverse(leaf--->left);
preOrderTranverse(leaf--->right);
}
}

///////////////////////////////////////////////////////////////////////
void btree::preOrderTranverse()
{
if(root!=NULL)
preOrderTranverse(root);
else

return;
}

//////////////////////////////////////////////////////////////////////
int btree::Print(ofstream& fout,int num) const
{
if(leaf == NULL)
{
fout key_value;
return true;
}
//node *leaf;
//foutkey_value;
//}

// while(root != NULL)
// {
// foutkey_value;
// }

#include

struct node;

typedef node *Nodeptr;

struct node
{
int key_value;
node *left;
node *right;

};


class btree
{
public:
btree();

void insert(int key);
void InOrderTranverse();
void preOrderTranverse();

int Print(ofstream& fout, int num) const;

private:
void insert(int key, node *leaf);
void InOrderTranverse(node *leaf);
void preOrderTranverse(node *leaf);
//Nodeptr *root;
node *root;
node *leaf;
};




Posted by justin on 03:44:00 02-19-2002
return *this;
and as far as locking up, the insert function tests is next is true then, call insert (recursive), maybe you have a broken null terminator somewhere, if it accidentally gets overwritten, then runaway is common...
Posted by fsvara on 14:35:00 02-19-2002
well, for that kind of prob, use some nice graphical debugger that shows you in a simple way what's going on
a nice gdb frontend is ddd. (data display debugger)... Beware tho, you get addicted really fast