-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBinary_tree.java
More file actions
54 lines (42 loc) · 1.23 KB
/
Binary_tree.java
File metadata and controls
54 lines (42 loc) · 1.23 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
public class Binary_tree {
public static void main(String[] args)
{
new Binary_tree().run();
}
static class Node(){
int data;
Node left;
Node right;
public Node(int data){
this.data=data;
}
}
public void run(){
Node rootnode = new Node(25);
insert(rootnode , 2);
insert(rootnode , 89);
insert(rootnode , 6);
insert(rootnode , 24);
insert(rootnode , 9);
insert(rootnode , 78);
insert(rootnode , 1);
}
public static void insert(Node node , int data){
if(data<node.data){
if(node.left != null){
insert(node.left , data);
}else{
System.out.println(" Inserted " + value + " to left of Node " + node.value);
node.left = new node(data);
}
}
else if(data>node.data){
if(node.right!=null){
insert(node.right,data);
}else{
System.out.println(" Inserted " + value + " to right of Node " + node.value);
node.right = new node(data);
}
}
}
}