-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathBST.java
More file actions
82 lines (64 loc) · 1.44 KB
/
Copy pathBST.java
File metadata and controls
82 lines (64 loc) · 1.44 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* Created with IntelliJ IDEA.
* User: naveen
* Date: 7/12/14
* Time: 7:54 PM
* To change this template use File | Settings | File Templates.
*/
public class BST {
Node root;
public static class Node{
Node left;
Node right;
int data;
public Node(int value){
left = null;
right = null;
data = value;
}
}
public BST(){
root = null;
}
public void insert(int data)
{
root = insertP(root,data);
}
private Node insertP(Node root,int data){
if(root == null){
root = new Node(data);
return root;
}else{
if(data > root.data){
root.right = insertP(root.right,data);
}else{
root.left = insertP(root.left,data);
}
}
return root;
}
public void inOrder(){
inOrderP(root);
}
private void inOrderP(Node root){
if(root == null)
{
return;
}else{
inOrderP(root.left);
System.out.println(root.data+" ");
inOrderP(root.right);
}
}
public static void main(String[] args){
BST bst = new BST();
bst.insert(40);
bst.insert(30);
bst.insert(50);
bst.insert(25);
bst.insert(35);
bst.insert(55);
bst.insert(60);
bst.inOrder();
}
}