@@ -59,13 +59,23 @@ const canvas = document.getElementById("main-canvas") as HTMLCanvasElement;
59
59
}
60
60
}
61
61
62
- function drawNode ( ctx : CanvasRenderingContext2D , position : Vector2D ) {
62
+ function drawNode ( ctx : CanvasRenderingContext2D , rootValue : number , position : Vector2D ) {
63
+ ctx . fillStyle = "#000000ff" ;
64
+ ctx . textAlign = "center" ;
65
+ ctx . textBaseline = "middle" ;
66
+ ctx . font = "20px system-ui, sans-serif"
67
+ ctx . fillText ( rootValue . toString ( ) , position . x , position . y ) ;
68
+
69
+ ctx . fillStyle = "white" ;
70
+
63
71
ctx . beginPath ( ) ;
64
- ctx . arc ( position . x , position . y , 10 , 0 , Math . PI * 2 , true ) ;
72
+ ctx . arc ( position . x , position . y , 20 , 0 , Math . PI * 2 , true ) ;
65
73
66
74
ctx . fill ( ) ;
67
75
}
68
76
function connectPoints ( ctx : CanvasRenderingContext2D , position1 : Vector2D , position2 : Vector2D ) {
77
+ ctx . lineWidth = 5 ;
78
+
69
79
ctx . beginPath ( ) ;
70
80
ctx . moveTo ( position1 . x , position1 . y ) ;
71
81
ctx . lineTo ( position2 . x , position2 . y ) ;
@@ -135,53 +145,103 @@ const canvas = document.getElementById("main-canvas") as HTMLCanvasElement;
135
145
136
146
return result
137
147
}
148
+ function insert ( tree : BinaryTree , value : number ) {
149
+ if ( value > tree . getRoot ( ) ) {
150
+ if ( tree . getLeft ( ) === null && tree . getRight ( ) === null ) {
151
+ tree . insertRight ( value ) ;
152
+ } else {
153
+ insert ( tree . getRight ( ) , value )
154
+ }
155
+ } else {
156
+ if ( tree . getLeft ( ) === null && tree . getRight ( ) === null ) {
157
+ tree . insertLeft ( value ) ;
158
+ } else {
159
+ insert ( tree . getLeft ( ) , value )
160
+ }
161
+ }
162
+ }
163
+ function drawTree ( ctx : CanvasRenderingContext2D , tree : BinaryTree , canvasWidth : number , scale : number , i = 0 , rootPosition : any = null ) {
164
+ if ( tree ) {
165
+ if ( rootPosition === null ) {
166
+ rootPosition = new Vector2D ( canvasWidth / 2 , 50 ) ;
167
+ }
168
+
169
+ drawNode ( ctx , tree . getRoot ( ) , rootPosition ) ;
170
+
171
+ if ( tree . getLeft ( ) ) {
172
+ let leftPosition = new Vector2D ( rootPosition . x - ( 100 / ( i + 1 ) ) * scale , rootPosition . y + 50 ) ;
173
+
174
+ drawNode ( ctx , tree . getLeft ( ) . getRoot ( ) , leftPosition ) ;
175
+ connectPoints ( ctx , rootPosition , leftPosition ) ;
176
+ drawTree ( ctx , tree . getLeft ( ) , canvasWidth , scale , i + 1 , leftPosition ) ;
177
+ }
178
+ if ( tree . getRight ( ) ) {
179
+ let rightPosition = new Vector2D ( rootPosition . x + ( 100 / ( i + 1 ) ) * scale , rootPosition . y + 50 ) ;
180
+
181
+ drawNode ( ctx , tree . getRight ( ) . getRoot ( ) , rightPosition ) ;
182
+ connectPoints ( ctx , rootPosition , rightPosition ) ;
183
+ drawTree ( ctx , tree . getRight ( ) , canvasWidth , scale , i + 1 , rightPosition ) ;
184
+ }
185
+ }
186
+ }
187
+ function updateTraversals ( tree : BinaryTree ) {
188
+ var preOrderElement = document . getElementById ( 'preorder-list' ) as HTMLElement ;
189
+ preOrderElement . innerText = preOrder ( tree ) . toString ( ) ;
190
+
191
+ var inOrderElement = document . getElementById ( 'inorder-list' ) as HTMLElement ;
192
+ inOrderElement . innerText = inOrder ( tree ) . toString ( ) ;
193
+
194
+ var postOrderElement = document . getElementById ( 'postorder-list' ) as HTMLElement ;
195
+ postOrderElement . innerText = postOrder ( tree ) . toString ( ) ;
196
+ }
138
197
139
- if ( canvas . getContext ) {
140
- const ctx = canvas . getContext ( "2d" ) as CanvasRenderingContext2D ;
198
+ function buildCanvas ( ) {
199
+ if ( canvas . getContext ) {
200
+ const ctx = canvas . getContext ( "2d" ) as CanvasRenderingContext2D ;
201
+
202
+ var scale = 1 ;
203
+ canvas . style . width = '50vw' ;
204
+ canvas . style . height = '50vh' ;
205
+ canvas . width = canvas . offsetWidth ;
206
+ canvas . height = canvas . offsetHeight ;
207
+
208
+ ctx . globalCompositeOperation = 'destination-over' ;
209
+
210
+ ctx . fillStyle = "white" ;
211
+ ctx . strokeStyle = "#fca311ff" ;
212
+
213
+ var tree = new BinaryTree ( 1 ) ;
214
+ tree . insertLeft ( 2 ) ;
215
+ tree . insertRight ( 7 ) ;
216
+ tree . getLeft ( ) . insertLeft ( 3 ) ;
217
+ tree . getLeft ( ) . insertRight ( 6 ) ;
218
+ tree . getLeft ( ) . getLeft ( ) . insertLeft ( 4 ) ;
219
+ tree . getLeft ( ) . getLeft ( ) . insertRight ( 5 ) ;
220
+ tree . getRight ( ) . insertLeft ( 8 ) ;
221
+ tree . getRight ( ) . insertRight ( 9 ) ;
222
+
223
+ updateTraversals ( tree ) ;
224
+
225
+ console . log ( "MIN" , minRoot ( tree ) ) ;
226
+ console . log ( "MAX" , maxRoot ( tree ) ) ;
227
+
228
+ drawTree ( ctx , tree , canvas . width , scale ) ;
229
+
230
+ var number = document . getElementById ( 'insert-form-input' ) as HTMLInputElement ;
231
+ var numberForm = document . getElementById ( 'insert-form' ) as HTMLElement ;
232
+
233
+ // if (numberForm.addEventListener){
234
+
235
+ // }
236
+ numberForm . addEventListener ( "submit" , function ( ) {
237
+ console . log ( number . value ) ;
238
+ insert ( tree , parseInt ( number . value ) )
239
+ drawTree ( ctx , tree , canvas . width , scale ) ;
240
+ updateTraversals ( tree ) ;
241
+ } , false ) ;
242
+ }
243
+ }
141
244
142
- var scale = 1 ;
143
- canvas . style . width = '50vw' ;
144
- canvas . style . height = '50vh' ;
145
- canvas . width = canvas . offsetWidth ;
146
- canvas . height = canvas . offsetHeight ;
245
+ buildCanvas ( ) ;
147
246
148
- ctx . fillStyle = "white" ;
149
- ctx . strokeStyle = "#fca311ff" ;
150
-
151
- // const points = [
152
- // [canvas.offsetWidth / 2, 10],
153
- // [canvas.offsetWidth / 4, 100],
154
- // [canvas.offsetWidth / 1.5, 100],
155
- // [canvas.offsetWidth / 8, 200],
156
- // [canvas.offsetWidth / 2.5, 200],
157
- // [canvas.offsetWidth / 1.2, 200],
158
- // [canvas.offsetWidth / 16, 300],
159
- // [canvas.offsetWidth / 4.5, 300],
160
- // ];
161
-
162
- // points.forEach((point) => {
163
- // drawLine(ctx, point[0], point[1]);
164
- // });
165
-
166
- // points.forEach((point) => {
167
- // drawNode(ctx, point[0], point[1]);
168
- // });
169
-
170
- var tree = new BinaryTree ( 1 )
171
- tree . insertLeft ( 2 )
172
- tree . insertRight ( 7 )
173
- tree . getLeft ( ) . insertLeft ( 3 )
174
- tree . getLeft ( ) . insertRight ( 6 )
175
- tree . getLeft ( ) . getLeft ( ) . insertLeft ( 4 )
176
- tree . getLeft ( ) . getLeft ( ) . insertRight ( 5 )
177
- tree . getRight ( ) . insertLeft ( 8 )
178
- tree . getRight ( ) . insertRight ( 9 )
179
-
180
- let preOrderTree = postOrder ( tree ) ;
181
- for ( let i = 0 ; i < preOrderTree . length ; i ++ ) {
182
- console . log ( preOrderTree [ i ] ) ;
183
- }
184
-
185
- console . log ( "MIN" , minRoot ( tree ) ) ;
186
- console . log ( "MAX" , maxRoot ( tree ) ) ;
187
- }
247
+ window . addEventListener ( "resize" , buildCanvas ) ;
0 commit comments