# Thread: Where i'm wrong ? getting a segmentation fault

1. ## Where i'm wrong ? getting a segmentation fault

hi guys; I've tried to write a code whose name factors and this function takes a positive int as an argument and returns a binary tree which consists of the factors of the given int and Left child of any node in the tree should be the smallest int that is greater than 1, not equal to the node and divides the node without remainder. for example :
210
/ \
2 105
/ \
3 35
/ \
5 7
but when ı run my code ı get seg. fault and where ı am wrong ı dont know is there anyone who can help me ?
here is my code :
Code:
```struct node
{
int value;
struct node *left, *right;
int depth;
};

typedef struct node node;

node* factors(int num){
int i=2;
node * np ;
node ** bas;
bas=(node **)malloc(sizeof(node *));
np=(node *)malloc(sizeof(node));
bas=&np;
np->value=num;

while(i<=(num/2)){
if(num%i==0){
(np->left)->value=i;
(np->right)->value=num/i;
np=(np->right);
}
i+=1;
}
print_tree(np);
return np;
}```

2. 3 and 35 are under the 105 and 5 and 7 should be under the 35

3. Boy... you paint a picture of a tree with 7 nodes but you have code that only seems to ever allocate memory for a single node. You need to have an allocation (malloc) for every node in the tree. You are also accessing pointers that are uninitialized. When you allocate memory for your first node and stick the value 210 in it, the left and right pointers have random values. You then try to access those pointers as if they point to valid locations, they do not. I'm guessing your return value should be the root pointer, not np as that would be pointing to the last node of the tree and this would seem to not be of any worth.