Why is it showing "error: void value not ignored as it ought to be" in my insert function of Binary Search Tree?
I've written the following implementation of Binary Search Tree:
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
|
#include<iostream>
using namespace std;
struct Node
{
int Data;
Node* Left;
Node* Right;
};
Node* Root;
void Insert(Node* Root, int value)
{
Node* NewNode;
NewNode=new Node;
NewNode->Data = value;
NewNode->Left = NULL;
NewNode->Right= NULL;
if(Root==NULL)
{
Root=NewNode;
}
else if(value < Root->Data)
{
Root->Left = Insert(Root->Left, value);
}
else
{
Root->Right = Insert(Root->Right, value);
}
}
void Inorder(Node* Root)
{
if(Root!=NULL)
{
Inorder(Root->Left);
cout<<Root->Data<<" ";
Inorder(Root->Right);
}
}
void Preorder(Node* Root)
{
if(Root!=NULL)
{
cout<<Root->Data<<" ";
Preorder(Root->Left);
Preorder(Root->Right);
}
}
void Postorder(Node* Root)
{
if(Root!=NULL)
{
Postorder(Root->Left);
Postorder(Root->Right);
cout<<Root->Data<<" ";
}
}
int main()
{
Root=NULL;
Insert(Root,9);
Insert(Root,5);
Insert(Root,8);
Insert(Root,4);
Insert(Root,7);
Inorder(Root);
cout<<endl;
Postorder(Root);
cout<<endl;
Preorder(Root);
return 0;
}
|
But compiler is showing error: void value not ignored as it ought to be for the lines - 29 & 33. Why? How do I fix it?
The insert function doesn't return anything so it doesn't make sense trying to assign the return value.
I've changed the function. Now it doesn't show any error. But after running it doesn't show any output. @Peter87
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
|
#include<iostream>
using namespace std;
struct Node
{
int Data;
Node* Left;
Node* Right;
};
Node* Root;
//Changed function
Node* Insert(Node* Root, int value)
{
Node* NewNode;
NewNode=new Node;
NewNode->Data = value;
NewNode->Left = NULL;
NewNode->Right= NULL;
if(Root==NULL)
Root=NewNode;
else if(value < Root->Data)
Root->Left = Insert(Root->Left, value);
else
Root->Right = Insert(Root->Right, value);
return Root;
}
void Inorder(Node* Root)
{
if(Root!=NULL)
{
Inorder(Root->Left);
cout<<Root->Data<<" ";
Inorder(Root->Right);
}
}
void Preorder(Node* Root)
{
if(Root!=NULL)
{
cout<<Root->Data<<" ";
Preorder(Root->Left);
Preorder(Root->Right);
}
}
void Postorder(Node* Root)
{
if(Root!=NULL)
{
Postorder(Root->Left);
Postorder(Root->Right);
cout<<Root->Data<<" ";
}
}
int main()
{
Root=NULL;
Insert(Root,9);
Insert(Root,5);
Insert(Root,8);
Insert(Root,4);
Insert(Root,7);
Inorder(Root);
cout<<endl;
Postorder(Root);
cout<<endl;
Preorder(Root);
return 0;
}
|
Last edited on
Topic archived. No new replies allowed.