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 > {
910private 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
1314public BinaryTree (T data ) {
1415this .data =data ;
1516 }
1617
1718@ Override
18- public String toString (){
19+ public String toString () {
1920return 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 ) {
2729this .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+
3639int cpr =newData .data .compareTo (this .data );//new value comparission respect to actual value
37-
40+
3841if (cpr <0 )
3942if (this .left ==null )
4043this .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 ) {
5862int cpr =data .compareTo (this .data );//new value comparission respect to actual value
59-
63+
6064if (cpr <0 ) {
6165if (this .left ==null )
6266return null ;//the value doesn't exist
@@ -65,43 +69,45 @@ public BinaryTree search(T data){
6569if (cpr >0 ) {
6670if (this .right ==null )
6771return null ;//the value doesn't exist
68- return this .right .search (data );
72+ return this .right .search (data );
6973 }
7074return 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 ) {
7984return 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 ) {
8793for (int i =0 ;i <tabCounter ;i ++)
8894System .out .print ("\t " );
89-
95+
9096System .out .println (this );
91-
97+
9298if (this .left !=null )
9399this .left .print (tabCounter +1 );//it can't be ++ , pls don't change it
94100if (this .right !=null )
95101this .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 () {
102108this .print (0 );
103109 }
104-
110+
105111//getters and setters
106112public T getData () {
107113return data ;