Ошибка сегментации в Mac, но не в других онлайн-компиляторах - PullRequest
0 голосов
/ 12 февраля 2019

Этот код работает на ideone и других компиляторах, но выдает ошибку сегментации в моем mac или некоторые ненужные значения.Пожалуйста помоги.Это стандартная процедура обхода заказа, и она должна просто печатать от 1 до 7.

https://ideone.com/l5tkks

#include <bits/stdc++.h>
using namespace std;

struct node{
    int data;
    struct node* left;
    struct node* right;
};

typedef struct node* Node;

Node insert(Node root, int num){
    if(root==NULL){
        Node newNode=(Node)malloc(sizeof(Node));
        newNode->data=num;
        newNode->left=NULL;
        newNode->right=NULL;
        return newNode;
    }

    if(root->data>num)
        root->left=insert(root->left,num);
    else
        root->right=insert(root->right,num);
    return root;
}

void printinorder(Node root){
    if(root==NULL)
        return;

    printinorder(root->left);
    cout<<root->data<<endl;
    printinorder(root->right);
}

int main(){
    Node tree=NULL;
    tree=insert(tree,1);
    tree=insert(tree,2);
    tree=insert(tree,3);
    tree=insert(tree,4);
    tree=insert(tree,5);
    tree=insert(tree,6);
    tree=insert(tree,7);
    printinorder(tree);
}

1 Ответ

0 голосов
/ 12 февраля 2019
Node newNode = (Node) malloc(sizeof(Node));

должно быть

Node newNode = (Node) malloc(sizeof(node));
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...