# hey i dont know where i did wrong may be in main of balancing tree cas

Page 1 of 1

## 1 Replies - 629 Views - Last Post: 26 October 2013 - 02:35 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=332464&amp;s=25ac44991ab64ad0771e5ca37aa93912&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 elizzy

Reputation: 0
• Posts: 1
• Joined: 26-October 13

# hey i dont know where i did wrong may be in main of balancing tree cas

Posted 26 October 2013 - 12:31 AM

```#include <iostream>
#include <conio.h>
#include <cstdlib>
#include<process.h>
#include<stdio.h>
using namespace std;
struct tree
{int info;
struct tree *left, *right;
tree (int x) : info(x),left(NULL),right (NULL){}

};
class binary_tree
{public:
tree *root;
binary_tree()
{root = NULL;

}
void create_tree(tree **, int );
void pre_order(tree *);
void in_order (tree *);
void post_order(tree *);
int total_height (tree * );
int total_node (tree *);
int nonleaf_node(tree *);
int leaf_node(tree *);
void remove_tree (tree **);
tree **search(tree** ,int);
void deletenode(int);
bool isBalanced(tree *)
{
}
};
class floyd_w
{int n;
int a[10][10];
int b[10][10];
int c[10][10];
public:
void get_data();
void apsm();
void disp();

};
void floyd_w::get_data()
{int i,j ,k;
cout<<"enter the number of nodes in the graph";
cin>>n;
cout<<"enter the value in the cost matrix";
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cout<<"c["<<i<<","<<j<<"] = ";
cin>>c[i][j];
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{

a[i][j]=b[i][j];

}
}
}
void floyd_w::disp()
{cout<<" for the entered graph is :";
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cout<<a[i][j]<< "	";
}
cout<<endl;
}
}
void floyd_w::apsm()
{
int i,j,k;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{

a[i][j]=c[i][j];

}
}
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(a[i][j]<a[i][k]+a[k][j])
{
a[i][j]=a[i][j];
}
else
{
a[i][j]=a[i][k]+a[k][j];
}
}
}
}
}
void binary_tree :: create_tree(tree **root, int n)
{if (*root==NULL)
{*root = new tree;
(*root)->info=n;
(*root)->left=NULL;
(*root)->right=NULL;

}
else
{if((*root)->info >n)
create_tree(&((*root)->left),n);
else
create_tree(&((*root)->right),n);
}
}
void binary_tree ::pre_order(tree *root)
{if (root!=NULL)
{cout <<" "<<root->info;
pre_order(root->left);
pre_order(root->right);

}
}
void binary_tree :: in_order(tree *root)
{if (root!=NULL)
{in_order(root->left);
cout<<" " <<root->info;
in_order(root->right);

}
}
void binary_tree :: post_order(tree *root)
{if (root!=NULL)
{post_order(root->left);
post_order(root->right);
cout<<" " <<root->info;
}
}
int binary_tree :: total_height(tree *root)
{int left_height, right_height;
if(root==NULL)
return 0;
left_height = total_height(root->left);
right_height = total_height(root->right);
if(left_height-right_height>1||left_height-right_height<-1||right_height==-1||left_height==-1)
return -1;
else
return left_height>right_height?left_height+1:right_height+1;
}
bool binary_tree :: isBalanced(tree *root)
{if(!root)
return true;
int result = total_height(root);
if(result==-1)
return false;
else
return true;
}

int binary_tree :: total_node(tree *root)
{if (root==NULL)
return 0;
else
return(total_node(root->left)+ total_node(root->right) + 1);
}
int binary_tree :: nonleaf_node(tree *root)
{if ((root==NULL) || (root->left==NULL && root->right==NULL))
return 0;
else
return (nonleaf_node(root->left)+ nonleaf_node(root->right ) + 1);

}
int binary_tree :: leaf_node(tree *root)
{if (root==NULL)
return 0;
else
if (root->left == NULL && root->right==NULL)
return 1;
else
return (leaf_node(root->left)+leaf_node(root->right));
}
void binary_tree :: remove_tree(tree **root)
{if ((*root)!=NULL)
{remove_tree(&(*root)->left);
remove_tree(&(*root)->right);
delete (*root);
}
}
tree ** binary_tree :: search(tree **root, int n)
{
if( ((*root)->info == n) || ( (*root) == NULL) )
return root;
else
if( n < (*root)->info)
return search( &(*root)->left, n);
else
return search( &(*root)->right, n);
}
int main()
{binary_tree bt;
floyd_w fw;

int ch;
int height=0,total=0,n,no;
tree **res;
while(1)
{cout<<"BINARY TREE OPERATIONS\n";
cout<<"1.create binary tree \n";
cout<<"2.preorder traversal\n";
cout<<"3.inordertraversal\n";
cout<<"4.postorder traversal\n";
cout<<"5.depth of tree\n";
cout<<"6.total nodes\n";
cout<<"7.internal nodes\n";
cout<<"8.external nodes\n";
cout<<"9.remove tree\n";
cout<<"10.search node\n";
cout<<"FLOYDS WARSHALL ALL PAIR SHORTEST MATRIX\n\n";
cout<<"11.enter for calculating all pair shortest matrix\n";
cout<<"12. exit\n";
cin>>ch;
switch(ch)
{case 1:
cout<<"how many nodes you want to enter to create a tree";
cin>>no;
for(int i=0;i<no;i++)
{cout<<"enter the value : ";
cin>>n;
bt.create_tree(&bt.root,n);

}break;
case 2:
cout<<"preorder traversal :\n";
bt.pre_order(bt.root);
getch();
break;
case 3:
cout<<"inorder traversal:\n ";
bt.in_order(bt.root);
getch();
break;
case 4:
cout<<"post order traversal\n";
bt.post_order(bt.root);
getch();
break;
case 5:

cout<<"deapth of tree ";
bt.total_height(bt.root);
cout<<"height is"<<root;

bt.isBalanced(bt.root);
if (bt.root==true)
cout<<"tree is balanced";
else
cout<<"tree is not balanced";
getch();
break;
case 6:
total =bt.total_node(bt.root);
cout<<"\n total nodes = "<<total;
getch();
break;
case 7:
total= bt.nonleaf_node(bt.root);
cout<<"\n internal or non leaf nodes ="<<total;
getch();
break;
case 8:
total = bt.leaf_node(bt.root);
cout<<"\n external or leaf nodes ="<<total;
getch();
break;
case 9:
bt.remove_tree(&bt.root);
cout<<"\n tree is removed from memory";
getch();
break;

case 10:

cout<<"Enter number to be searched : ";
cin>>n;
res = bt.search(&bt.root,n);
if (res != NULL)
cout<<"\nSearch Element  Found";
else
cout<<"\nSearch Element was not  Found";
getch();
break;
case 11:
fw.get_data();
fw.apsm();
cout<<"all pair shortest matrix";
fw.disp();
getch();
break;
case 12:
exit(1);
}
}
}
```

