if (head->val == NULL) {
head->val = val;
return 0;
}
, tuto , val NULL, insert int. :
struct bstNode *new_bstNode(int val, struct bstNode *left, struct bstNode *right)
{
struct bstNode *elem = malloc(sizeof(*elem));
if (elem == NULL)
return NULL;
elem->val = val;
elem->left = left;
elem->right = right;
return elem;
}
struct bstNode *insert(struct bstNode *head, int val) {
if (head == NULL)
return new_bstNode(val, NULL, NULL);
if (val < head->val) {
if (head->left != NULL)
return insert(head->left, val);
else
return head->left = new_bstNode(val, NULL, NULL);
} else {
if (head->right != NULL)
return insert(head->right, val);
else
return head->right = new_bstNode(val, NULL, NULL);
}
}
,
int main() {
struct bstNode *bstTree = insert(bstTree, 8);
insert(bstTree, 5);
insert(bstTree, 98);
insert(bstTree, 2);
insert(bstTree, 15);
insert(bstTree, 65);
insert(bstTree, 15);
printDFS(bstTree);
}