Balanced binary tree problems... - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# Balanced binary tree problems...

Posts: 27Member
[b][red]This message was edited by BigVent at 2005-4-12 9:53:14[/red][/b][hr]
I'm getting errors of variables not being initialized. This is happening with the void balance(leftend, rightend, tref &root). Can anyone please help me see clearer on this, or anything else you see wrong.

Bigvent

[code]
#include
#include
using namespace std;

typedef char prezname[25];
prezname prez[50];
typedef struct cell *tref;
typedef struct cell{
prezname prez;
tref left;
tref right;
}tnode;
int n;
int *leftend, *rightend;
//tref *root;

ifstream fin("prez.dat");
ofstream fout("report.dat");

//insertion function
void insert(prezname keyname, tref &root)
{

//call void lnr

}

//lnr function
void lnr(tref root)
{
if(root!=NULL)
{
lnr(root->left);
cout<<root->prez<<endl;

//at this time check & print out if there are any left or right kids

lnr(root->right);
}

//call balance function
}

//balance function
void balance(leftend, rightend, tref &root)
{
tref r=root;
int mid;
if(leftend>rightend)
root=NULL;
else
{
mid=(leftend+rightend)/2;
root=new cell;

strcpy(root->prez, a[mid]);

balance(leftend, mid-1, root->left);
balance(mid+1, rightend, root->right);
}
}

void main()
{
fin>>prez[n];

while(!fin.eof())
{
//what the hell is this for?
n++;
fin>>prez[n];
}

//call insert
insert(prezname, root);
}

//using any tree traversal, write out the contents & tree struct
[/code]

• Posts: 51Member
: [b][red]This message was edited by BigVent at 2005-4-12 9:53:14[/red][/b][hr]
: I'm getting errors of variables not being initialized. This is happening with the void balance(leftend, rightend, tref &root). Can anyone please help me see clearer on this, or anything else you see wrong.
:
: Thanks a million in advance!
:
: Bigvent
:
: [code]
: #include
: #include
: using namespace std;
:
: typedef char prezname[25];
: prezname prez[50];
: typedef struct cell *tref;
: typedef struct cell{
: prezname prez;
: tref left;
: tref right;
: }tnode;
: int n;
: int *leftend, *rightend;
: //tref *root;
:
: ifstream fin("prez.dat");
: ofstream fout("report.dat");
:
: //insertion function
: void insert(prezname keyname, tref &root)
: {
:
: //call void lnr
:
: }
:
: //lnr function
: void lnr(tref root)
: {
: if(root!=NULL)
: {
: lnr(root->left);
: cout<<root->prez<<endl;
:
: //at this time check & print out if there are any left or right kids
:
: lnr(root->right);
: }
:
: //call balance function
: }
:
:
: //balance function
: void balance(leftend, rightend, tref &root)
: {
: tref r=root;
: int mid;
: if(leftend>rightend)
: root=NULL;
: else
: {
: mid=(leftend+rightend)/2;
: root=new cell;
:
: strcpy(root->prez, a[mid]);
:
: balance(leftend, mid-1, root->left);
: balance(mid+1, rightend, root->right);
: }
: }
:
: void main()
: {
: fin>>prez[n];
:
: while(!fin.eof())
: {
: //what the hell is this for?
: n++;
: fin>>prez[n];
: }
:
: //call insert
: insert(prezname, root);
: }
:
: //using any tree traversal, write out the contents & tree struct
: [/code]
:
:
:

Looks to me that you're not copying pointers to left and right when you create a new root in that function. Very quick scan...