11package com .thealgorithms .tree ;
2- import java .util .*;
3- /*
2+
3+ import java .util .Scanner ;
4+
5+ /**
46 * A simple implementation of a Binary Search Tree (BST) in Java.
57 * Supports insertion of integer values and tree traversals: inorder, preorder, and postorder.
68 */
79public class BinarySearchTree {
810
9- //node class
10- static class node {
11- int data ;
12- node left ;
13- node right ;
11+ // Node class
12+ static class Node {
13+ int data ;
14+ Node left , right ;
1415
15- node (int d ) {
16- this .data = d ;
16+ Node (int d ) {
17+ this .data = d ;
18+ }
1719 }
18- }
1920
20- //insert value into BST
21- public static node insert_recurssive (node root , int d ) {
22- if (root == null ) {
23- root = new node (d );
24- return root ;
25- }
26- if (root .data > d ) {
27- //left subtree
28- root .left = insert_recurssive (root .left , d );
29- } else {
30- //right subtree
31- root .right = insert_recurssive (root .right , d );
21+ // Insert value into BST recursively
22+ public static Node insertRecursive (Node root , int d ) {
23+ if (root == null ) {
24+ return new Node (d );
25+ }
26+ if (d < root .data ) {
27+ root .left = insertRecursive (root .left , d );
28+ } else {
29+ root .right = insertRecursive (root .right , d );
30+ }
31+ return root ;
3232 }
33- return root ;
34- }
3533
36- //inorder traversal
37- public static void inorder (node root ) {
38- if (root == null ) {
39- return ;
34+ // Inorder traversal
35+ public static void inorder (Node root ) {
36+ if (root == null ) return ;
37+ inorder (root .left );
38+ System .out .print (root .data + " " );
39+ inorder (root .right );
4040 }
41- inorder (root .left );
42- System .out .print (root .data + " " );
43- inorder (root .right );
44- }
4541
46- //postorder traversal
47- public static void postorder (node root ) {
42+ // Preorder traversal
43+ public static void preorder (Node root ) {
44+ if (root == null ) return ;
45+ System .out .print (root .data + " " );
46+ preorder (root .left );
47+ preorder (root .right );
48+ }
4849
49- if (root == null ) {
50- return ;
50+ // Postorder traversal
51+ public static void postorder (Node root ) {
52+ if (root == null ) return ;
53+ postorder (root .left );
54+ postorder (root .right );
55+ System .out .print (root .data + " " );
5156 }
52- postorder (root .left );
53- postorder (root .right );
54- System .out .print (root .data + " " );
55- }
5657
57- //preorder traversal
58- public static void preorder (node root ) {
58+ // Main method
59+ public static void main (String [] args ) {
60+ Scanner sc = new Scanner (System .in );
5961
60- if (root == null ) {
61- return ;
62- }
63- System .out .print (root .data + " " );
64- preorder (root .left );
65- preorder (root .right );
66- }
67-
68- //main method
69- public static void main (String [] args ) {
70-
71- Scanner sc = new Scanner (System .in );
72- System .out .println ("Enter the number of integer elements" );
73- int n = sc .nextInt ();
74-
75- int values [] = new int [n ];
76- System .out .println ("Enter " + n + " values:-" );
77- for (int i = 0 ; i < n ; i ++)
78- values [i ] = sc .nextInt ();
79- sc .close ();
80- node root = null ;
81- //insert values into BST
82- for (int i = 0 ; i < values .length ; i ++) {
83- root = insert_recurssive (root , values [i ]);
84- }
62+ System .out .println ("Enter the number of elements:" );
63+ int n = sc .nextInt ();
64+
65+ int [] values = new int [n ];
66+ System .out .println ("Enter " + n + " values:" );
67+ for (int i = 0 ; i < n ; i ++) {
68+ values [i ] = sc .nextInt ();
69+ }
70+ sc .close ();
71+
72+ Node root = null ;
73+ for (int value : values ) {
74+ root = insertRecursive (root , value );
75+ }
76+
77+ // Traversal outputs
78+ System .out .println ("Inorder Traversal:" );
79+ inorder (root );
8580
86- //traversal of BST
87- System .out .println ("Inorder Traversal is:-" );
88- inorder (root );
89- System .out .println ("\n Postorder Traversal is:-" );
90- postorder (root );
91- System .out .println ("\n Preorder Traversal is:-" );
92- preorder (root );
93- }
81+ System .out .println ("\n Postorder Traversal:" );
82+ postorder (root );
83+
84+ System .out .println ("\n Preorder Traversal:" );
85+ preorder (root );
86+
87+ // Test Cases
88+ System .out .println ("\n \n Running basic test case..." );
89+
90+ int [] testValues = {
91+ 10 ,
92+ 5 ,
93+ 15 ,
94+ 3 ,
95+ 7 ,
96+ 12 ,
97+ 18
98+ };
99+ Node testRoot = null ;
100+ for (int val : testValues ) {
101+ testRoot = insertRecursive (testRoot , val );
102+ }
103+
104+ System .out .print ("Expected Inorder: 3 5 7 10 12 15 18\n Actual: " );
105+ inorder (testRoot );
106+
107+ System .out .print ("\n Expected Preorder: 10 5 3 7 15 12 18\n Actual: " );
108+ preorder (testRoot );
109+
110+ System .out .print ("\n Expected Postorder: 3 7 5 12 18 15 10\n Actual: " );
111+ postorder (testRoot );
112+
113+ System .out .println ("\n Basic test case complete." );
114+ }
94115}
0 commit comments