-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarysearchTree.java
More file actions
49 lines (48 loc) · 1.25 KB
/
BinarysearchTree.java
File metadata and controls
49 lines (48 loc) · 1.25 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
package Daily_Problem;
import java.util.*;
public class BinarysearchTree {
static class Node{
int data;
Node left;
Node right;
public Node(int n)
{
this.data=n;
this.left=null;
this.right=null;
}
}
public static Node BST(int arr[],int s,int e)
{
if (s > e) {
return null;
}
int mid = (s + e) / 2;
Node node = new Node(arr[mid]);
node.left = BST(arr, s, mid - 1);
node.right = BST(arr, mid + 1, e);
return node;
}
public static void display(Node node, int level) {
if (node == null) {
return;
}
display(node.right, level + 1);
if(level!=0){
for (int i = 0; i < level-1; i++) {
System.out.print("|\t\t");
}System.out.println("|------>"+node.data);
}
else{
System.out.println(node.data);
}
display(node.left, level + 1);
}
public static void main(String[] args) {
int arr[]={10,20,30,40,50,60,70};
int start=0;
int end=arr.length-1;
Node nn= BST(arr,start,end);
display(nn, 0);
}
}