-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBSTree.java
100 lines (93 loc) · 1.91 KB
/
BSTree.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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
public class BSTree
{
static Node root;
static class Node
{
int data;
Node left;
Node right;
Node(int d)
{
data = d;
left = null;
right = null;
}
}
public static void insert(Node temp,int d)
{
Node nn = new Node(d);
if(temp == null)
{
root = nn;
}
else if(d < temp.data)
{
if(temp.left == null)
{
temp.left = nn;
}
else
{
temp = temp.left;
insert(temp,d);
}
}
else
{
if(temp.right == null)
{
temp.right = nn;
}
else
{
temp = temp.right;
insert(temp,d);
}
}
}
public static void printInord(Node temp)
{
if(temp == null)
return;
printInord(temp.left);
System.out.print(temp.data+" ");
printInord(temp.right);
}
public static void Search(Node temp,int d)
{
if(temp==null)
{
System.out.println("\nNot Found");
return;
}
if(temp.data == d)
{
System.out.println("Found");
return;
}
else if(d < temp.data)
{
Search(temp.left,d);
}
else
{
Search(temp.right,d);
}
}
public static void main(String[] pavan)
{
insert(root,2);
insert(root,12);
insert(root,5);
insert(root,9);
insert(root,8);
insert(root,1);
insert(root,11);
insert(root,13);
insert(root,10);
System.out.println("Inorder");
printInord(root);
Search(root,50);
Search(root,5);
}
}