22
33/**
44 * Binary tree for general value type, without redundancy
5- * @author RICARDO
5+ *
66 * @param <T> root data
77 */
8+
89public class BinaryTree <T extends Comparable > {
910 private final T data ;
10- private BinaryTree right , // the upper binary tree
11- left ; // the lower binary tree
11+ private BinaryTree right , // the upper binary tree
12+ left ; // the lower binary tree
1213
1314 public BinaryTree (T data ) {
1415 this .data = data ;
1516 }
1617
1718 @ Override
18- public String toString (){
19+ public String toString () {
1920 return this .data .toString ();
2021 }
21-
22+
2223 /**
2324 * inserts a new value in it's correspondant place
24- * @param newDataValue value of the new banary tree to add on this tree
25+ *
26+ * @param newDataValue value of the new binary tree to add on this tree
2527 */
26- public void insert (T newDataValue ){
28+ public void insert (T newDataValue ) {
2729 this .insert (new BinaryTree (newDataValue ));
2830 }
29-
31+
3032 /**
3133 * inserts a new binary tree in it's correspondant place
32- * @param newData new value to add on this tree
34+ *
35+ * @param newData new value to add on this tree
3336 */
34- public void insert (BinaryTree newData ){
35-
37+ public void insert (BinaryTree newData ) {
38+
3639 int cpr = newData .data .compareTo (this .data ); //new value comparission respect to actual value
37-
40+
3841 if (cpr < 0 )
3942 if (this .left == null )
4043 this .setLeft (newData );
@@ -51,12 +54,13 @@ else if (cpr > 0)
5154
5255 /**
5356 * search and specific value on the tree
54- * @param data Searched value
55- * @return Binary tree wich contains the value, null if it doesn't exist
57+ *
58+ * @param data Searched value
59+ * @return Binary tree which contains the value, null if it doesn't exist
5660 */
57- public BinaryTree search (T data ){
61+ public BinaryTree search (T data ) {
5862 int cpr = data .compareTo (this .data ); //new value comparission respect to actual value
59-
63+
6064 if (cpr < 0 ) {
6165 if (this .left == null )
6266 return null ; //the value doesn't exist
@@ -65,43 +69,45 @@ public BinaryTree search(T data){
6569 if (cpr > 0 ) {
6670 if (this .right == null )
6771 return null ; //the value doesn't exist
68- return this .right .search (data );
72+ return this .right .search (data );
6973 }
7074 return this ;
7175 }
72-
76+
7377 /**
7478 * Checks if the data value exist in the tree
79+ *
7580 * @param data data to be searched
7681 * @return true if this tree contains the data value, false if not.
7782 */
78- public boolean contains (T data ){
83+ public boolean contains (T data ) {
7984 return this .search (data ) != null ;
8085 }
81-
86+
8287 /**
8388 * uses recursive black magic to print this tree in console
89+ *
8490 * @param tabCounter prev tabs
8591 */
86- private void print (int tabCounter ){
92+ private void print (int tabCounter ) {
8793 for (int i = 0 ; i < tabCounter ; i ++)
8894 System .out .print ("\t " );
89-
95+
9096 System .out .println (this );
91-
97+
9298 if (this .left != null )
9399 this .left .print (tabCounter + 1 ); //it can't be ++ , pls don't change it
94100 if (this .right != null )
95101 this .right .print (tabCounter + 1 ); //it can't be ++ , pls don't change it
96102 }
97-
103+
98104 /**
99105 * uses black magic to print this tree in console
100106 */
101- public void print (){
107+ public void print () {
102108 this .print (0 );
103109 }
104-
110+
105111 //getters and setters
106112 public T getData () {
107113 return data ;
0 commit comments