我收到的编译错误是"还没有声明rightTree"正好在我宣布正确的树木......
此外,它说"树没有命名类型"当我还宣布上面的树...
与leftTree相同的情况......
代码:
#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;
}
编译器消息:
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;
^
提前感谢您的帮助....
我编写了一个代码,用于在O(logn)中从数组构建分段树 我无法调试此错误。 我在网上寻找它,它说要在上面声明树和节点,但这也没有用。
另外,在编写Node * node = new Node;没有显示错误。
答案 0 :(得分:1)
Tree* mergeTree(Node** nodes,Tree* Tree,int start,int end)
您必须重命名第二个参数。参数名称隐藏了类型名称。
答案 1 :(得分:1)
在函数声明中,第二个参数的名称隐藏了结构Tree
的名称。
Tree* mergeTree(Node** nodes,Tree* Tree,int start,int end){
^^^^^^^^^^
因此,重新命名参数或使用结构Tree
的详细类型名称,例如
struct Tree* rightTree = new struct Tree;
考虑到可变长度数组不是C ++的标准功能。所以一般来说这段代码
int n;
cin>>n;
int a[n],i=0;
无效。
主
中也有内存泄漏int main() {
Tree* segmentTree=new Tree;
// ...
segmentTree=segmentTree_form(a,n);
//..
return 0;
}
首先,动态分配一个内存,并将其地址分配给指针segmentTree
,然后覆盖该指针。所以分配的内存不会被释放。
你应该使用operator new而不是malloc。所以这段代码片段
Tree* segmentTree_form(int a[],int n){
int i=0;
Node** nodes=(Node**)malloc(n*sizeof(Node*));
没有意义。
答案 2 :(得分:0)
问题是在方法mergeTree
内,名称Tree
引用第二个变量Tree* Tree
而不是类型Tree
(变量名称隐藏了类型名称)。
更改为Tree * tree
(小写)以修复它。