File tree Expand file tree Collapse file tree 3 files changed +69
-4
lines changed Expand file tree Collapse file tree 3 files changed +69
-4
lines changed Original file line number Diff line number Diff line change @@ -209,14 +209,16 @@ public static void main(String[] args) {
209
209
210
210
System .out .println ("\n ----------Pre Order----------" );
211
211
tree .traversePreOrder ();
212
-
212
+
213
213
System .out .println ("\n ----------In Order----------" );
214
214
tree .traverseInOrder ();
215
-
215
+
216
216
System .out .println ("\n ----------Post Order----------" );
217
217
tree .traversePostOrder ();
218
-
219
-
218
+
219
+ System .out .println ("\n " + tree .get (27 ));
220
+ System .out .println (tree .min ());
221
+ System .out .println (tree .max ());
220
222
221
223
}
222
224
Original file line number Diff line number Diff line change @@ -35,4 +35,28 @@ public void traversePostOrder() {
35
35
}
36
36
}
37
37
38
+ public TreeNode get (int value ) {
39
+ if (root != null ) {
40
+ return root .get (value );
41
+ }
42
+
43
+ return null ;
44
+ }
45
+
46
+ public int min () {
47
+ if (root == null ) {
48
+ return Integer .MIN_VALUE ;
49
+ } else {
50
+ return root .min ();
51
+ }
52
+ }
53
+
54
+ public int max () {
55
+ if (root == null ) {
56
+ return Integer .MAX_VALUE ;
57
+ } else {
58
+ return root .max ();
59
+ }
60
+ }
61
+
38
62
}
Original file line number Diff line number Diff line change @@ -68,6 +68,40 @@ public void traversePostOrder() {
68
68
System .out .print (data + " " );
69
69
}
70
70
71
+ public TreeNode get (int value ) {
72
+ if (value == data ) {
73
+ return this ;
74
+ }
75
+
76
+ if (value < data ) {
77
+ if (leftChild != null ) {
78
+ return leftChild .get (value );
79
+ }
80
+ } else {
81
+ if (rightChild != null ) {
82
+ return rightChild .get (value );
83
+ }
84
+ }
85
+
86
+ return null ;
87
+ }
88
+
89
+ public int min () {
90
+ if (leftChild == null ) {
91
+ return data ;
92
+ } else {
93
+ return leftChild .min ();
94
+ }
95
+ }
96
+
97
+ public int max () {
98
+ if (rightChild == null ) {
99
+ return data ;
100
+ } else {
101
+ return rightChild .max ();
102
+ }
103
+ }
104
+
71
105
public int getData () {
72
106
return data ;
73
107
}
@@ -92,4 +126,9 @@ public void setRightChild(TreeNode rightChild) {
92
126
this .rightChild = rightChild ;
93
127
}
94
128
129
+ @ Override
130
+ public String toString () {
131
+ return "[Data=" + data + "]" ;
132
+ }
133
+
95
134
}
You can’t perform that action at this time.
0 commit comments