2
2
3
3
/**
4
4
* Binary tree for general value type, without redundancy
5
- * @author RICARDO
5
+ *
6
6
* @param <T> root data
7
7
*/
8
+
8
9
public class BinaryTree <T extends Comparable > {
9
10
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
12
13
13
14
public BinaryTree (T data ) {
14
15
this .data = data ;
15
16
}
16
17
17
18
@ Override
18
- public String toString (){
19
+ public String toString () {
19
20
return this .data .toString ();
20
21
}
21
-
22
+
22
23
/**
23
24
* 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
25
27
*/
26
- public void insert (T newDataValue ){
28
+ public void insert (T newDataValue ) {
27
29
this .insert (new BinaryTree (newDataValue ));
28
30
}
29
-
31
+
30
32
/**
31
33
* 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
33
36
*/
34
- public void insert (BinaryTree newData ){
35
-
37
+ public void insert (BinaryTree newData ) {
38
+
36
39
int cpr = newData .data .compareTo (this .data ); //new value comparission respect to actual value
37
-
40
+
38
41
if (cpr < 0 )
39
42
if (this .left == null )
40
43
this .setLeft (newData );
@@ -51,12 +54,13 @@ else if (cpr > 0)
51
54
52
55
/**
53
56
* 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
56
60
*/
57
- public BinaryTree search (T data ){
61
+ public BinaryTree search (T data ) {
58
62
int cpr = data .compareTo (this .data ); //new value comparission respect to actual value
59
-
63
+
60
64
if (cpr < 0 ) {
61
65
if (this .left == null )
62
66
return null ; //the value doesn't exist
@@ -65,43 +69,45 @@ public BinaryTree search(T data){
65
69
if (cpr > 0 ) {
66
70
if (this .right == null )
67
71
return null ; //the value doesn't exist
68
- return this .right .search (data );
72
+ return this .right .search (data );
69
73
}
70
74
return this ;
71
75
}
72
-
76
+
73
77
/**
74
78
* Checks if the data value exist in the tree
79
+ *
75
80
* @param data data to be searched
76
81
* @return true if this tree contains the data value, false if not.
77
82
*/
78
- public boolean contains (T data ){
83
+ public boolean contains (T data ) {
79
84
return this .search (data ) != null ;
80
85
}
81
-
86
+
82
87
/**
83
88
* uses recursive black magic to print this tree in console
89
+ *
84
90
* @param tabCounter prev tabs
85
91
*/
86
- private void print (int tabCounter ){
92
+ private void print (int tabCounter ) {
87
93
for (int i = 0 ; i < tabCounter ; i ++)
88
94
System .out .print ("\t " );
89
-
95
+
90
96
System .out .println (this );
91
-
97
+
92
98
if (this .left != null )
93
99
this .left .print (tabCounter + 1 ); //it can't be ++ , pls don't change it
94
100
if (this .right != null )
95
101
this .right .print (tabCounter + 1 ); //it can't be ++ , pls don't change it
96
102
}
97
-
103
+
98
104
/**
99
105
* uses black magic to print this tree in console
100
106
*/
101
- public void print (){
107
+ public void print () {
102
108
this .print (0 );
103
109
}
104
-
110
+
105
111
//getters and setters
106
112
public T getData () {
107
113
return data ;
0 commit comments