couldnt connect 2 algorithms...

hi,i jot 2 ideas about constructing atree (inorder) with out using recursion
now this is the first:
1
2
3
4
5
6
7
8
9
10
void insert<Type>(Type info)
{
TreeNode<Type>* newNode;
newNode=new TreeNode<Type>;
newNode->data=info;
newNode->rightLink=NULL;
newNode->leftLink=NULL;
count++;

insert<Type>(newNode);


now this function is public wich will reseve a parameter from the user..
i thought that i should creat the node and then i can gust link it to the
tree...!!thats way i call the predefined insert wich takes as parameter
a pointer to the node..and i put it in the private of my class...

this is the code..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void insert<Type>(TreeNode<Type>*& current)
{
   if (root==NULL)
     root=current;
 TreeNode<Type>* Nroot=root;
if (count%2==0)//contain the number of nodes
{
Nroot->leftLink=current;
Nroot=Nroot->rightLink;
}
else

Nroot->left=current;

}


its not realy perfect put this what i came up so that i can blance my tree
and i want to combine the 2 algorithms but i didnt go so far..
please may any one hlep me..thanks indeed
Last edited on
The first function isn't doing anything that TreeNode constructor should be doing. That is to say, TreeNode constructor should take data as the parameter, and then assign the data value, and set the links to NULL, and in this case, increment a class static counter.

The second function uses the count in a bizarre way. If you want to check if the nodes point to another node, you should compare their values with NULL, not against the TreeNode count.
i did the constructer as u said,so i have missed smth very important ...
1
2
3
4
5
6
7
8
void insert(TreeNode<Type>*& current)
{
TreeNode<Type> *Nroot=root;
    if (Nroot->rightLink!=NULL)
    Nroot->leftLink=curent;
 else
   Nroot->leftLink=current;
}

im very sorry i was confused i want to connect the last code with the above
the above i did to calculate the number of nodes in the tree and decide which
should subtree we should start with...in other words i didnt figure out how
can i balnce the tree while i have an input...
if you may help me.
Topic archived. No new replies allowed.