summaryrefslogtreecommitdiff
path: root/tree.cpp
blob: a6d4451474fc86771651d0732d835bd1b90b4182 (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

#include <cstdlib>
#include <iostream>
#include <string>
#include <cassert>

using namespace std;

struct node
{
    node* left;
    node* right;
    long value;

    node(long _value) : left(NULL), right(NULL), value(_value) {}
};

void clean_node(node* nd)
{
    if (!nd)
        return;

    clean_node(nd->left);
    nd->left = NULL;
    clean_node(nd->right);
    nd->right = NULL;
    delete nd;
}

void clean_tree(node* root)
{
    clean_node(root);
}

void print_node(node* nd)
{
    if (!nd)
        return;

    print_node(nd->left);
    cout <<  nd->value << " ";
    print_node(nd->right);
}

void print_tree(node* root)
{
    cout << "tree content: ";
    print_node(root);
    cout << endl;
}

void insert_value(node* root, long value)
{
    while (true)
    {
        assert(root);
        if (root->value == value)
            // duplicates are not allowed.
            return;

        if (value < root->value)
        {
            if (!root->left)
            {
                root->left = new node(value);
                return;
            }

            root = root->left;
        }
        else
        {
            assert(value > root->value);
            if (!root->right)
            {
                root->right = new node(value);
                return;
            }

            root = root->right;
        }
    }
}

bool count_depth(const node* nd, int& depth)
{
    assert(nd);

    int d_left = 0;
    int d_right = 0;
    if (nd->left)
    {
        if (!count_depth(nd->left, d_left))
            return false;
    }

    if (nd->right)
    {
        if (!count_depth(nd->right, d_right))
            return false;
    }

    cout << "(" << nd->value << ") left:" << d_left << " right:" << d_right << endl;

    if (d_left > d_right && d_left-d_right > 1)
        return false;

    if (d_right > d_left && d_right-d_left > 1)
        return false;

    depth += 1 + (d_left > d_right ? d_left : d_right);
    return true;
}

void check_balance(const node* root)
{
    cout << "checking the balance of the tree..." << endl;
    if (!root)
        return;

    int depth = -1;
    bool res = count_depth(root, depth);
    if (res)
        cout << "tree is balanced (depth: " << depth << ")" << endl;
    else
        cout << "tree is not balanced" << endl;
}

int main()
{
    node* root = NULL;
    while (true)
    {
        cout << "> ";

        string val;
        cin >> val;

        if (val.empty())
            break;

        if (val == "q" || val == "quit")
        {
            cout << "terminating" << endl;
            break;
        }

        if (val == "c" || val == "check")
        {
            check_balance(root);
            continue;
        }

        // End position on successful conversion.
        const char* end_pos = val.c_str() + val.size();

        char* end_ptr = NULL;
        long lval = strtol(&val[0], &end_ptr, 10);
        if (end_pos != end_ptr)
        {
            // String didn't get scanned in its entirety. Must have failed.
            cerr << "invalid integer: " << val << endl;
            continue;
        }
        cout << "input number: " << lval << endl;

        if (!root)
            root = new node(lval);
        else
            insert_value(root, lval);

        print_tree(root);
    }

    clean_tree(root);

    return EXIT_SUCCESS;
}