-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawBinaryTree.js
153 lines (110 loc) · 4.47 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import {BinaryTreeNode} from './BinaryTreeNode.js';
import { DEFAULT_CONFIG, getRequiredHeightAndWidth,drawNode,connectEdges,treeConstructor } from './treeUtils.js';
import {inOrderTraversal, preOrderTraversal, postOrderTraversal} from './Algorithm.js';
const canvas = document.querySelector('canvas');
function drawBinaryTree(root, canvasElement){
const maxWidth = window.innerWidth;
const maxHeight = window.innerHeight;
// Initialize canvas dimensions
canvasElement.width = maxWidth;
canvasElement.height = maxHeight;
const {requiredCanvasHeight, requiredCanvasWidth} =getRequiredHeightAndWidth(root);
const windowWidthCenter = maxWidth/2;
const requiredWidthCenter = requiredCanvasWidth/2;
const xStart = windowWidthCenter - requiredWidthCenter;
const xEnd = windowWidthCenter + requiredWidthCenter;
const horizontalConfig = {xStart, xEnd};
recursivelyDrawNodes(root, canvasElement, 0.5, horizontalConfig)
}
function recursivelyDrawNodes(root, canvasElement, currentLine, horizontalConfig){
const {xStart, xEnd} = horizontalConfig;
const xPos = (xStart + xEnd)/2;
const yPos = currentLine * DEFAULT_CONFIG.nodeHeightSpacing;
drawNode(root.value, canvasElement, xPos, yPos);
if(root.left != null){
const leftNodeHorizontalConfig = {xStart, xEnd: xPos};
recursivelyDrawNodes(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){
const rightNodeHorizontalConfig = {xStart: xPos, xEnd};
recursivelyDrawNodes(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 prevValue = ''
function init(value){
prevValue = 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('.apply-btn');
const clearBtn = document.querySelector('.clear-btn');
const inOrderBtn = document.querySelector('.in-order-btn');
const preOrderBtn = document.querySelector('.pre-order-btn');
const postOrderBtn = document.querySelector('.post-order-btn');
applyBtn.addEventListener('click', ()=>{
if(textArea.value === '') return;
init(textArea.value)
})
clearBtn.addEventListener('click', () => {
textArea.value = '';
document.querySelector('.in-order-algo-result').innerHTML = "";
document.querySelector('.pre-order-algo-result').innerHTML = "";
document.querySelector('.post-order-algo-result').innerHTML = "";
clearCanvas()
})
inOrderBtn.addEventListener('click', ()=>{
if(textArea.value === '') return;
const inOrderTraversedArray = [];
const rootNode = treeConstructor(textArea.value);
const result = inOrderTraversal(rootNode,inOrderTraversedArray)
const inOrderDiv = document.querySelector('.in-order-algo-result')
inOrderDiv.innerHTML = result
})
preOrderBtn.addEventListener('click', ()=>{
if(textArea.value === '') return;
const preOrderTraversedArray = [];
const rootNode = treeConstructor(textArea.value);
const result = preOrderTraversal(rootNode, preOrderTraversedArray);
const preOrderDiv = document.querySelector('.pre-order-algo-result')
preOrderDiv.innerHTML = result
})
postOrderBtn.addEventListener('click', ()=>{
if(textArea.value === '') return;
const postOrderTraversedArray = [];
const rootNode = treeConstructor(textArea.value);
const result = postOrderTraversal(rootNode,postOrderTraversedArray)
const postOrderDiv = document.querySelector('.post-order-algo-result')
postOrderDiv.innerHTML = result
})
window.addEventListener('resize', () => {
init(prevValue)
if(textArea.value == ''){
clearCanvas()
}
})