-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search_tree.java
More file actions
61 lines (55 loc) · 1.17 KB
/
binary_search_tree.java
File metadata and controls
61 lines (55 loc) · 1.17 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
import java.util.*;
class tree
{
static class Node
{
int data;
Node lchild,rchild;
Node(int data)
{
this.data = data;
lchild =rchild = null;
}
}
Node root;
tree()
{
this.root=null;
}
Node insert(Node root,int data)
{
if(root==null)
{
Node nn = new Node(data);
root = nn;
return root;
}
else if(root.data>data)
root.lchild = insert(root.lchild,data);
else if(root.data<data)
root.rchild = insert(root.rchild,data);
return root;
}
void inorderRec(Node root) {
if (root != null) {
inorderRec(root.lchild);
System.out.println(root.data);
inorderRec(root.rchild);
}
}
}
class main
{
public static void main(String args[])
{
tree t = new tree();
t.root = t.insert(t.root, 2);
t.insert(t.root, 3);
t.insert(t.root, 4);
t.insert(t.root, 15);
t.insert(t.root, 6);
t.insert(t.root, 7);
t.insert(t.root, 8);
t.inorderRec(t.root);
}
}