Segmentation Fault 11 Building Binary Trees in C -


when run code length of binary tree segmentation fault: 11 error. i've tried correcting , way can run calling size function left or right nodes. when run way (which according me correct) error.

#include <stdio.h> #include <stdlib.h> #include <time.h> #include <assert.h>  struct node {     int data;     struct node* left; struct node* right; };  typedef struct node node;  node* newnode( int data ){     node* node = malloc( sizeof(node) );     assert(node);     node->data = data;     node->left = null;     node->right = null;      return node; }  node* insert( node* node, int data ) {     if( node == null){         return newnode(data);     }     else{         if( data <= node->data ){             node->left = insert(node->left, data);         }         else{             node->right = insert(node->right,data);         }     }     return node; }  node* buildonetwothree() {     node* root = newnode(2);     root->left = newnode(1);     root->right = newnode(5);     return root; }  int size( node* tree ) {     int n = 0;     if( tree == null ){         return 0;     } else {         return size(tree->left) + 1 + size(tree->right);     } }   int main(){      node* tree = null;     tree = buildonetwothree();     printf("size = %i \n", size(tree)+size(tree->right) );      return 0; } 

change

node* node = malloc( sizeof(node) );//<<-sizeof(node) : has been interpreted variable name, not type. 

to

node* node = malloc( sizeof(struct node) ); 

Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -