Reputation: 1
I want to create tree in C++. I could compile the code without errors or warnings, but i don't get an output.
I think error is in inorder fn, but don't know how to remove this.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct tree
{
int data;
struct tree * left;
struct tree * right;
};
typedef struct tree * TREE;
TREE maketree(int x)
{
TREE tree = (TREE)malloc(sizeof(tree));
if(tree == NULL)
{
return NULL;
}
tree->left = tree->right = NULL;
tree->data = x;
return tree;
}
void setleft(TREE tree,int x)
{
if(tree == NULL || tree->left != NULL)
{
cout<<"\t Error !! Inserting New Node At Left Side !\n";
}
else
{
tree->left = maketree(x);
}
}
void setright(TREE tree,int x)
{
if(tree == NULL || tree->right != NULL)
{
cout<<"\t Error !! Inserting New Node At Right Side !\n";
}
else
{
tree->right = maketree(x);
}
}
void inorder(TREE root)
{
if(root != NULL)
{
TREE left=root->left;
TREE right=root->right;
inorder(left);
cout<<root->data;
inorder(right);
}
}
void main()
{
clrscr();
TREE root = NULL,child,parent;
int i,j = 1;
cout<<"Root Of Binary Search Tree :- ";
cin>>i;
root = maketree(i);
cout<<"\n\n";
while(i)
{
cout<<j<<" Node Value:- ";
cin>>i;
if(i < 0)
{
break;
}
parent = child = root;
while((i != parent->data) && (child != NULL))
{
parent = child;
if(i < parent->data)
{
child = parent->left;
}
else
{
child = parent->right;
}
}
if(i == parent->data)
{
cout<<"\t Value "<<i<<" Already Present In BST !!\n";
}
else if(i < parent->data)
{
setleft(parent,i);
}
else
{
setright(parent,i);
}
j++;
}
inorder(root);
getch();
}
Upvotes: 0
Views: 2847
Reputation: 31435
If you want to write in C++ then write in C++. Use constructors and destructors and class methods. Possibly make your data members private. And use new
rather than malloc
and your destructors will probably want to delete (the child nodes of the tree).
What you have written is in C other than you've incorporated the worst feature of C++, iostream, and that in its old deprecated non-standard version.
This looks like some school exercise.
I also can't see anywhere where you free
the data you have allocated with malloc
.
Your sorting logic should be in a tree-based function, not main.
Your "error" may be the lack of whitespace in the output, but I don't know.
It's legal but not good practice to use tree
as both a data-type (it's a struct, which in C++ does not need qualifying with struct) and a variable (for which it is often being used).
Ok, now a bit of code, mostly based on yours.
class tree
{
tree * left;
tree * right;
int value;
public:
explicit tree( int v );
~tree();
bool insert( int v );
void print( std::ostream& ) const;
private:
tree( const tree& );
tree& operator=( const tree& );
};
tree::tree( int v ) :
left( NULL ),
right( NULL ),
value( v )
{
}
tree::~tree()
{
delete right;
delete left;
}
bool tree::insert( int v )
{
// inserts v in the correct place in the tree, returns true
// if it inserted or false if it already exists
// I want you to fill in the detail for this function
}
void tree::print( std::ostream& os ) const
{
// prints the tree
if( left )
{
left->print( os );
}
os << value << '\n';
if( right )
{
right->print( os );
}
}
There, I have left one function for you to implement. You do not need to implement the private copy constructor or assignment operator.
Also implement main()
. Note that there is no need to implement the tree in main on the heap (with new). Implement it on the stack.
main()
will read in the numbers, insert them in to the tree calling its insert()
method and then at the end print the tree, passing std::cout
as a parameter.
You need to #include <iostream>
(not iostream.h) and it will work.
Upvotes: 3