finding the height of a Binary search tree

my code was working fine until i tried to implement the function to find the height of the tree. any idea where i messed up or a better way of doing this? i need to find the height of both the left and right side so that i can check to see if its balanced or not.
thanks!
-Sporx

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
#include <iostream>
#include <cstdlib>
using namespace std;
class BinarySearchTree
{
    //private:
    public:
     struct tree_node
        {
           tree_node* left;
           tree_node* right;
           int data;
        };
        tree_node* root;

        BinarySearchTree ()
        {
           root = NULL;
        }
        bool isEmpty() const { return root==NULL; }
        void print_inorder();
        void inorder(tree_node*);
        void insert(int);
int height (tree_node*);

};



// Smaller elements go left
// larger elements go right
void BinarySearchTree::insert(int d)
{
    tree_node* t = new tree_node;
    tree_node* parent;
    t->data = d;
    t->left = NULL;
    t->right = NULL;
    parent = NULL;
  // is this a new tree?
  if(isEmpty()) root = t;
  else
  {
    //Note: ALL insertions are as leaf nodes
    tree_node* curr;
    curr = root;
    // Find the Node's parent
    while(curr)
    {
        parent = curr;
        if(t->data > curr->data) curr = curr->right;
        else curr = curr->left;
    }

    if(t->data < parent->data)
       parent->left = t;
    else
       parent->right = t;
  }
}


void BinarySearchTree::print_inorder()
{
  inorder(root);
}

void BinarySearchTree::inorder(tree_node* p)
{
    if(p != NULL)
    {
        if(p->left) inorder(p->left);
        cout<<" "<<p->data<<" ";
        if(p->right) inorder(p->right);
    }
    else return;
}








int BinarySearchTree::height(tree_node* count){
	int l = 0;
	int r = 0;
cout<<"test";
	if(cout == NULL)
	{
		return 0;
	}
	{
		l = height(count->left);
		r = height(count->right);
		if( l > r || l == r)
		{
			return (l + 1);
		}
		else
		{
			return (r + 1);
		}
	}
cout<<"left is "<<l<<endl<<"right is "<<r<<endl;
}











int main()
{
    BinarySearchTree b;
    int ch,tmp,tmp1;
  int counter=1;
    while(counter==1)
    {
       cout<<endl<<endl;
       cout<<" Binary Search Tree Operations "<<endl;
       cout<<" ----------------------------- "<<endl;
       cout<<" 1. Insertion/Creation "<<endl;
       cout<<" 2. In-Order Traversal "<<endl;
       cout<<" 3. Exit "<<endl;
       cout<<" Enter your choice : ";
       cin>>ch;
       switch(ch)
        {
           case 1 : cout<<" Enter Number to be inserted : ";
                    cin>>tmp;
                    b.insert(tmp);
                    break;
           case 2 : cout<<endl;
                    cout<<" In-Order Traversal "<<endl;
                    cout<<" -------------------"<<endl;
                    b.height();
                    b.print_inorder();
                    break;
           case 3 :
           system("pause");
                    counter=0;

                    break;
       }
    }




}

1
2
3
4
	if(cout == NULL)
	{
		return 0;
	}


should be

1
2
3
4
	if(count == NULL)
	{
		return 0;
	}



because cout will never be NULL.


Maikel
thanks for the heads up on the typo!=] i shoulda caught that.. however theres still a problem somewhere, im getting errors on lines 143 stating :
"no matching function call for binarysearchtree::height()",
and line 86, stating
"candidates are: int BinarySearchtree::height(BinarySearchtree::tree_Node*)"

and I dont know what either mean or how to fix them=/ any advice?
 
b.height(&b);


alternativly do an overload by:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class BinarySearchTree
{
    //private:
    public:
     struct tree_node
        {
           tree_node* left;
           tree_node* right;
           int data;
        };
        tree_node* root;

        BinarySearchTree ()
        {
           root = NULL;
        }
        bool isEmpty() const { return root==NULL; }
        void print_inorder();
        void inorder(tree_node*);
        void insert(int);
        int height (tree_node*);
        int height () { return height(this); } // OVERLOAD
};


Maikel
Topic archived. No new replies allowed.