1
- import { BinaryTreeNode } from "./BinaryTreeNode.js"
2
- import { getRequiredHeightAndWidth } from "./treeutils.js" ;
3
-
1
+ import { BinaryTreeNode } from "./BinaryTreeNode.js" ;
2
+ import {
3
+ DEFAULT_CONFIG ,
4
+ connectEdges ,
5
+ drawNode ,
6
+ getRequiredHeightAndWidth ,
7
+ } from "./treeutils.js" ;
4
8
5
9
const canvas = document . querySelector ( "canvas" ) ;
6
10
7
11
function drawBinaryTree ( root , canvasElement ) {
8
-
9
- const maxWidth = window . innerWidth ; // taking max width of window
10
- const maxHeight = window . innerHeight ; // taking max height of window
12
+ const maxWidth = window . innerWidth ; // taking max width of window
13
+ const maxHeight = window . innerHeight ; // taking max height of window
14
+
15
+ // Initialize the canvas dimensions
16
+ canvasElement . width = maxWidth ;
17
+ canvasElement . height = maxHeight ;
18
+
19
+ // calculate required width & height to draw the tree structure
20
+ const { requiredCanvasHeight, requiredCanvasWidth } =
21
+ getRequiredHeightAndWidth ( root ) ;
11
22
12
- // Initialize the canvas dimensions
13
- canvasElement . width = maxWidth ;
14
- canvasElement . height = maxHeight ;
23
+ // calculate center Points
15
24
16
- // calculate required width & height to draw the tree structure
17
- const { requiredCanvasHeight, requiredCanvasWidth } =
18
- getRequiredHeightAndWidth ( root ) ;
25
+ const windowWidthCenter = maxWidth / 2 ;
26
+ const requiredWidthCenter = requiredCanvasWidth / 2 ;
19
27
28
+ const xStart = windowWidthCenter - requiredWidthCenter ;
29
+ const xEnd = windowWidthCenter + requiredWidthCenter ;
20
30
21
- // calculate center Points
22
-
23
- const windowWidthCenter = maxWidth / 2 ;
24
- const requiredWidthCenter = requiredCanvasWidth / 2 ;
31
+ const horizontalConfig = { xStart, xEnd } ;
25
32
26
- const xStart = windowWidthCenter - requiredWidthCenter ;
27
- const xEnd = windowWidthCenter + requiredWidthCenter ;
33
+ // Draw
28
34
29
- const horizontalConfig = { xStart, xEnd }
30
-
31
- // Draw
35
+ recursivelyDrawNotes ( root , canvasElement , 0.5 , horizontalConfig ) ;
32
36
}
33
37
38
+ function recursivelyDrawNotes (
39
+ root ,
40
+ canvasElement ,
41
+ currentLine ,
42
+ horizontalConfig
43
+ ) {
44
+ const { xStart, xEnd } = horizontalConfig ;
45
+
46
+ const xPos = ( xStart + xEnd ) / 2 ; // Find x-position for circle
47
+ const yPos = currentLine * DEFAULT_CONFIG . nodeHeightSpacing ; // Find y-position for circle
48
+
49
+ drawNode ( root . value , canvasElement , xPos , yPos ) ; // draw the circle
50
+
51
+ if ( root . left !== null ) {
52
+ // recursively Left node created
53
+ const leftNodeHorizontalConfig = { xStart, xEnd : xPos } ;
54
+ recursivelyDrawNotes (
55
+ root . left ,
56
+ canvasElement ,
57
+ currentLine + 1 ,
58
+ leftNodeHorizontalConfig
59
+ ) ;
60
+
61
+ connectEdges (
62
+ canvasElement ,
63
+ {
64
+ xStart : xPos ,
65
+ xEnd : ( xStart + xPos ) / 2 ,
66
+ } ,
67
+ {
68
+ yStart : yPos + DEFAULT_CONFIG . radius ,
69
+ yEnd :
70
+ ( currentLine + 1 ) * DEFAULT_CONFIG . nodeHeightSpacing -
71
+ DEFAULT_CONFIG . radius ,
72
+ }
73
+ ) ;
74
+ }
75
+
76
+ if ( root . right !== null ) {
77
+ // recursively right node created
78
+ const rightNodeHorizontalConfig = { xStart : xPos , xEnd } ;
79
+ recursivelyDrawNotes (
80
+ root . right ,
81
+ canvasElement ,
82
+ currentLine + 1 ,
83
+ rightNodeHorizontalConfig
84
+ ) ;
85
+
86
+ connectEdges (
87
+ canvasElement ,
88
+ {
89
+ xStart : xPos ,
90
+ xEnd : ( xPos + xEnd ) / 2 ,
91
+ } ,
92
+ {
93
+ yStart : yPos + DEFAULT_CONFIG . radius ,
94
+ yEnd :
95
+ ( currentLine + 1 ) * DEFAULT_CONFIG . nodeHeightSpacing -
96
+ DEFAULT_CONFIG . radius ,
97
+ }
98
+ ) ;
99
+ }
100
+ }
34
101
35
- const root = new BinaryTreeNode ( 1 ) ; // creating first node
102
+ const root = new BinaryTreeNode ( 1 ) ; // creating first node
36
103
37
104
const node2 = new BinaryTreeNode ( 2 ) ;
38
105
root . setLeft ( node2 ) ;
39
106
40
107
const node3 = new BinaryTreeNode ( 3 ) ;
41
108
root . setRight ( node3 ) ;
42
109
43
- console . log ( root ) ;
110
+ const node4 = new BinaryTreeNode ( 4 ) ;
111
+ node3 . setLeft ( node4 ) ;
44
112
113
+ const node7 = new BinaryTreeNode ( 7 ) ;
114
+ node3 . setRight ( node7 ) ;
115
+
116
+ const node5 = new BinaryTreeNode ( 5 ) ;
117
+ node2 . setLeft ( node5 ) ;
118
+
119
+ const node6 = new BinaryTreeNode ( 6 ) ;
120
+ node2 . setRight ( node6 ) ;
121
+
122
+ console . log ( root ) ;
45
123
46
- drawBinaryTree ( root , canvas )
124
+ drawBinaryTree ( root , canvas ) ;
0 commit comments