-
-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathBinarySearchTree:Insertion.java
More file actions
42 lines (38 loc) · 866 Bytes
/
Copy pathBinarySearchTree:Insertion.java
File metadata and controls
42 lines (38 loc) · 866 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* Node is defined as :
class Node
int data;
Node left;
Node right;
*/
static Node Insert(Node root,int value)
{
if(root==null){
Node temp=new Node();
temp.data=value;
temp.left=null;
temp.right=null;
return temp;
}
if(root.data>value){
if(root.left==null){
Node temp=new Node();
temp.data=value;
temp.left=null;
temp.right=null;
root.left=temp;
return root;
}
root.left=Insert(root.left,value);
}else{
if(root.right==null){
Node temp=new Node();
temp.data=value;
temp.left=null;
temp.right=null;
root.right=temp;
return root;
}
root.right=Insert(root.right,value);
}
return root;
}