-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreeutils.js
133 lines (102 loc) · 3.11 KB
/
treeutils.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
import { BinaryTreeNode } from "./BinaryTreeNode.js";
export const DEFAULT_CONFIG = {
radius: 20,
nodeWidthSpacing: 25, // width spacing
nodeHeightSpacing: 100, // line height
nodeFontSize: 10, // font size
};
export function getRequiredHeightAndWidth(root) {
const heightOfTree = root.getHeight(); // calculate the height of canvas
const maxLeafNodes = Math.pow(2, heightOfTree); // calculate the width of canvas
const requiredCanvasHeight = heightOfTree * DEFAULT_CONFIG.nodeHeightSpacing; // calculate the height of canvas
const requiredCanvasWidth = maxLeafNodes * DEFAULT_CONFIG.nodeWidthSpacing; // calculate the width of canvas
return {
requiredCanvasHeight,
requiredCanvasWidth,
};
}
export function drawNode(value, canvasElement, x, y) {
const context = canvasElement.getContext("2d"); // tool to draw
// draw circle
context.beginPath();
context.arc(x, y, DEFAULT_CONFIG.radius, 0, 2 * Math.PI, false);
context.fillStyle = "lightsalmon";
context.fill();
// draw circle border
context.beginPath();
context.arc(x, y, DEFAULT_CONFIG.radius, 0, 2 * Math.PI, false);
context.strokeStyle = "brown";
context.stroke();
// write value
context.font = `${DEFAULT_CONFIG.nodeFontSize}pt serif`;
context.fillStyle = "brown";
context.textAlign = "center";
context.fillText(value, x, y + DEFAULT_CONFIG.nodeFontSize / 2);
}
// Connecting edges of nodes
export function connectEdges(canvasElement, xCoordinates, yCoordinates) {
const { xStart, xEnd } = xCoordinates;
const { yStart, yEnd } = yCoordinates;
const xHalf = (xStart + xEnd) / 2;
const yHalf = (yStart + yEnd) / 2;
const start = { x: xStart, y: yStart };
const cPoint1 = { x: xHalf, y: yHalf };
const cPoint2 = { x: xEnd, y: yHalf };
const end = { x: xEnd, y: yEnd };
// draw curve
const context = canvasElement.getContext("2d");
context.beginPath();
context.strokeStyle = "brown";
context.moveTo(start.x, start.y);
context.bezierCurveTo(
cPoint1.x,
cPoint1.y,
cPoint2.x,
cPoint2.y,
end.x,
end.y
);
// context.lineTo(end.x, end.y);
context.stroke();
}
export function treeConstructor(input) {
input = parseInput(input);
const queue = [];
let idx = 0;
const root = new BinaryTreeNode(input[idx]);
idx++;
queue.push(root);
while (queue.length > 0 && idx < input.length) {
const node = queue.shift();
// Left child
if (idx < input.length) {
if (input[idx] !== null) {
const leftNode = new BinaryTreeNode(input[idx]);
node.setLeft(leftNode);
queue.push(leftNode);
}
idx++;
}
// Right Child
if (idx < input.length) {
if (input[idx] !== null) {
const rightNode = new BinaryTreeNode(input[idx]);
node.setRight(rightNode);
queue.push(rightNode);
}
idx++;
}
}
return root;
}
function parseInput(input) {
let parseInput = "";
for (let i = 0; i < input.length; i++) {
const ch = input.charAt(i);
if (ch !== "") parseInput += ch;
}
return parseInput.split(",").map((elem) => {
if (elem === "null") return null;
else return elem;
});
}