-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_tree.js
More file actions
168 lines (155 loc) · 3.44 KB
/
Copy pathbinary_tree.js
File metadata and controls
168 lines (155 loc) · 3.44 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
let d = document;
let _ = (id) => d.getElementById(id);
_("bt").style.backgroundColor="teal";
let canvas = _("bt");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let cn = canvas.getContext("2d");
let randomColor = () => {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
class Node{
constructor(val, radius=20, font="20px Calibri", color=randomColor()){
this.left = null;
this.right = null;
this.fromLeft = 0;
this.fromTop = 0;
this.radius = radius;
this.val = val;
this.font = font;
this.color = color
}
}
class BinaryTree{
constructor(){
this.root = null;
}
width(){
let queue = [];
let width = 0;
let current = this.root;
queue.push(current);
while(queue.length){
let count = queue.length;
width = Math.max(count, width);
while(count--){
let node = queue.shift();
if(node.left){
queue.push(node.left);
}
if(node.right){
queue.push(node.right);
}
}
}
return width;
}
height(){
let root = this.root;
function traverse(node){
if(!node) return 0;
let hLeft = traverse(node.left);
let hRight = traverse(node.right);
return Math.max(hLeft + 1, hRight + 1);
}
return traverse(root);
}
insert(val){
let newNode = new Node(val);
if(!this.root){
this.root = newNode;
this.root.fromLeft = _("bt").offsetWidth/2;
this.root.fromTop = 50;
return this;
}else{
let node = this.root;
let widthSep = 200;
let heightSep = 50;
while(true){
if(newNode.val < node.val){
if(node.left === null){
newNode.fromLeft = node.fromLeft - widthSep;
newNode.fromTop = node.fromTop + heightSep;
node.left = newNode;
return this;
}else{
node = node.left;
}
}else if(newNode.val > node.val){
if(node.right === null){
newNode.fromLeft = node.fromLeft + widthSep;
newNode.fromTop = node.fromTop + heightSep;
node.right = newNode;
return this;
}else{
node = node.right;
}
}else{
return this;
}
widthSep = widthSep/2;
}
}
}
plot(){
let queue = [];
let current = this.root;
queue.push(current);
while(queue.length){
let node = queue.shift();
cn.beginPath();
cn.arc(node.fromLeft, node.fromTop, node.radius, 0, 2 * Math.PI);
cn.fillStyle = node.color;
cn.fill();
cn.lineWidth = 5;
cn.strokeStyle = '#003300';
cn.stroke();
cn.fillStyle = "white";
cn.textAlign = "center";
cn.font = node.font;
cn.fillText(node.val, node.fromLeft, node.fromTop + (node.radius)/4);
if(node.left){
cn.beginPath();
cn.moveTo(node.fromLeft - node.radius, node.fromTop);
cn.lineTo(node.left.fromLeft, node.left.fromTop - node.radius);
cn.stroke();
queue.push(node.left);
}
if(node.right){
cn.beginPath();
cn.moveTo(node.fromLeft + node.radius, node.fromTop);
cn.lineTo(node.right.fromLeft, node.right.fromTop - node.radius);
cn.stroke();
queue.push(node.right);
}
}
}
}
let tree = new BinaryTree();
tree.insert(10);
tree.insert(19);
tree.insert(8);
tree.insert(17);
tree.insert(6);
tree.insert(84);
tree.insert(9);
tree.insert(18);
tree.insert(15);
tree.insert(4);
tree.insert(5);
tree.insert(13);
tree.insert(2);
tree.insert(11);
tree.insert(19);
tree.insert(28);
tree.insert(37);
tree.insert(46);
tree.insert(55);
tree.insert(16);
tree.insert(1);
tree.plot();