11package com .thealgorithms .tree ;
22
3-
43import java .util .Arrays ;
54
65/**
@@ -22,28 +21,25 @@ class Node {
2221 private Node right ;
2322 private int height ;
2423
25- public Node (int value ) {
24+ Node (int value ) {
2625 this .value = value ;
2726 }
2827
29- public int getValue () {
28+ int getValue () {
3029 return value ;
3130 }
32-
33- public Node getLeft () {
31+
32+ Node getLeft () {
3433 return left ;
3534 }
36-
37- public Node getRight () {
35+
36+ Node getRight () {
3837 return right ;
3938 }
40-
41- public int getHeight () {
39+
40+ int getHeight () {
4241 return height ;
4342 }
44-
45-
46-
4743 }
4844
4945 private Node root ;
@@ -58,7 +54,7 @@ public BinarySearchTree() {
5854 public int height (Node node ) {
5955 return node == null ? -1 : node .height ;
6056 }
61-
57+
6258 /**
6359 * Returns the root node of the BST.
6460 * Used for testing and internal inspection.
@@ -116,7 +112,9 @@ public boolean balanced() {
116112 }
117113
118114 private boolean balanced (Node node ) {
119- if (node == null ) return true ;
115+ if (node == null ) {
116+ return true ;
117+ }
120118
121119 int balanceFactor = Math .abs (height (node .left ) - height (node .right ));
122120 System .out .println ("Node value: " + node .value + " | Balance Factor: " + balanceFactor );
@@ -132,7 +130,9 @@ public void prettyDisplay() {
132130 }
133131
134132 private void prettyDisplay (Node node , int level ) {
135- if (node == null ) return ;
133+ if (node == null ) {
134+ return ;
135+ }
136136
137137 prettyDisplay (node .right , level + 1 );
138138
@@ -141,9 +141,10 @@ private void prettyDisplay(Node node, int level) {
141141 System .out .print ("|\t " );
142142 }
143143 System .out .println ("|----> " + node .value );
144- } else {
144+ } else {
145145 System .out .println (node .value );
146146 }
147+
147148 prettyDisplay (node .left , level + 1 );
148149 }
149150
@@ -156,7 +157,9 @@ public void populateSorted(int[] nums) {
156157 }
157158
158159 private void populateSorted (int [] nums , int start , int end ) {
159- if (start >= end ) return ;
160+ if (start >= end ) {
161+ return ;
162+ }
160163
161164 int mid = start + (end - start ) / 2 ;
162165 insert (nums [mid ]);
@@ -173,7 +176,10 @@ public void preOrder() {
173176 }
174177
175178 private void preOrder (Node node ) {
176- if (node == null ) return ;
179+ if (node == null ) {
180+ return ;
181+ }
182+
177183 System .out .print (node .value + " " );
178184 preOrder (node .left );
179185 preOrder (node .right );
@@ -182,13 +188,16 @@ private void preOrder(Node node) {
182188 /**
183189 * Inorder traversal: Left -> Root -> Right
184190 */
185- public void inOrder (){
191+ public void inOrder () {
186192 inOrder (root );
187193 System .out .println ();
188194 }
189195
190196 private void inOrder (Node node ) {
191- if (node == null ) return ;
197+ if (node == null ) {
198+ return ;
199+ }
200+
192201 inOrder (node .left );
193202 System .out .print (node .value + " (height: " + node .height + ") | " );
194203 inOrder (node .right );
@@ -203,7 +212,10 @@ public void postOrder() {
203212 }
204213
205214 private void postOrder (Node node ) {
206- if (node == null ) return ;
215+ if (node == null ) {
216+ return ;
217+ }
218+
207219 postOrder (node .left );
208220 postOrder (node .right );
209221 System .out .print (node .value + " " );
@@ -217,7 +229,10 @@ public void display() {
217229 }
218230
219231 private void display (Node node , String details ) {
220- if (node == null ) return ;
232+ if (node == null ) {
233+ return ;
234+ }
235+
221236 System .out .println (details + node .value );
222237 display (node .left , "Left child of " + node .value + ": " );
223238 display (node .right , "Right child of " + node .value + ": " );
0 commit comments