-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
123 lines (118 loc) · 3.85 KB
/
main.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
import { BinaryTreeNode, drawBinaryTree, VisualizationType, setTheme } from 'binary-tree-visualizer';
let opt = {
textFont:20,
strokeColor:'#3772de',
colorArray: [{
borderColor: '#0080ff',
bgColor: "#fff"
}]
};
setTheme(opt);
const insertBtn = document.querySelector("button");
insertBtn.addEventListener("click", ()=>{
if(root.value==" "){
root = new BinaryTreeNode(parseInt(document.getElementById("element").value));
draw();
}
else{
insert(root,parseInt(document.getElementById("element").value));
draw();
}
})
function insert(node, value){
if(node.value==value)
return;
if(value<node.value){
if(node.left!=undefined){
insert(node.left,value);
return;
}
node.setLeft(new BinaryTreeNode(value));
return;
}
if(node.right!=undefined){
insert(node.right,value);
return;
}
node.setRight(new BinaryTreeNode(value));
}
let preorderArray = [];
function preorder(node, temp){
if(node!=undefined){
preorderArray.push(node.value);
preorder(node.left,1);
preorder(node.right,0);
}
}
let inorderArray = [];
function inorder(node,temp){
if(node!=undefined){
inorder(node.left,1);
inorderArray.push(node.value);
inorder(node.right,0);
}
}
let postorderArray = [] ;
function postorder(node, temp){
if(node!=undefined){
postorder(node.left,1);
postorder(node.right,0);
postorderArray.push(node.value);
}
}
const preorderBtn = document.querySelectorAll("button")[1];
const inorderBtn = document.querySelectorAll("button")[2];
const postorderBtn = document.querySelectorAll("button")[3];
preorderBtn.addEventListener("click",()=>{
preorderArray = [];
preorder(root,-1);
const element = document.getElementById("output_1");
if(element!=undefined)
element.remove();
const output_1 = document.createElement("h2");
output_1.setAttribute("id","output_1");
let textnode = document.createTextNode("Preorder: [");
output_1.appendChild(textnode);
textnode = document.createTextNode(preorderArray);
output_1.appendChild(textnode);
textnode = document.createTextNode("]");
output_1.appendChild(textnode);
document.getElementById("output").appendChild(output_1);
})
inorderBtn.addEventListener("click",()=>{
inorderArray = [];
inorder(root,-1);
const element = document.getElementById("output_1");
if(element!=undefined)
element.remove();
const output_1 = document.createElement("h2");
output_1.setAttribute("id","output_1");
let textnode = document.createTextNode("Inorder: [");
output_1.appendChild(textnode);
textnode = document.createTextNode(inorderArray);
output_1.appendChild(textnode);
textnode = document.createTextNode("]");
output_1.appendChild(textnode);
document.getElementById("output").appendChild(output_1);
})
postorderBtn.addEventListener("click",()=>{
postorderArray = [];
postorder(root,-1);
const element = document.getElementById("output_1");
if(element!=undefined)
element.remove();
const output_1 = document.createElement("h2");
output_1.setAttribute("id","output_1");
let textnode = document.createTextNode("Postorder: [");
output_1.appendChild(textnode);
textnode = document.createTextNode(postorderArray);
output_1.appendChild(textnode);
textnode = document.createTextNode("]");
output_1.appendChild(textnode);
document.getElementById("output").appendChild(output_1);
})
function draw(){
drawBinaryTree(root,document.querySelector("canvas"),{type:VisualizationType.HIGHLIGHT});
}
let root = new BinaryTreeNode(" ");
draw();