You are viewing a single comment's thread. Return to all comments →
typedef struct Node { int data; struct Node *left; struct Node *right; } Node;
Node *createNode(int data) { Node *newNode = (Node *)malloc(sizeof(Node)); newNode->data = data; newNode->left = NULL; newNode->right = NULL; return newNode; }
Node *insert(Node *root, int data) { if (root == NULL) { return createNode(data); }
if (data < root->data) { root->left = insert(root->left, data); } else if (data > root->data) { root->right = insert(root->right, data); } return root;
}
void printInorder(Node *root) { if (root != NULL) { printInorder(root->left); printf("%d ", root->data); printInorder(root->right); } }
Seems like cookies are disabled on this browser, please enable them to open this website
Binary Search Tree : Insertion
You are viewing a single comment's thread. Return to all comments →
typedef struct Node { int data; struct Node *left; struct Node *right; } Node;
Node *createNode(int data) { Node *newNode = (Node *)malloc(sizeof(Node)); newNode->data = data; newNode->left = NULL; newNode->right = NULL; return newNode; }
Node *insert(Node *root, int data) { if (root == NULL) { return createNode(data); }
}
void printInorder(Node *root) { if (root != NULL) { printInorder(root->left); printf("%d ", root->data); printInorder(root->right); } }