File tree Expand file tree Collapse file tree 3 files changed +61
-0
lines changed Expand file tree Collapse file tree 3 files changed +61
-0
lines changed Original file line number Diff line number Diff line change
1
+ public class Node <T extends Comparable > {
2
+
3
+ public Node left ;
4
+ public Node right ;
5
+ public T data ;
6
+
7
+ public Node (T data ){
8
+ this .left = null ;
9
+ this .right = null ;
10
+ this .data = data ;
11
+ }
12
+
13
+ public void printTree (){
14
+ if (left != null ){
15
+ left .printTree ();
16
+ }
17
+ System .out .println (data );
18
+ if (right != null ){
19
+ right .printTree ();
20
+ }
21
+ }
22
+
23
+ public void insert (T data ){
24
+ if (this .data != null ){
25
+ if (this .data .compareTo (data ) < 0 ){
26
+ if (this .left == null ){
27
+ this .left = new Node <T >(data );
28
+ return ;
29
+ } else {
30
+ this .left .insert (data );
31
+ }
32
+ }
33
+ else if (this .data .compareTo (data ) > 0 ){
34
+ if (this .right == null ){
35
+ this .right = new Node <T >(data );
36
+ return ;
37
+ } else {
38
+ this .right .insert (data );
39
+ }
40
+ }
41
+ }
42
+ else {
43
+ this .data = data ;
44
+ }
45
+ }
46
+
47
+ public void search_value (T data ){
48
+ if (this .data == data ){
49
+ System .out .println (String .format ("Value %s is found" , data ));
50
+ } else if (this .data .compareTo (data ) < 0 ){
51
+ this .left .search_value (data );
52
+ return ;
53
+ } else if (this .data .compareTo (data ) > 0 ){
54
+ this .right .search_value (data );
55
+ return ;
56
+ } else {
57
+ System .out .println (String .format ("Value %s does not exist!" , data ));
58
+ return ;
59
+ }
60
+ }
61
+ }
File renamed without changes.
File renamed without changes.
You can’t perform that action at this time.
0 commit comments