Jumping into C++ cha 17 number 3 (binary tree and checking balance)

Hi guys, i'm still going through the binary tree chapter of my book, and in this exercise i' m supposed to create binary tree of nodes and give the user the possibility to check if the tree he's created is balanced or not. Here's my code:

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
 #include <iostream>

using namespace std;

struct node
{
   int key;
   node* right = NULL;
   node* left = NULL;
};

node* insert (node* tree, int key)
{
    if (!tree)
    {
        node* new_tree = new node;
        new_tree->key = key;
        return new_tree;
    }
    else if ( key < tree->key)
        insert (tree->left, key);
    else
        insert ( tree->right, key);
}

int Counting (node* tree, int count)
{
    if (!tree)
        return count;
    count ++;
    count = Counting (tree->right, count);
    count = Counting (tree->left, count);
    return count;
}

void Delete (node* tree)
{
    if (!tree)
        return;
    Delete (tree->left);
    Delete (tree->right);
    delete tree;
}

int main ()
{
    node* tree = NULL;
    int count_right = 0;
    int count_left = 0;
    int key = 0;
    int input;
    cout<<"1. Insert a value \n2. Check if the left side is equal to the right one \n3. Quit"<<endl;
    cin >> input;
    while (input != 3)
    {
        switch (input)
        {
        case 1:
            cout<<"Please insert the value of the node"<<endl;
            cin >> key;
            tree = insert(tree, key);
            break;
        case 2:
            count_left = (tree->left, count_left);
            count_right = (tree->right, count_right);
            cout << "Left/Right side = "<< count_left <<"/"<< count_right<< ".\n" ;
            if (count_left == count_right)
                cout<<"The tree's fully balanced. \n";
            else
                cout<<"The tree isn't fully balanced \n";
        }
        cout<<"1. Insert a value \n2. Check if the left side is equal to the right one \n3. Quit"<<endl;
        cin >> input;
    }
    Delete (tree);
}

I've created a function that counts the number of nodes. In the main function I use that function to count the nodes on each side of the tree, but no matter how many nodes I create, it keeps tellin me that the right/left ratio is 0/0.
Could any of you tell me what I m doing wrong? thx a lot
Lines 31 and 32... you're using count to accumulate a total. You need to add to this total, not overwrite it!

Instead of count = Counting (...);
try count += Counting (...);
What in the world do you think these two lines are doing?
1
2
            count_left = (tree->left, count_left);
            count_right = (tree->right, count_right);

Instead of count = Counting (...);
try count += Counting (...);

It didn t work.... there s always the same problem :/

What in the world do you think these two lines are doing?
1
2
            count_left = (tree->left, count_left);
            count_right = (tree->right, count_right);


Well... apparently something wrong xD. My intent was to use the counting function on the tree's left branch first and then on the right one. And simultaneously to store the values returned by that function in 2 different variables. That's what those 2 lines should be about ...
And where in those two lines are you calling the counting function?
-.-"… This is what happen whe you keep programming for longer then 6 hours xD
of course that was the problem. Then I ve found some other silly problems but now I m done, thx everybody for your time
Last edited on
You're welcome - glad I could help!
Topic archived. No new replies allowed.