1
+ class BinaryTree <T extends Comparable <T >> {
2
+
3
+ private class Node <T extends Comparable <T >>{
4
+ T value ;
5
+ Node <T > left ;
6
+ Node <T > right ;
7
+
8
+ Node (T value ){
9
+ this .value = value ;
10
+ this .left =null ;
11
+ this .right =null ;
12
+ }
13
+
14
+ public int compareTo (T value ){
15
+ return this .value .compareTo (value );
16
+ }
17
+ }
18
+
19
+ private Node <T > root ;
20
+
21
+ private Node <T > addrecursively (Node <T > current , T value ){
22
+
23
+ if (current == null )
24
+ return new Node (value );
25
+ else if (current .value .compareTo (value ) > 0 )
26
+ current .left = addrecursively (current .left , value );
27
+ else
28
+ current .right = addrecursively (current .right , value );
29
+
30
+ return current ;
31
+ }
32
+
33
+ public void add (T value ){
34
+ root = addrecursively (root , value );
35
+ }
36
+
37
+ public Node <T > searchRecursively (Node <T > root , T value ){
38
+ if (root .value .compareTo (value ) > 0 ) {
39
+ root = searchRecursively (root .left , value );
40
+ } else if (root .value .compareTo (value ) < 0 ) {
41
+ root = searchRecursively (root .right , value );
42
+ } else if (root .value .compareTo (value ) == 0 )
43
+ return root ;
44
+
45
+ return root ;
46
+ }
47
+
48
+ public Node <T > getNodeParent (Node <T > curNode , T value , Node <T > parent ){
49
+
50
+ if (curNode ==null )
51
+ return null ;
52
+
53
+ else if (curNode .value .compareTo (value )>0 ){
54
+ parent = getNodeParent (curNode .left , value , curNode );
55
+ }
56
+ else if (curNode .value .compareTo (value )<0 ){
57
+ parent = getNodeParent (curNode .right , value , curNode );
58
+ }
59
+ else if (curNode .value .compareTo (value ) == 0 )
60
+ return parent ;
61
+
62
+ return parent ;
63
+
64
+ }
65
+
66
+ public Node <T > findMinimum (Node <T > node ){
67
+ while (node .left !=null ){
68
+ node = node .left ;
69
+ }
70
+ return node ;
71
+ }
72
+
73
+ public void remove (T value ){
74
+ delete (root , value );
75
+ }
76
+
77
+ public void delete (Node <T > node , T value ){
78
+
79
+ Node <T > curr = node ;
80
+ Node <T > parent = null ;
81
+
82
+ curr = searchRecursively (curr , value );
83
+ if (curr == null )
84
+ return ;
85
+
86
+ parent = getNodeParent (node , curr .value , parent );
87
+
88
+ if (curr .left ==null && curr .right ==null ){
89
+ if (parent !=null ){
90
+ if (parent .left ==curr ){
91
+ parent .left =null ;
92
+ }
93
+ else
94
+ parent .right =null ;
95
+ }
96
+ else {
97
+ curr = null ;
98
+ return ;
99
+ }
100
+ }
101
+ else if (curr .left !=null && curr .right !=null ){
102
+
103
+ Node <T > smallestNode = findMinimum (curr .right );
104
+ T val = smallestNode .value ;
105
+ delete (root , val );
106
+ curr .value =val ;
107
+ }
108
+
109
+ else if (curr .left ==null ){
110
+ if (parent .left ==curr ){
111
+ parent .left = curr .right ;
112
+ }
113
+ else {
114
+ parent .right =curr .right ;
115
+ }
116
+ }
117
+ else if (curr .right ==null ){
118
+ if (parent .left ==curr ){
119
+ parent .left = curr .left ;
120
+ }
121
+ else {
122
+ parent .right =curr .left ;
123
+ }
124
+ }
125
+
126
+ }
127
+
128
+
129
+ public void traverseDfsInorder (Node node ){
130
+
131
+ if (node !=null ){
132
+ traverseDfsInorder (node .left );
133
+ System .out .print (" " +node .value );
134
+ traverseDfsInorder (node .right );
135
+ }
136
+
137
+ }
138
+
139
+ public void traverseDfsPreorder (Node node ){
140
+
141
+ if (node !=null ){
142
+ System .out .print (" " +node .value );
143
+ traverseDfsInorder (node .left );
144
+ traverseDfsInorder (node .right );
145
+ }
146
+
147
+ }
148
+
149
+ public void traverseDfsPostorder (Node node ){
150
+
151
+ if (node !=null ){
152
+ traverseDfsInorder (node .left );
153
+ traverseDfsInorder (node .right );
154
+ System .out .print (" " +node .value );
155
+ }
156
+
157
+ }
158
+
159
+ public void printTree (){
160
+ System .out .println ("Inorder: " );
161
+ traverseDfsInorder (root );
162
+
163
+ System .out .println ("\n Preorder: " );
164
+ traverseDfsPreorder (root );
165
+
166
+ System .out .println ("\n Postorder: " );
167
+ traverseDfsPostorder (root );
168
+
169
+ System .out .println ();
170
+ }
171
+
172
+ }
173
+
174
+ public class BinaryTreeClass {
175
+
176
+ public static void main (String args []){
177
+
178
+ BinaryTree tree = new BinaryTree ();
179
+ tree .add (11 );
180
+ tree .add (15 );
181
+ tree .add (41 );
182
+ tree .add (8 );
183
+ tree .add (6 );
184
+ tree .add (45 );
185
+ tree .add (75 );
186
+ tree .add (23 );
187
+ tree .add (3 );
188
+ tree .add (9 );
189
+
190
+ System .out .println ("-----Traversing order----" );
191
+ tree .printTree ();
192
+
193
+ tree .remove (11 );
194
+ tree .remove (15 );
195
+
196
+ System .out .println ("-----After Deleting Traversing order----" );
197
+ tree .printTree ();
198
+
199
+ }
200
+
201
+ }
0 commit comments