Binary_tree Error !!!

Hi,

btree.walkInorder(Disp); in main: This function returns an error


thanks in advance...

//header files
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
#ifndef  BINTREE_H_
#define  BINTREE_H_
struct Datatype{
    char name[32];
    int no;
};


struct Bnode{
	Datatype val;
	Bnode *pLeft;
	Bnode *pRight;

};

class Btree{
    size_t count;
    Bnode *proot;
public:
    Btree();
    Bnode * Insertnode(char *p, int val);
    Datatype *Findnode(int no); 
    void walkInorder(void (*proc)(Datatype *));
    void closeBinTree(Btree *hbtree);
    static void walkPostOrder(Bnode *pnode);
    void GetRandomRecord(Datatype *pVal);
    void walkInorder(Bnode *pnode, void(*proc)(Datatype *));
};

  

#endif 




// Bintree.cpp

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "BinTree.h"
#include <iostream>


Btree::Btree()
{
    
    count = 0;
    proot  = 0;

}
Bnode * Btree::Insertnode(char *p, int val)
{
    Bnode *pnewnode, *pnode, *pprewnode;
    pnode = this->proot;

    while (pnode != 0) {
        pprewnode = pnode;
        if (val > pnode->val.no)
            pnode = pnode->pRight;
        else if (val < pnode->val.no)
            pnode = pnode->pLeft;
        else
            return 0;
    }

    pnewnode = new Bnode;
    pnewnode->val.no;
    strcpy(pnewnode->val.name, p);
    pnewnode->pLeft = pnewnode->pRight  = 0;

    if (this->proot != 0){
        if (val > pprewnode->val.no)
            pprewnode->pRight = pnewnode;
        else
            pprewnode->pLeft = pnewnode;
    }
    else
        ++this->count;

    return pnewnode;
}

Datatype *Btree::Findnode(int no)
{
    Bnode *pnode;
    pnode = this->proot;

    while (pnode != 0)
        if (no == pnode->val.no)
            return &pnode->val;
    pnode = no < pnode->val.no ? pnode->pLeft : pnode->pRight;

    return 0;
}


void Btree::walkInorder(Bnode *pnode, void(*proc)(Datatype *))
{
    if (pnode->pRight != 0)
        walkInorder(pnode->pRight, proc);

    proc(&pnode->val);

    if (pnode->pLeft != 0)
        walkInorder(pnode->pLeft, proc);

}

void Btree::walkInorder(void(*proc)(Datatype *))
{
    walkInorder(this->proot, proc);

}

void Btree::walkPostOrder(Bnode *pnode)
{
    if (pnode->pRight != 0)
        walkPostOrder(pnode->pRight);
    if (pnode->pLeft != 0)
        walkPostOrder(pnode->pLeft);

    delete pnode;
        
}
void Btree::closeBinTree(Btree *hbtree)
{

    walkPostOrder(hbtree->proot);
    delete(hbtree);
}


void Btree::GetRandomRecord(Datatype *pVal)
{
	int i;

	for (i = 0; i < 31; ++i)
		pVal->name[i] = 'A' + rand() % 26;
	pVal->name[i] = '\0';

	pVal->no = rand();
}





1
2
3
4
void Disp(Datatype *pVal)
{
	cout << "(" << pVal->no <<  pVal->name <<")" <<  endl;
}




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
int main()
{

    Btree btree;


    btree.Insertnode("firi", 270);
    btree.Insertnode("Efri", 1);
    btree.Insertnode("Aysi", 54);
    btree.Insertnode("Sevi", 757);
    btree.Insertnode("Guli", 234);
    btree.Insertnode("Abri", 12);
    btree.Insertnode("Cani", 23);
    btree.Insertnode("Hari", 72);
    btree.Insertnode("mali", 110);
    btree.Insertnode("Yali", 312);
    btree.Insertnode("Gari", 3);
    btree.Insertnode("Ozi", 42);


    btree.walkInorder(Disp);
        

	return 0;
}
Last edited on
firix wrote:
btree.walkInorder(Disp); in main: This function returns an error
well, you posted enough so that you're supposed to know how to ask questions...

'walkInorder' does not return anything since void. How could this possibly return an error?

if you mean compiler error? Then try btree.walkInorder(&Disp); // note: & on line 21 in main
Error
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Bnode * Btree::Insertnode(char *p, int val)
{
    Bnode *pnewnode, *pnode, *pprewnode;
    pnode = this->proot;

    while (pnode != 0) {
         pprewnode = pnode;
        if (val > pnode->val.no)
            pnode = pnode->pRight;
        else if (val < pnode->val.no)
            pnode = pnode->pLeft;
        else
            return 0;
    }

    pnewnode = new Bnode;
    pnewnode->val.no = val;
    strcpy(pnewnode->val.name, p);
    pnewnode->pLeft = pnewnode->pRight  = pnewnode->parent = 0;

    if (this->proot != 0){
        if (val > pprewnode->val.no){
            pprewnode->pRight = pnewnode;
            pnewnode->parent = pprewnode;
        }

        else{
            pprewnode->pLeft = pnewnode;
            pnewnode->parent = pprewnode;
        }
    }
    else
        this->proot = pnewnode;  // above this line is missing
        ++this->count;

    return pnewnode;
}


void Btree::rotateRight()
{
    Bnode *pivot = proot->pLeft;
    proot->pLeft = pivot->pRight;
    pivot->pRight = proot;
}

void Btree::rotateLeft()
{

}
Topic archived. No new replies allowed.