-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawBinaryTree.js
135 lines (109 loc) · 3.17 KB
/
drawBinaryTree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { BinaryTreeNode } from "./BinaryTreeNode.js";
import {
DEFAULT_CONFIG,
connectEdges,
drawNode,
getRequiredHeightAndWidth,
treeConstructor,
} from "./treeutils.js";
const canvas = document.querySelector("canvas");
function drawBinaryTree(root, canvasElement) {
const maxWidth = window.innerWidth; // taking max width of window
const maxHeight = window.innerHeight; // taking max height of window
// Initialize the canvas dimensions
canvasElement.width = maxWidth;
canvasElement.height = maxHeight;
// calculate required width & height to draw the tree structure
const { requiredCanvasHeight, requiredCanvasWidth } =
getRequiredHeightAndWidth(root);
// calculate center Points
const windowWidthCenter = maxWidth / 2;
const requiredWidthCenter = requiredCanvasWidth / 2;
const xStart = windowWidthCenter - requiredWidthCenter;
const xEnd = windowWidthCenter + requiredWidthCenter;
const horizontalConfig = { xStart, xEnd };
// Draw
recursivelyDrawNotes(root, canvasElement, 0.5, horizontalConfig);
}
function recursivelyDrawNotes(
root,
canvasElement,
currentLine,
horizontalConfig
) {
const { xStart, xEnd } = horizontalConfig;
const xPos = (xStart + xEnd) / 2; // Find x-position for circle
const yPos = currentLine * DEFAULT_CONFIG.nodeHeightSpacing; // Find y-position for circle
drawNode(root.value, canvasElement, xPos, yPos); // draw the circle
if (root.left !== null) {
// recursively Left node created
const leftNodeHorizontalConfig = { xStart, xEnd: xPos };
recursivelyDrawNotes(
root.left,
canvasElement,
currentLine + 1,
leftNodeHorizontalConfig
);
connectEdges(
canvasElement,
{
xStart: xPos,
xEnd: (xStart + xPos) / 2,
},
{
yStart: yPos + DEFAULT_CONFIG.radius,
yEnd:
(currentLine + 1) * DEFAULT_CONFIG.nodeHeightSpacing -
DEFAULT_CONFIG.radius,
}
);
}
if (root.right !== null) {
// recursively right node created
const rightNodeHorizontalConfig = { xStart: xPos, xEnd };
recursivelyDrawNotes(
root.right,
canvasElement,
currentLine + 1,
rightNodeHorizontalConfig
);
connectEdges(
canvasElement,
{
xStart: xPos,
xEnd: (xPos + xEnd) / 2,
},
{
yStart: yPos + DEFAULT_CONFIG.radius,
yEnd:
(currentLine + 1) * DEFAULT_CONFIG.nodeHeightSpacing -
DEFAULT_CONFIG.radius,
}
);
}
}
let preValue = "";
function init(value) {
preValue = value;
clearCanvas();
const root = treeConstructor(value);
drawBinaryTree(root, canvas);
}
function clearCanvas() {
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
}
const textarea = document.querySelector("textarea");
const applyBtn = document.querySelector(".applyBtn");
const clearBtn = document.querySelector(".clearBtn");
applyBtn.addEventListener("click", () => {
if (textarea.value === "") return;
init(textarea.value);
});
clearBtn.addEventListener("click", () => {
textarea.value = "";
clearCanvas();
});
window.addEventListener("resize", () => {
init(preValue);
});