-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreeUtils.js
114 lines (91 loc) · 3.01 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
import { BinaryTreeNode } from "./BinaryTreeNode.js";
export const DEFAULT_CONFIG = {
radius:20,
nodeWidthSpacing:50,
nodeHeightSpacing:90,
fontSize:10
}
export const getRequiredHeightAndWidth=(root)=>{
const heightOfTree = root.getHeight();
const maxLeafNodes = Math.pow(2, heightOfTree);
const requiredCanvasHeight = heightOfTree * DEFAULT_CONFIG.nodeHeightSpacing;
const requiredCanvasWidth = maxLeafNodes * DEFAULT_CONFIG.nodeWidthSpacing;
return {requiredCanvasHeight, requiredCanvasWidth};
}
export const 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,Math.PI*2,false);
context.fillStyle = 'lightsalmon';
context.fill()
//Draw Circle border
context.beginPath();
context.arc(x,y,DEFAULT_CONFIG.radius,0,Math.PI*2,false);
context.strokeStyle = 'brown';
context.stroke()
// Write text
context.font = `${DEFAULT_CONFIG.fontSize}pt serif`;
context.fillStyle = 'brown';
context.textAlign = 'center';
context.fillText(value,x,y + DEFAULT_CONFIG.fontSize /2)
}
export const 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:xHalf};
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++]);
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(value){
let parsedInput = '';
for(let i=0; i< value.length;i++){
const ch = value.charAt(i);
if(ch !== ' ') parsedInput += ch;
}
return parsedInput.split(',')
.map(ele=>{
if(ele === 'null') return null;
else return ele;
})
}