1public static Node insert(Node root, int x){
2 if (root == null)
3 return new Node(x);
4 else if(x>root.getData())
5 root.setRightChild(insert(root.getRightChild(),x));
6 else
7 root.setLeftChild(insert(root.getLeftChild(),x));
8 return root;
9}
10
1/* This is just the seaching function you need to write the required code.
2 Thank you. */
3
4void searchNode(Node *root, int data)
5{
6 if(root == NULL)
7 {
8 cout << "Tree is empty\n";
9 return;
10 }
11
12 queue<Node*> q;
13 q.push(root);
14
15 while(!q.empty())
16 {
17 Node *temp = q.front();
18 q.pop();
19
20 if(temp->data == data)
21 {
22 cout << "Node found\n";
23 return;
24 }
25
26 if(temp->left != NULL)
27 q.push(temp->left);
28 if(temp->right != NULL)
29 q.push(temp->right);
30 }
31
32 cout << "Node not found\n";
33}
1# Python program to insert element in binary tree
2class newNode():
3
4 def __init__(self, data):
5 self.key = data
6 self.left = None
7 self.right = None
8
9""" Inorder traversal of a binary tree"""
10def inorder(temp):
11
12 if (not temp):
13 return
14
15 inorder(temp.left)
16 print(temp.key,end = " ")
17 inorder(temp.right)
18
19
20"""function to insert element in binary tree """
21def insert(temp,key):
22
23 if not temp:
24 root = newNode(key)
25 return
26 q = []
27 q.append(temp)
28
29 # Do level order traversal until we find
30 # an empty place.
31 while (len(q)):
32 temp = q[0]
33 q.pop(0)
34
35 if (not temp.left):
36 temp.left = newNode(key)
37 break
38 else:
39 q.append(temp.left)
40
41 if (not temp.right):
42 temp.right = newNode(key)
43 break
44 else:
45 q.append(temp.right)
46
47# Driver code
48if __name__ == '__main__':
49 root = newNode(10)
50 root.left = newNode(11)
51 root.left.left = newNode(7)
52 root.right = newNode(9)
53 root.right.left = newNode(15)
54 root.right.right = newNode(8)
55
56 print("Inorder traversal before insertion:", end = " ")
57 inorder(root)
58
59 key = 12
60 insert(root, key)
61
62 print()
63 print("Inorder traversal after insertion:", end = " ")
64 inorder(root)
65
66
1#include<stdlib.h>
2#include<stdio.h>
3
4struct bin_tree {
5int data;
6struct bin_tree * right, * left;
7};
8typedef struct bin_tree node;
9
10void insert(node ** tree, int val)
11{
12 node *temp = NULL;
13 if(!(*tree))
14 {
15 temp = (node *)malloc(sizeof(node));
16 temp->left = temp->right = NULL;
17 temp->data = val;
18 *tree = temp;
19 return;
20 }
21
22 if(val < (*tree)->data)
23 {
24 insert(&(*tree)->left, val);
25 }
26 else if(val > (*tree)->data)
27 {
28 insert(&(*tree)->right, val);
29 }
30
31}
32
33void print_preorder(node * tree)
34{
35 if (tree)
36 {
37 printf("%d\n",tree->data);
38 print_preorder(tree->left);
39 print_preorder(tree->right);
40 }
41
42}
43
44void print_inorder(node * tree)
45{
46 if (tree)
47 {
48 print_inorder(tree->left);
49 printf("%d\n",tree->data);
50 print_inorder(tree->right);
51 }
52}
53
54void print_postorder(node * tree)
55{
56 if (tree)
57 {
58 print_postorder(tree->left);
59 print_postorder(tree->right);
60 printf("%d\n",tree->data);
61 }
62}
63
64void deltree(node * tree)
65{
66 if (tree)
67 {
68 deltree(tree->left);
69 deltree(tree->right);
70 free(tree);
71 }
72}
73
74node* search(node ** tree, int val)
75{
76 if(!(*tree))
77 {
78 return NULL;
79 }
80
81 if(val < (*tree)->data)
82 {
83 search(&((*tree)->left), val);
84 }
85 else if(val > (*tree)->data)
86 {
87 search(&((*tree)->right), val);
88 }
89 else if(val == (*tree)->data)
90 {
91 return *tree;
92 }
93}
94
95void main()
96{
97 node *root;
98 node *tmp;
99 //int i;
100
101 root = NULL;
102 /* Inserting nodes into tree */
103 insert(&root, 9);
104 insert(&root, 4);
105 insert(&root, 15);
106 insert(&root, 6);
107 insert(&root, 12);
108 insert(&root, 17);
109 insert(&root, 2);
110
111 /* Printing nodes of tree */
112 printf("Pre Order Display\n");
113 print_preorder(root);
114
115 printf("In Order Display\n");
116 print_inorder(root);
117
118 printf("Post Order Display\n");
119 print_postorder(root);
120
121 /* Search node into tree */
122 tmp = search(&root, 4);
123 if (tmp)
124 {
125 printf("Searched node=%d\n", tmp->data);
126 }
127 else
128 {
129 printf("Data Not found in tree.\n");
130 }
131
132 /* Deleting all nodes of tree */
133 deltree(root);
134}
1public class BinarySearchTree {
2
3 public class Node {
4 //instance variable of Node class
5 public int data;
6 public Node left;
7 public Node right;
8
9 //constructor
10 public Node(int data) {
11 this.data = data;
12 this.left = null;
13 this.right = null;
14 }
15
16 }
17 // instance variable
18 public Node root;
19
20 // constructor for initialise the root to null BYDEFAULT
21 public BinarySearchTree() {
22 this.root = null;
23 }
24
25 // insert method to insert the new Date
26 public void insert(int newData) {
27 this.root = insert(root, newData);
28 }
29
30 public Node insert(Node root, int newData) {
31 // Base Case: root is null or not
32 if (root == null) {
33 // Insert the new data, if root is null.
34 root = new Node(newData);
35 // return the current root to his sub tree
36 return root;
37 }
38 // Here checking for root data is greater or equal to newData or not
39 else if (root.data >= newData) {
40 // if current root data is greater than the new data then now process the left sub-tree
41 root.left = insert(root.left, newData);
42 } else {
43 // if current root data is less than the new data then now process the right sub-tree
44 root.right = insert(root.right, newData);
45 }
46 return root;
47 }
48
49 //Traversal
50 public void preorder() {
51 preorder(root);
52 }
53
54 public void preorder(Node root) {
55 if (root == null) {
56 return;
57 }
58 System.out.print(root.data + " ");
59 preorder(root.left);
60 preorder(root.right);
61
62 }
63 public static void main(String[] args) {
64 // Creating the object of BinarySearchTree class
65 BinarySearchTree bst = new BinarySearchTree();
66 // call the method insert
67 bst.insert(2);
68 bst.insert(4);
69 bst.insert(1);
70 bst.insert(3);
71 bst.insert(5);
72 bst.preorder();
73 }
74
75}
76
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}