Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
给出SIGV错误的程序 我写过C++程序,它给了我_C++_Arrays_Binary Search Tree - Fatal编程技术网

给出SIGV错误的程序 我写过C++程序,它给了我

给出SIGV错误的程序 我写过C++程序,它给了我,c++,arrays,binary-search-tree,C++,Arrays,Binary Search Tree,非常大输入的SIGV错误 基本上,下面的程序输出的数字小于数组中的当前数字。我使用了BST和数组来实现 #include <iostream> using namespace std; struct bst { long long int data; struct bst *right; struct bst *left; }; struct bst *head=NULL; //function to increment void increment(s

非常大输入的SIGV错误

基本上,下面的程序输出的数字小于数组中的当前数字。我使用了BST和数组来实现

#include <iostream>
using namespace std;

struct bst {
    long long int data;
    struct bst *right;
    struct bst *left;
};

struct bst *head=NULL;
//function to increment 
void increment(struct bst *node,long long int w[]) {
    if(node==NULL)
    return;
    w[node->data] += 1;
    increment(node->left,w);
    increment(node->right,w);
}

void insert(long long int data,long long int w[]){
    struct bst *node;
    struct bst *current;
    current=head;
    if(head==NULL) {
        node = new bst;
        node->data = data;
        node->right = NULL;
        node->left=NULL;
        head=node;
        }else {
        while(1) {
            if(data > current->data){
                if(current->right!=NULL)
                current=current->right;
                else {
                    node = new bst;
                    node->data = data;
                    node->right = NULL;
                    node->left=NULL;
                    current->right=node;
                    break;
                }
            }
            else{
                if(current->left!=NULL){
                    //if data is less than current number than incremet 1 in right sub tree of current node 
                    //and current node
                    w[current->data] +=1;
                    increment(current->right,w);
                    current=current->left;

                }
                else {
                    node = new bst;
                    node->data = data;
                    node->right = NULL;
                    node->left=NULL;
                    current->left=node;
                    //if data is less than current number than incremet 1 in right sub tree of current node 
                    //and current node
                    w[current->data] +=1;
                    increment(current->right,w);
                    break;
                }
            }

        }
    }
}

void deletenode(struct bst *node) {
    if(node == NULL)
    return;
    deletenode(node->left);
    deletenode(node->right);
    delete(node);
}


int main() {
    int t;
    cin>>t;
    while(t--) {
        long long int n;long long int counter=0;
        cin>>n;
        long long int a[n];
        //array to keep count of numbers which are less than index of w[i]
        long long int w[1000000]={0};
        for(long long int i=0;i<n;i++) {
            cin>>a[i];
            insert(a[i],w);
        }
        for(long long int i=0;i<n;i++) {
            cout<<w[a[i]]<<" ";
        }
        cout<<"n";
        //deleting BST
        deletenode(head);
        head=NULL;
    }
    return 0;
}
#包括
使用名称空间std;
结构bst{
长整型数据;
结构bst*右;
结构bst*左;
};
struct bst*head=NULL;
//递增函数
无效增量(结构bst*节点,长整型w[]){
if(node==NULL)
回来
w[节点->数据]+=1;
增量(节点->左侧,w);
增量(节点->右侧,w);
}
无效插入(长整型数据,长整型w[]){
结构bst*节点;
结构bst*电流;
电流=水头;
if(head==NULL){
节点=新的bst;
节点->数据=数据;
节点->右=空;
节点->左=空;
头部=节点;
}否则{
而(1){
如果(数据>当前->数据){
如果(当前->右侧!=NULL)
当前=当前->右侧;
否则{
节点=新的bst;
节点->数据=数据;
节点->右=空;
节点->左=空;
当前->右=节点;
打破
}
}
否则{
如果(当前->左!=空){
//如果数据小于当前节点的右子树中的当前数目,则为增量1
//和当前节点
w[当前->数据]+=1;
增量(当前->右侧,w);
当前=当前->左侧;
}
否则{
节点=新的bst;
节点->数据=数据;
节点->右=空;
节点->左=空;
当前->左=节点;
//如果数据小于当前节点的右子树中的当前数目,则为增量1
//和当前节点
w[当前->数据]+=1;
增量(当前->右侧,w);
打破
}
}
}
}
}
void deletenode(结构bst*节点){
if(node==NULL)
回来
删除节点(节点->左侧);
删除节点(节点->右侧);
删除(节点);
}
int main(){
int t;
cin>>t;
而(t--){
长整型n;长整型计数器=0;
cin>>n;
长整型a[n];
//数组以保持小于w[i]索引的数字计数
长整型w[1000000]={0};
对于(长整型i=0;i>a[i];
插入(a[i],w);
}

对于
main
中的(long long int i=0;i),这可能会导致问题-

long long int w[1000000]={0};
您应该在堆上分配内存。您可以这样做-

long long int *w=new long long int[1000000]

在调试器中运行,捕获错误,告诉我们崩溃发生在代码中的什么地方(以及所有相关变量的值)。谢谢@ameyCu,我认为问题在于我过度使用了堆栈。@SandepKumarsingh是的,它可能导致了堆栈溢出:-)