1struct Node
2{
3 int data;
4 Node *left, *right;
5};
6
7// Function to create a new binary tree node having a given key
8Node* newNode(int key)
9{
10 Node* node = new Node;
11 node->data = key;
12 node->left = node->right = nullptr;
13
14 return node;
15}
16
17// Function to perform inorder traversal on the tree
18void inorder(Node* root)
19{
20 if (root == nullptr) {
21 return;
22 }
23
24 inorder(root->left);
25 cout << root->data << " ";
26 inorder(root->right);
27}
28
29// Recursive function to insert a key into a BST
30Node* insert(Node* root, int key)
31{
32 // if the root is null, create a new node and return it
33 if (root == nullptr) {
34 return newNode(key);
35 }
36
37 // if the given key is less than the root node, recur for the left subtree
38 if (key < root->data) {
39 root->left = insert(root->left, key);
40 }
41 // if the given key is more than the root node, recur for the right subtree
42 else {
43 root->right = insert(root->right, key);
44 }
45
46 return root;
47}
1void BSNode::insert(std::string value) {
2
3 if (this->_data == value) {
4 _count++;
5 return;
6 }
7
8 if (this->_data > value) {
9 if (this->getLeft() == nullptr) {
10 this->_left = new BSNode(value);
11 }
12 this->getLeft()->insert(value);
13 return;
14 }
15
16 if (this->getRight() == nullptr) {
17 this->_right = new BSNode(value);
18 return;
19 }
20 this->getRight()->insert(value);
21}