-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert.java
More file actions
60 lines (56 loc) · 1.74 KB
/
insert.java
File metadata and controls
60 lines (56 loc) · 1.74 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#########################insert.java#####################################################
/*Insert class with functions for inserting both integer and string data
in the binary search tree
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Aviral
*/
public class insert {
//Insert in BST for integer values
public static void insert(node node,int value) {
if (value < node.data) {
if (node.left != null) {
insert(node.left, value);
}
else
{
System.out.println(" Inserted " + value + " to left of node " + node.data);
node.left = new node(value);
}
} else if (value > node.data) {
if (node.right != null) {
insert(node.right, value);
}
else {
System.out.println(" Inserted " + value + " to right of node " + node.data);
node.right = new node(value);
}
}
}
//Insert in BST for String values
public static void insert1(NodeStr node,String value) {
if (value.compareTo(node.data)<0) {
if (node.left != null) {
insert1(node.left, value);
}
else
{
System.out.println(" Inserted " + value + " to left of node " + node.data);
node.left = new NodeStr(value);
}
} else if (value.compareTo(node.data)>0) {
if (node.right != null) {
insert1(node.right, value);
}
else {
System.out.println(" Inserted " + value + " to right of node " + node.data);
node.right = new NodeStr(value);
}
}
}
}