Reputation: 595
I am getting compiler error that "rightTree is not declared yet" exactly on the line where i am declaring the rightTree...
Also, It says "Tree does not name a type" when i have also declared Tree above...
Same case with leftTree...
Code:
#include<bits/stdc++.h>
using namespace std;
struct Tree;
struct Node;
long int max(long int a,long int b){
if(a>=b){
return a;
}else{
return b;
}
}
struct Node{
long int value;
int left_index;
int right_index;
Node* left_child;
Node* right_child;
};
struct Tree{
Node* root;
int start;
int end;
};
Tree* createTree(Node* root,int start,int end){
Tree* T=new Tree;
T->root=root;
T->start=start;
T->end=end;
return T;
}
Node* createNode(long value,int left_index,int right_index){
Node* node=new Node;
node->value=value;
node->left_index=left_index;
node->right_index=right_index;
node->left_child=NULL;
node->right_child=NULL;
return node;
}
void printNode(Node* node){
cout<<"The value is: "<<node->value<<endl;
cout<<"Left index is: "<<node->left_index<<endl;
cout<<"Right index is: "<<node->right_index<<endl;
if(node->left_child!=NULL){
cout<<"Left child value is: "<<node->left_child->value<<endl;
}
if(node->right_child!=NULL){
cout<<"Right child value is: "<<node->right_child->value<<endl;
}
return;
}
Tree* mergeTree(Node** nodes,Tree* Tree,int start,int end){
int i;
/*for(i=0;i<=end;i++){
cout<<"--\nNode "<<i<<endl;
printNode(nodes[i]);
}*/
if(start>end){
cout<<"Found start>end..."<<endl;
Tree=nullptr;
return Tree;
}else if(start==end){
Tree->root=nodes[start];
Tree->start=start;
Tree->end=end;
return Tree;
}else if(end==start+1){
if(nodes[start]->value==max(nodes[start]->value,nodes[end]->value)){
Tree->root=nodes[start];
}else{
Tree->root=nodes[end];
}
Tree->start=start;
Tree->end=end;
return Tree;
}
//base case
int mid=(start+end)/2;
Tree* rightTree=new Tree;
rightTree=mergeTree(nodes,rightTree,start,mid);
Tree* leftTree=new Tree;
leftTree=mergeTree(nodes,leftTree,mid+1,end);
Node* root=new Node;
if(rightTree->root->value==max(rightTree->root->value,leftTree->root->value)){
root=rightTree->root;
}else{
root=leftTree->root;
}
root->left_child=leftTree->root;
root->right_child=leftTree->root;
//Node* root=new Node;
return createTree(root,start,end);
}
Tree* segmentTree_form(int a[],int n){
int i=0;
Node** nodes=(Node**)malloc(n*sizeof(Node*));
for(i=0;i<n;i++){
nodes[i]=createNode(a[i],i,i);
}
//Leaf Nodes created.
/*for(i=0;i<n;i++){
cout<<"--\nNode "<<i<<endl;
printNode(nodes[i]);
}*/
Tree* T=new Tree;
T=mergeTree(nodes,T,0,n-1);
return T;
}
int main() {
Tree* segmentTree=new Tree;
int n;
cin>>n;
int a[n],i=0;
for(i=0;i<n;i++){
cin>>a[i];
}
segmentTree=segmentTree_form(a,n);
//Node* node=createNode(-1,0,0);
//printNode(node);
return 0;
}
Compiler Message:
prog.cpp: In function 'Tree* mergeTree(Node**, Tree*, int, int)':
prog.cpp:88:11: error: 'rightTree' was not declared in this scope
Tree* rightTree=new Tree;
^
prog.cpp:88:25: error: 'Tree' does not name a type
Tree* rightTree=new Tree;
^
prog.cpp:91:11: error: 'leftTree' was not declared in this scope
Tree* leftTree=new Tree;
^
prog.cpp:91:24: error: 'Tree' does not name a type
Tree* leftTree=new Tree;
^
Thanks in advance for helping....
I have written a code for building a segmentation tree from an array in O(logn) I could not debug this error. I looked for it in the net and it said to declare Tree and Node above but this didn't work too.
Also, On writing Node* node=new Node; shows no error.
Upvotes: 2
Views: 47
Reputation: 311068
In the function declaraion the name of the second parameter hides the name of the structure Tree
.
Tree* mergeTree(Node** nodes,Tree* Tree,int start,int end){
^^^^^^^^^^
So eiher rename the parameter or use the elaborated type name for the structure Tree
as for example
struct Tree* rightTree = new struct Tree;
Take ino account that Variable Length Arrays is not a standard feature of the C++. So in general this code
int n;
cin>>n;
int a[n],i=0;
is invalid.
Also there is a memory leak in main
int main() {
Tree* segmentTree=new Tree;
// ...
segmentTree=segmentTree_form(a,n);
//..
return 0;
}
At first the a memory dynamically allocated and its address is assigned to the pointer segmentTree
and then this pointer is overwritten. So the allocated memory is not freed.
And you should use the operator new instead of malloc. So this code snippet
Tree* segmentTree_form(int a[],int n){
int i=0;
Node** nodes=(Node**)malloc(n*sizeof(Node*));
does not make sense.
Upvotes: 1
Reputation: 8501
The problem is that inside the method mergeTree
, the name Tree
refers to the second variable Tree* Tree
and not the type Tree
(The variable name hides the type name).
Change to Tree * tree
(lowercase) to fix it.
Upvotes: 0
Reputation: 1013
Tree* mergeTree(Node** nodes,Tree* Tree,int start,int end)
You have to rename second parameter. The name of parameter has hidden the name of type.
Upvotes: 1