-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearchTree.java
54 lines (52 loc) · 1.01 KB
/
BinarySearchTree.java
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
class Node{
int data;
Node left,right;
Node(int data){
this.data = data;
this.left = null;
this.right = null;
}
}
class BinaryTree{
Node root;
BinaryTree(Node root){
this.root = root;
}
void insert(Node root, int data){
if(data > root.data){
if(root.left == null){
Node new_node = new Node(data);
root.left =new_node;
}
else
insert(root.left, data);
}
else{
if(root.right == null){
Node new_node = new Node(data);
root.right= new_node;
}
else
insert(root.right, data);
}
return;
}
void print(Node root){
if(root == null){
return;
}
print(root.right);
System.out.println(root.data);
print(root.left);
}
public static void main(String[] args){
BinaryTree t = new BinaryTree(new Node(55));
t.insert(t.root,15);
t.insert(t.root,60);
t.insert(t.root,50);
t.insert(t.root,11);
t.insert(t.root,43);
t.insert(t.root,78);
t.print(t.root);
}
}