Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can anyone give me some suggestions 1

Status
Not open for further replies.

Sedai

Programmer
Mar 4, 2001
158
US
Can anyone give me some suggestions on how to design an iterative algorithm that gives me the ints from 1 to n in preorder.
I need to insert 1 - n ints in a Binary search tree, keeping it balanced. I have a working recursive algorithm, but it turns out it is not practical to use it in my program.

After sloving the problem recursively (actually quite simple), I find it very hard to think iteratively. I need just a hint really, I'd like to do as much of it no my own as possible. Any hints would be greatly appreciated!

Thank you.

Avendeval


 
Do you need something like this: (?)

class GiveInt()
{
private:
m_nCurrentInt;
public:
m_nMaxValue;
int GiveNextInt()
};
int GiveInt::GiveNextInt()
{
if(++m_nCurrentInt>m_nMaxValue)
{
m_nCurrentInt=0;
return 0;
}
else
return ++m_nCurrentInt;
}

call it like:
GiveInt gi;
gi.m_nMaxValue=2000;
....
gi.GiveNextInt();

HTH, s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
im sorry i think thats not what i need.
in short i need an alternative way (iterative solution) to do the same thing the following recursive function does:
Code:
void a(int r, int s){
	if(r==s)
		return;
	int x = (r+s)/2;
	cout << x;
	a(r,x);
	a(x+1,s);
}
thanks :) Avendeval


 
Here is the code.

Class GiveInt
{
private:
int *values;
int index;
void generate(int low, int high)
{
if(low>high)
return;
int x = (low+high+1)/2;
values[index++] = x;
generate(low,x-1);
generate(x+1,high);
}
public:
GiveInt()
{
values = NULL;
index = 0;
}
~GiveInt()
{
if(values != NULL)
{
delete values;
values = NULL;
}
}
void SetMaxValue(int m_Max);
{
if(values != NULL)
{
delete values;
values = NULL;
}
values = new int[m_Max];
index = 0;
generateInts(1, m_Max);
index = 0;
}
int GetInt()
{
if (values == NULL)
return -1;
return values[index++];
}
}

I have changed the code for generating the interger. The reason i have already ansered in ur pervious posted thread (Simple Recursion)

Hope this helps

raochetan
 
nice...
thank you for your time and effort! Avendeval


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top