1
- package com .dataStructures ;
1
+ package com .datastructures ;
2
2
3
3
/**
4
4
* Binary tree for general value type, without redundancy
8
8
9
9
public class BinaryTree <T extends Comparable > {
10
10
private final T data ;
11
- private BinaryTree right , // the upper binary tree
11
+ private BinaryTree < T > right , // the upper binary tree
12
12
left ; // the lower binary tree
13
13
14
14
public BinaryTree (T data ) {
@@ -21,35 +21,38 @@ public String toString() {
21
21
}
22
22
23
23
/**
24
- * inserts a new value in it's correspondant place
24
+ * inserts a new value in it's correspondent place
25
25
*
26
26
* @param newDataValue value of the new binary tree to add on this tree
27
27
*/
28
28
public void insert (T newDataValue ) {
29
- this .insert (new BinaryTree (newDataValue ));
29
+ this .insert (new BinaryTree <> (newDataValue ));
30
30
}
31
31
32
32
/**
33
- * inserts a new binary tree in it's correspondant place
33
+ * inserts a new binary tree in it's correspondent place
34
34
*
35
35
* @param newData new value to add on this tree
36
36
*/
37
- public void insert (BinaryTree newData ) {
37
+ public void insert (BinaryTree < T > newData ) {
38
38
39
39
int cpr = newData .data .compareTo (this .data ); //new value comparission respect to actual value
40
40
41
- if (cpr < 0 )
42
- if (this .left == null )
41
+ if (cpr < 0 ) {
42
+ if (this .left == null ) {
43
43
this .setLeft (newData );
44
- else
44
+ } else {
45
45
this .left .insert (newData );
46
- else if (cpr > 0 )
47
- if (this .right == null )
46
+ }
47
+ } else if (cpr > 0 ) {
48
+ if (this .right == null ) {
48
49
this .setRight (newData );
49
- else
50
+ } else {
50
51
this .right .insert (newData );
51
- else
52
+ }
53
+ } else {
52
54
System .out .println ("Redundant value, not added" );
55
+ }
53
56
}
54
57
55
58
/**
@@ -58,8 +61,8 @@ else if (cpr > 0)
58
61
* @param data Searched value
59
62
* @return Binary tree which contains the value, null if it doesn't exist
60
63
*/
61
- public BinaryTree search (T data ) {
62
- int cpr = data .compareTo (this .data ); //new value comparission respect to actual value
64
+ public BinaryTree < T > search (T data ) {
65
+ int cpr = data .compareTo (this .data ); //new value comparison respect to actual value
63
66
64
67
if (cpr < 0 ) {
65
68
if (this .left == null )
@@ -113,19 +116,19 @@ public T getData() {
113
116
return data ;
114
117
}
115
118
116
- public BinaryTree getRight () {
119
+ public BinaryTree < T > getRight () {
117
120
return right ;
118
121
}
119
122
120
- public void setRight (BinaryTree right ) {
123
+ public void setRight (BinaryTree < T > right ) {
121
124
this .right = right ;
122
125
}
123
126
124
- public BinaryTree getLeft () {
127
+ public BinaryTree < T > getLeft () {
125
128
return left ;
126
129
}
127
130
128
- public void setLeft (BinaryTree left ) {
131
+ public void setLeft (BinaryTree < T > left ) {
129
132
this .left = left ;
130
133
}
131
134
}
0 commit comments