1313 * </p>
1414 *
1515 * @author [Madhur Panwar](<a href="https://github.com/mdrpanwar">git-Madhur Panwar</a>)
16+ * @author [Udaya Krishnan M](<a href="https://github.com/UdayaKrishnanM/">git-Udaya Krishnan M</a>) {added prettyDisplay() method}
1617 */
1718public class BSTRecursiveGeneric <T extends Comparable <T >> {
1819
@@ -28,6 +29,29 @@ public BSTRecursiveGeneric() {
2829 root = null ;
2930 }
3031
32+ /**
33+ * Displays the tree is a structed format
34+ */
35+ public void prettyDisplay (){
36+ prettyDisplay (root , 0 );
37+ }
38+
39+ private void prettyDisplay (Node <T > node , int level ){
40+ if (node == null ){
41+ return ;
42+ }
43+ prettyDisplay (node .right , level + 1 );
44+ if (level != 0 ){
45+ for (int i = 0 ; i < level - 1 ; i ++){
46+ System .out .print ("|\t " );
47+ }
48+ System .out .println ("|---->" + node .data );
49+ } else {
50+ System .out .println (node .data );
51+ }
52+ prettyDisplay (node .left , level + 1 );
53+ }
54+
3155 /**
3256 * main function for testing
3357 */
@@ -38,7 +62,12 @@ public static void main(String[] args) {
3862
3963 integerTree .add (5 );
4064 integerTree .add (10 );
41- integerTree .add (9 );
65+ integerTree .add (-9 );
66+ integerTree .add (4 );
67+ integerTree .add (3 );
68+ integerTree .add (1 );
69+ System .out .println ("Pretty Display of current tree is:" );
70+ integerTree .prettyDisplay ();
4271 assert !integerTree .find (4 )
4372 : "4 is not yet present in BST" ;
4473 assert integerTree .find (10 )
@@ -54,16 +83,21 @@ public static void main(String[] args) {
5483 assert integerTree .find (70 )
5584 : "70 was inserted but not found" ;
5685 /*
57- Will print in following order
58- 5 10 20 70
86+ Will print in following order
87+ 5 10 20 70
5988 */
89+ System .out .println ("Pretty Display of current tree is:" );
90+ integerTree .prettyDisplay ();
6091 integerTree .inorder ();
92+ System .out .println ("Pretty Display of current tree is:" );
93+ integerTree .prettyDisplay ();
6194 System .out .println ();
6295 System .out .println ("Testing for string data..." );
6396 // String
6497 BSTRecursiveGeneric <String > stringTree = new BSTRecursiveGeneric <String >();
6598
6699 stringTree .add ("banana" );
100+ stringTree .add ("apple" );
67101 stringTree .add ("pineapple" );
68102 stringTree .add ("date" );
69103 assert !stringTree .find ("girl" )
@@ -80,11 +114,15 @@ public static void main(String[] args) {
80114 stringTree .add ("hills" );
81115 assert stringTree .find ("hills" )
82116 : "hills was inserted but not found" ;
117+ System .out .println ("Pretty Display of current tree is:" );
118+ stringTree .prettyDisplay ();
83119 /*
84120 Will print in following order
85121 banana hills india pineapple
86122 */
87123 stringTree .inorder ();
124+ System .out .println ("Pretty Display of current tree is:" );
125+ stringTree .prettyDisplay ();
88126 }
89127
90128 /**
0 commit comments