Binary_tree pointer and references

hello friends,

I'm trying to write an implemenation of an unbalanced binary search tree.I have most of the basic functions working like create tree, attach new nodes, search, maxnode and minnode.I'm trying to write maxnode an minnode function that finds the largest and the smallest value.

when I change from pointer to reference the return value
Results are changing.

thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

int main()
{

btree.Insertnode("deniz", 6000);
    btree.Insertnode("deniz", 200);
    btree.Insertnode("deniz", 300);
    btree.Insertnode("deniz", 100);
    btree.Insertnode("deniz", 250);
    btree.Insertnode("deniz", 400);
    btree.Insertnode("deniz", 350);
    btree.Insertnode("deniz", 50);
    btree.Insertnode("deniz", 10);
    btree.Insertnode("deniz", 500);
    btree.Insertnode("deniz", 450);
    btree.Insertnode("deniz", 600);
    btree.Insertnode("deniz", 150);
    btree.Insertnode("deniz", 130);
    btree.Insertnode("deniz", 180);
    btree.Insertnode("deniz", 80);


    
    Bnode *result2 = btree.maxNode(btree.proot);
    Bnode *result1 = btree.minNode(btree.proot);

    cout << "max:"<< result1->val.no << endl;
    cout << "min:"<< result2->val.no << endl;   


    return 0;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Bnode *Btree::maxNode(Bnode *pnode)const
{
   Bnode *pivot = pnode;
    while (pnode != 0) {
        pnode = pnode->pRight;
         if (pnode != 0 &&pnode->val.no > pivot->val.no)
            pivot = pnode;
   
    }
    return pivot;

}

Bnode *Btree::minNode(Bnode *pnode)const
{
    
    
    Bnode *pivot = pnode;
    while (pnode != 0) {
        pnode = pnode->pLeft;
        if (pnode != 0 && pnode->val.no < pivot->val.no)
            pivot = pnode;
   
    }
    return pivot;


}




max:10
min:6000
Press any key to continue . . .



but I change the functions as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int &Btree::maxNode(Bnode *pnode)const
{
  int &val  pnode->val.no;
    while (pnode != 0) {
        pnode = pnode->pRight;
         if (pnode != 0 &&pnode->val.no > pnode->val.no)
             val = pnode->val.no;
   
    }
    return val;

}

int &Btree::minNode(Bnode *pnode)const
{

    int &val = pnode->val.no;
    while (pnode != 0) {
        pnode = pnode->pLeft;
        if (pnode != 0 && pnode->val.no < pnode->val.no)
            val = pnode->val.no;
   
    }
    return val;


}





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

int main()
{
    Btree btree;

       
    btree.walkInorder(Disp);



    cout << "****************************************************" << endl;
    


   
    cout << "max:"<<btree.maxNode(btree.proot) << endl;
    cout << "min:" << btree.minNode(btree.proot) << endl;


   return 0;
}



max and min are changing as follows.


max:10
min:10
Press any key to continue . . .




Where am I doing wrong
Last edited on
1
2
3
4
5
6
7
8
9
10
11
Bnode *Btree::maxNode(Bnode *pnode)const
{
   Bnode *pivot = pnode;
    while (pnode != 0) {
        pivot = pnode
        pnode = pnode->pRight;
// This is BST, that conditions are superfluous 
//      if (pnode != 0 &&pnode->val.no > pivot->val.no)
//          pivot = pnode;
    }
    return pivot;

1
2
3
4
5
6
7
8
9
10
11
//int &Btree::minNode(Bnode *pnode)const //you shouldn't return a reference (it could break your structure
int Btree::minNode(Bnode *pnode)const
{
    int &val = pnode->val.no; //this make an alias 
    while (pnode != 0) {
        pnode = pnode->pLeft;
        if (pnode != 0 && pnode->val.no < pnode->val.no)
            val = pnode->val.no; //you are overwriting the content of var (you can't modify what the alias refer)
    }
    return val;
}


Topic archived. No new replies allowed.