INSERTING nodes in a bst code example
Example: bst to insert tree
struct Node
{
int data;
Node *left, *right;
};
Node* newNode(int key)
{
Node* node = new Node;
node->data = key;
node->left = node->right = nullptr;
return node;
}
void inorder(Node* root)
{
if (root == nullptr) {
return;
}
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
Node* insert(Node* root, int key)
{
if (root == nullptr) {
return newNode(key);
}
if (key < root->data) {
root->left = insert(root->left, key);
}
else {
root->right = insert(root->right, key);
}
return root;
}