This post has been edited by Salem_c: 26 October 2013 - 02:23 AM
Reason for edit:: fixed cpde tags - learn to use them yourself.

Is This A Good Question/Topic? 0

## Replies To: hey i dont know where i did wrong may be in main of balancing tree cas

### #2 Salem_c

• void main'ers are DOOMED

Reputation: 2144
• Posts: 4,208
• Joined: 30-May 10

## Re: hey i dont know where i did wrong may be in main of balancing tree cas

Posted 26 October 2013 - 02:35 AM

The first three things you need to do is learn how to indent code properly.
This serves at least two useful purposes.
1. It makes it easier for you to spot errors in program flow logic.

Code which is too badly formatted just goes unread while there are better things to do.

```#include <iostream>
//#include <conio.h>
#include <cstdlib>
//#include<process.h>
//#include<stdio.h>
using namespace std;
struct tree {
int info;
struct tree *left, *right;
tree(int x):info(x), left(NULL), right(NULL) {
}
};
class binary_tree {
public:
tree * root;
binary_tree() {
root = NULL;

} void create_tree(tree **, int);
void pre_order(tree *);
void in_order(tree *);
void post_order(tree *);
int total_height(tree *);
int total_node(tree *);
int nonleaf_node(tree *);
int leaf_node(tree *);
void remove_tree(tree **);
tree **search(tree **, int);
void deletenode(int);
bool isBalanced(tree *) {
}
};

class floyd_w {
int n;
int a[10][10];
int b[10][10];
int c[10][10];
public:
void get_data();
void apsm();
void disp();

};
void floyd_w::get_data()
{
int i, j, k;
cout << "enter the number of nodes in the graph";
cin >> n;
cout << "enter the value in the cost matrix";
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
cout << "c[" << i << "," << j << "] = ";
cin >> c[i][j];
}
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
a[i][j] = b[i][j];
}
}
}

void floyd_w::disp()
{
cout << " for the entered graph is :";
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
}

void floyd_w::apsm()
{
int i, j, k;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
a[i][j] = c[i][j];
}
}
for (k = 1; k <= n; k++) {
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
if (a[i][j] < a[i][k] + a[k][j]) {
a[i][j] = a[i][j];
} else {
a[i][j] = a[i][k] + a[k][j];
}
}
}
}
}

void binary_tree::create_tree(tree ** root, int n)
{
if (*root == NULL) {
*root = new tree;
(*root)->info = n;
(*root)->left = NULL;
(*root)->right = NULL;
} else {
if ((*root)->info > n)
create_tree(&((*root)->left), n);
else
create_tree(&((*root)->right), n);
}
}

void binary_tree::pre_order(tree * root)
{
if (root != NULL) {
cout << " " << root->info;
pre_order(root->left);
pre_order(root->right);
}
}

void binary_tree::in_order(tree * root)
{
if (root != NULL) {
in_order(root->left);
cout << " " << root->info;
in_order(root->right);
}
}

void binary_tree::post_order(tree * root)
{
if (root != NULL) {
post_order(root->left);
post_order(root->right);
cout << " " << root->info;
}
}

int binary_tree::total_height(tree * root)
{
int left_height, right_height;
if (root == NULL)
return 0;
left_height = total_height(root->left);
right_height = total_height(root->right);
if (left_height - right_height > 1 || left_height - right_height < -1 || right_height == -1
|| left_height == -1)
return -1;
else
return left_height > right_height ? left_height + 1 : right_height + 1;
}

bool binary_tree::isBalanced(tree * root)
{
if (!root)
return true;
int result = total_height(root);
if (result == -1)
return false;
else
return true;
}

int binary_tree::total_node(tree * root)
{
if (root == NULL)
return 0;
else
return (total_node(root->left) + total_node(root->right) + 1);
}

int binary_tree::nonleaf_node(tree * root)
{
if ((root == NULL) || (root->left == NULL && root->right == NULL))
return 0;
else
return (nonleaf_node(root->left) + nonleaf_node(root->right) + 1);
}

int binary_tree::leaf_node(tree * root)
{
if (root == NULL)
return 0;
else if (root->left == NULL && root->right == NULL)
return 1;
else
return (leaf_node(root->left) + leaf_node(root->right));
}

void binary_tree::remove_tree(tree ** root)
{
if ((*root) != NULL) {
remove_tree(&(*root)->left);
remove_tree(&(*root)->right);
delete(*root);
}
}

tree **binary_tree::search(tree ** root, int n)
{
if (((*root)->info == n) || ((*root) == NULL))
return root;
else if (n < (*root)->info)
return search(&(*root)->left, n);
else
return search(&(*root)->right, n);
}

int main()
{
binary_tree bt;
floyd_w fw;

int ch;
int height = 0, total = 0, n, no;
tree **res;
while (1) {
cout << "BINARY TREE OPERATIONS\n";
cout << "1.create binary tree \n";
cout << "2.preorder traversal\n";
cout << "3.inordertraversal\n";
cout << "4.postorder traversal\n";
cout << "5.depth of tree\n";
cout << "6.total nodes\n";
cout << "7.internal nodes\n";
cout << "8.external nodes\n";
cout << "9.remove tree\n";
cout << "10.search node\n";
cout << "FLOYDS WARSHALL ALL PAIR SHORTEST MATRIX\n\n";
cout << "11.enter for calculating all pair shortest matrix\n";
cout << "12. exit\n";
cin >> ch;
switch (ch) {
case 1:
cout << "how many nodes you want to enter to create a tree";
cin >> no;
for (int i = 0; i < no; i++) {
cout << "enter the value : ";
cin >> n;
bt.create_tree(&bt.root, n);

}
break;
case 2:
cout << "preorder traversal :\n";
bt.pre_order(bt.root);
getch();
break;
case 3:
cout << "inorder traversal:\n ";
bt.in_order(bt.root);
getch();
break;
case 4:
cout << "post order traversal\n";
bt.post_order(bt.root);
getch();
break;
case 5:
cout << "deapth of tree ";
bt.total_height(bt.root);
cout << "height is" << root;
bt.isBalanced(bt.root);
if (bt.root == true)
cout << "tree is balanced";
else
cout << "tree is not balanced";
getch();
break;
case 6:
total = bt.total_node(bt.root);
cout << "\n total nodes = " << total;
getch();
break;
case 7:
total = bt.nonleaf_node(bt.root);
cout << "\n internal or non leaf nodes =" << total;
getch();
break;
case 8:
total = bt.leaf_node(bt.root);
cout << "\n external or leaf nodes =" << total;
getch();
break;
case 9:
bt.remove_tree(&bt.root);
cout << "\n tree is removed from memory";
getch();
break;
case 10:
cout << "Enter number to be searched : ";
cin >> n;
res = bt.search(&bt.root, n);
if (res != NULL)
cout << "\nSearch Element  Found";
else
cout << "\nSearch Element was not  Found";
getch();
break;
case 11:
fw.get_data();
fw.apsm();
cout << "all pair shortest matrix";
fw.disp();
getch();
break;
case 12:
exit(1);
}
}
}

```

