Skip to content

Commit 462e479

Browse files
Add files via upload
1 parent d2a0ebe commit 462e479

File tree

1 file changed

+29
-35
lines changed

1 file changed

+29
-35
lines changed

Trees/BinarySearchTree/BST.java

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,63 @@
11
package com.company;
22

33
class BST {
4-
class Node{
4+
class Node {
55
int data;
66
Node left;
77
Node right;
8-
Node(){
8+
9+
Node () {
910
}
10-
Node(int data){
11-
this.data=data;
12-
this.left=this.right=null;
11+
12+
Node (int data) {
13+
this.data = data;
14+
this.left = this.right = null;
1315
}
1416
}
1517

1618
Node root;
1719

18-
void insert(int data){
19-
root=insertRec(root,data);
20+
void insert (int data) {
21+
root = insertRec(root, data);
2022
}
21-
private Node insertRec(Node root,int data){
22-
if(root==null){
23-
root=new Node(data);
23+
24+
private Node insertRec (Node root, int data) {
25+
if (root == null) {
26+
root = new Node(data);
2427
return root;
25-
}else{
26-
if(data<root.data){
27-
root.left=insertRec(root.left,data);
28-
}else if(data>root.data){
29-
root.right=insertRec(root.right,data);
28+
} else {
29+
if (data < root.data) {
30+
root.left = insertRec(root.left, data);
31+
} else if (data > root.data) {
32+
root.right = insertRec(root.right, data);
3033
}
3134
}
3235
return root;
3336
}
3437

35-
void inorder(){
38+
void inorder () {
3639
inorderRec(root);
3740
System.out.println();
3841
}
39-
private void inorderRec(Node root){
40-
if(root!=null){
42+
43+
private void inorderRec (Node root) {
44+
if (root != null) {
4145
inorderRec(root.left);
42-
System.out.print(root.data+" ");
46+
System.out.print(root.data + " ");
4347
inorderRec(root.right);
4448
}
4549
}
4650

47-
void preorder(){
51+
void preorder () {
4852
preorderRec(root);
53+
System.out.println();
4954
}
50-
private void preorderRec(Node root){
51-
if(root!=null){
52-
System.out.print(root.data+" ");
55+
56+
private void preorderRec (Node root) {
57+
if (root != null) {
58+
System.out.print(root.data + " ");
5359
preorderRec(root.left);
5460
preorderRec(root.right);
5561
}
5662
}
57-
58-
void postorder(){
59-
postorderRec(root);
60-
}
61-
private void postorderRec(Node root){
62-
if(root!=null){
63-
postorderRec(root.left);
64-
postorderRec(root.right);
65-
System.out.print(root.data+" ");
66-
}
67-
}
68-
6963
}

0 commit comments

Comments
 (0)