33public class BinarySearchTree {
44 class Node {
55 int key ;
6- Node left ,right ;
7- Node (int key ){
6+ Node left , right ;
7+ Node (int key ) {
88 this .key = key ;
99 left = right = null ;
1010 }
11-
1211 }
1312
1413 Node root ;
1514
16- public void insert (int key ){
17- root = insertRec (root ,key );
15+ public void insert (int key ) {
16+ root = insertRec (root , key );
1817 }
1918
20- public Node insertRec (Node root , int key ){
21- if (root == null ){
19+ public Node insertRec (Node root , int key ) {
20+ if (root == null ) {
2221 return new Node (key );
2322 }
24- if (key < root .key ){
25- root .left = insertRec (root .left ,key );
26- }else if (key > root .key ){
27- root .right = insertRec (root .right ,key );
23+ if (key < root .key ) {
24+ root .left = insertRec (root .left , key );
25+ } else if (key > root .key ) {
26+ root .right = insertRec (root .right , key );
2827 }
2928 return root ;
3029 }
3130
32- public void inorder (){
31+ public void inorder () {
3332 inorderRec (root );
3433 }
35-
36- void inorderRec (Node root ){
37- if (root != null ){
34+
35+ public void inorderRec (Node root ) {
36+ if (root != null ) {
3837 inorderRec (root .left );
3938 System .out .print (root .key + " " );
4039 inorderRec (root .right );
4140 }
4241 }
4342
44- public void preOrder (){
43+ public void preOrder () {
4544 preOrderRec (root );
4645 }
4746
48- void preOrderRec (Node root ){
49- if (root !=null ){
47+ public void preOrderRec (Node root ) {
48+ if (root != null ) {
5049 System .out .print (root .key + " " );
5150 preOrderRec (root .left );
5251 preOrderRec (root .right );
5352 }
5453 }
5554
56- public void postOrder (){
55+ public void postOrder () {
5756 postOrderRec (root );
5857 }
5958
60- void postOrderRec (Node root ){
61- if (root != null ){
59+ public void postOrderRec (Node root ) {
60+ if (root != null ) {
6261 postOrderRec (root .left );
6362 postOrderRec (root .right );
6463 System .out .print (root .key + " " );
@@ -68,7 +67,7 @@ void postOrderRec(Node root){
6867 public static void main (String [] args ) {
6968 BinarySearchTree tree = new BinarySearchTree ();
7069 tree .insert (50 );
71- tree .insert (30 );
70+ tree .insert (30 );
7271 tree .insert (20 );
7372 tree .insert (40 );
7473 tree .insert (70 );
@@ -88,4 +87,3 @@ public static void main(String[] args) {
8887 System .out .println ();
8988 }
9089}
91-
0 commit comments