Next, don't just dump your code and say "it's got errors" or some other vague plea.
Posting exact error message(s) and asking what they mean is a far better way of going about it.
```\$ g++ -c -Wall -Wextra bar.cpp
bar.cpp: In member function ‘bool binary_tree::isBalanced(tree*)’:
bar.cpp:31:3: warning: no return statement in function returning non-void [-Wreturn-type]
bar.cpp: In member function ‘void floyd_w::get_data()’:
bar.cpp:47:13: warning: unused variable ‘k’ [-Wunused-variable]
bar.cpp: In member function ‘void binary_tree::create_tree(tree**, int)’:
bar.cpp:99:17: error: no matching function for call to ‘tree::tree()’
bar.cpp:99:17: note: candidates are:
bar.cpp:10:4: note: tree::tree(int)
bar.cpp:10:4: note:   candidate expects 1 argument, 0 provided
bar.cpp:7:8: note: tree::tree(const tree&)
bar.cpp:7:8: note:   candidate expects 1 argument, 0 provided
bar.cpp: At global scope:
bar.cpp:152:6: error: redefinition of ‘bool binary_tree::isBalanced(tree*)’
bar.cpp:30:8: error: ‘bool binary_tree::isBalanced(tree*)’ previously defined here
bar.cpp: In function ‘int main()’:
bar.cpp:247:13: error: ‘getch’ was not declared in this scope
bar.cpp:262:30: error: ‘root’ was not declared in this scope
bar.cpp:264:22: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
bar.cpp:214:7: warning: unused variable ‘height’ [-Wunused-variable]

```

The first obstacle would seem to be that your tree struct lacks a default constructor.

Three, learn to code within your comfort zone of compiler error messages. That means you should compile early and often (and test as you go). You simply can't write 300+ lines of code and then press 'compile' at the last minute as some act of repentance, hoping the compiler will show mercy on your code (it won't). Even if you get past the compiler, the run-time can still kill your program for any transgression.