Skip to content

Commit 1ddf601

Browse files
committed
moved script to separate file
1 parent 225aab8 commit 1ddf601

File tree

3 files changed

+189
-190
lines changed

3 files changed

+189
-190
lines changed

src/components/BST.astro

Lines changed: 1 addition & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -22,193 +22,4 @@ import BSTNode from './BST_node.astro';
2222
canvas {
2323
height: 100vh;
2424
}
25-
</style>
26-
<script>
27-
const canvas = document.getElementById("main-canvas") as HTMLCanvasElement;
28-
29-
class Vector2D {
30-
public x: number;
31-
public y: number;
32-
constructor (x: number, y: number) {
33-
this.x = x;
34-
this.y = y;
35-
}
36-
}
37-
38-
class BinaryTree {
39-
private root: number;
40-
private left: any;
41-
private right: any;
42-
constructor (root: number) {
43-
this.root = root;
44-
this.left = null;
45-
this.right = null;
46-
}
47-
48-
getLeft() {
49-
return this.left;
50-
}
51-
getRight() {
52-
return this.right;
53-
}
54-
getRoot() {
55-
return this.root;
56-
}
57-
58-
setLeft(node: BinaryTree) {
59-
this.left = node;
60-
}
61-
setRight(node: BinaryTree) {
62-
this.right = node;
63-
}
64-
setRoot(num: number) {
65-
this.root = num;
66-
}
67-
68-
insertLeft(num: number) {
69-
if (this.left === null) {
70-
this.left = new BinaryTree(num);
71-
} else {
72-
const tree = new BinaryTree(num);
73-
tree.left = this.left;
74-
this.left = tree;
75-
}
76-
}
77-
insertRight(num: number) {
78-
if (this.right === null) {
79-
this.right = new BinaryTree(num);
80-
} else {
81-
const tree = new BinaryTree(num);
82-
tree.right = this.right;
83-
this.right = tree;
84-
}
85-
}
86-
}
87-
88-
function drawNode(ctx: CanvasRenderingContext2D, position: Vector2D) {
89-
ctx.beginPath();
90-
ctx.arc(position.x, position.y, 10, 0, Math.PI * 2, true);
91-
92-
ctx.fill();
93-
}
94-
function connectPoints(ctx: CanvasRenderingContext2D, position1: Vector2D, position2: Vector2D) {
95-
ctx.beginPath();
96-
ctx.moveTo(position1.x, position1.y);
97-
ctx.lineTo(position2.x, position2.y);
98-
ctx.stroke();
99-
100-
ctx.beginPath();
101-
ctx.moveTo(position2.x, position2.y);
102-
ctx.lineTo(position1.x, position1.y);
103-
ctx.stroke();
104-
}
105-
function preOrder(tree: any): any[number] {
106-
let result = [];
107-
result.push(tree.getRoot());
108-
if (tree.getLeft()) {
109-
result = result.concat(preOrder(tree.getLeft()));
110-
}
111-
if (tree.getRight()) {
112-
result = result.concat(preOrder(tree.getRight()));
113-
}
114-
return result;
115-
}
116-
function inOrder(tree: any): any[number] {
117-
let result: any[number] = [];
118-
if (tree.getLeft()) {
119-
result = result.concat(inOrder(tree.getLeft()));
120-
}
121-
result.push(tree.getRoot());
122-
if (tree.getRight()) {
123-
result = result.concat(inOrder(tree.getRight()));
124-
}
125-
return result;
126-
}
127-
function postOrder(tree: any): any[number] {
128-
let result: any[number] = [];
129-
if (tree.getLeft()) {
130-
result = result.concat(postOrder(tree.getLeft()));
131-
}
132-
if (tree.getRight()) {
133-
result = result.concat(postOrder(tree.getRight()));
134-
}
135-
result.push(tree.getRoot());
136-
return result;
137-
}
138-
function minRoot(tree: BinaryTree): number {
139-
let result: number = Infinity;
140-
if (tree) {
141-
result = tree.getRoot();
142-
143-
let left_result = minRoot(tree.getLeft());
144-
let right_result = minRoot(tree.getRight());
145-
146-
result = Math.min(result, left_result, right_result);
147-
}
148-
149-
return result
150-
}
151-
function maxRoot(tree: BinaryTree): number {
152-
let result: number = -Infinity;
153-
if (tree) {
154-
result = tree.getRoot();
155-
156-
let left_result = maxRoot(tree.getLeft());
157-
let right_result = maxRoot(tree.getRight());
158-
159-
result = Math.max(result, left_result, right_result);
160-
}
161-
162-
return result
163-
}
164-
165-
if (canvas.getContext) {
166-
const ctx = canvas.getContext("2d") as CanvasRenderingContext2D;
167-
168-
var scale = 1;
169-
canvas.style.width = '50vw';
170-
canvas.style.height = '50vh';
171-
canvas.width = canvas.offsetWidth;
172-
canvas.height = canvas.offsetHeight;
173-
174-
ctx.fillStyle = "white";
175-
ctx.strokeStyle = "#fca311ff";
176-
177-
// const points = [
178-
// [canvas.offsetWidth / 2, 10],
179-
// [canvas.offsetWidth / 4, 100],
180-
// [canvas.offsetWidth / 1.5, 100],
181-
// [canvas.offsetWidth / 8, 200],
182-
// [canvas.offsetWidth / 2.5, 200],
183-
// [canvas.offsetWidth / 1.2, 200],
184-
// [canvas.offsetWidth / 16, 300],
185-
// [canvas.offsetWidth / 4.5, 300],
186-
// ];
187-
188-
// points.forEach((point) => {
189-
// drawLine(ctx, point[0], point[1]);
190-
// });
191-
192-
// points.forEach((point) => {
193-
// drawNode(ctx, point[0], point[1]);
194-
// });
195-
196-
var tree = new BinaryTree(1)
197-
tree.insertLeft(2)
198-
tree.insertRight(7)
199-
tree.getLeft().insertLeft(3)
200-
tree.getLeft().insertRight(6)
201-
tree.getLeft().getLeft().insertLeft(4)
202-
tree.getLeft().getLeft().insertRight(5)
203-
tree.getRight().insertLeft(8)
204-
tree.getRight().insertRight(9)
205-
206-
let preOrderTree = postOrder(tree);
207-
for (let i = 0; i < preOrderTree.length; i++) {
208-
console.log(preOrderTree[i]);
209-
}
210-
211-
console.log("MIN", minRoot(tree));
212-
console.log("MAX", maxRoot(tree));
213-
}
214-
</script>
25+
</style>

src/layouts/Layout.astro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const { title } = Astro.props;
1515
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
1616
<meta name="generator" content={Astro.generator} />
1717
<title>{title}</title>
18+
<script src="../typescript/BST.ts"></script>
1819
</head>
1920
<body>
2021
<slot />

src/typescript/BST.ts

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
const canvas = document.getElementById("main-canvas") as HTMLCanvasElement;
2+
3+
class Vector2D {
4+
public x: number;
5+
public y: number;
6+
constructor (x: number, y: number) {
7+
this.x = x;
8+
this.y = y;
9+
}
10+
}
11+
12+
class BinaryTree {
13+
private root: number;
14+
private left: any;
15+
private right: any;
16+
constructor (root: number) {
17+
this.root = root;
18+
this.left = null;
19+
this.right = null;
20+
}
21+
22+
getLeft() {
23+
return this.left;
24+
}
25+
getRight() {
26+
return this.right;
27+
}
28+
getRoot() {
29+
return this.root;
30+
}
31+
32+
setLeft(node: BinaryTree) {
33+
this.left = node;
34+
}
35+
setRight(node: BinaryTree) {
36+
this.right = node;
37+
}
38+
setRoot(num: number) {
39+
this.root = num;
40+
}
41+
42+
insertLeft(num: number) {
43+
if (this.left === null) {
44+
this.left = new BinaryTree(num);
45+
} else {
46+
const tree = new BinaryTree(num);
47+
tree.left = this.left;
48+
this.left = tree;
49+
}
50+
}
51+
insertRight(num: number) {
52+
if (this.right === null) {
53+
this.right = new BinaryTree(num);
54+
} else {
55+
const tree = new BinaryTree(num);
56+
tree.right = this.right;
57+
this.right = tree;
58+
}
59+
}
60+
}
61+
62+
function drawNode(ctx: CanvasRenderingContext2D, position: Vector2D) {
63+
ctx.beginPath();
64+
ctx.arc(position.x, position.y, 10, 0, Math.PI * 2, true);
65+
66+
ctx.fill();
67+
}
68+
function connectPoints(ctx: CanvasRenderingContext2D, position1: Vector2D, position2: Vector2D) {
69+
ctx.beginPath();
70+
ctx.moveTo(position1.x, position1.y);
71+
ctx.lineTo(position2.x, position2.y);
72+
ctx.stroke();
73+
74+
ctx.beginPath();
75+
ctx.moveTo(position2.x, position2.y);
76+
ctx.lineTo(position1.x, position1.y);
77+
ctx.stroke();
78+
}
79+
function preOrder(tree: any): any[number] {
80+
let result = [];
81+
result.push(tree.getRoot());
82+
if (tree.getLeft()) {
83+
result = result.concat(preOrder(tree.getLeft()));
84+
}
85+
if (tree.getRight()) {
86+
result = result.concat(preOrder(tree.getRight()));
87+
}
88+
return result;
89+
}
90+
function inOrder(tree: any): any[number] {
91+
let result: any[number] = [];
92+
if (tree.getLeft()) {
93+
result = result.concat(inOrder(tree.getLeft()));
94+
}
95+
result.push(tree.getRoot());
96+
if (tree.getRight()) {
97+
result = result.concat(inOrder(tree.getRight()));
98+
}
99+
return result;
100+
}
101+
function postOrder(tree: any): any[number] {
102+
let result: any[number] = [];
103+
if (tree.getLeft()) {
104+
result = result.concat(postOrder(tree.getLeft()));
105+
}
106+
if (tree.getRight()) {
107+
result = result.concat(postOrder(tree.getRight()));
108+
}
109+
result.push(tree.getRoot());
110+
return result;
111+
}
112+
function minRoot(tree: BinaryTree): number {
113+
let result: number = Infinity;
114+
if (tree) {
115+
result = tree.getRoot();
116+
117+
let left_result = minRoot(tree.getLeft());
118+
let right_result = minRoot(tree.getRight());
119+
120+
result = Math.min(result, left_result, right_result);
121+
}
122+
123+
return result
124+
}
125+
function maxRoot(tree: BinaryTree): number {
126+
let result: number = -Infinity;
127+
if (tree) {
128+
result = tree.getRoot();
129+
130+
let left_result = maxRoot(tree.getLeft());
131+
let right_result = maxRoot(tree.getRight());
132+
133+
result = Math.max(result, left_result, right_result);
134+
}
135+
136+
return result
137+
}
138+
139+
if (canvas.getContext) {
140+
const ctx = canvas.getContext("2d") as CanvasRenderingContext2D;
141+
142+
var scale = 1;
143+
canvas.style.width = '50vw';
144+
canvas.style.height = '50vh';
145+
canvas.width = canvas.offsetWidth;
146+
canvas.height = canvas.offsetHeight;
147+
148+
ctx.fillStyle = "white";
149+
ctx.strokeStyle = "#fca311ff";
150+
151+
// const points = [
152+
// [canvas.offsetWidth / 2, 10],
153+
// [canvas.offsetWidth / 4, 100],
154+
// [canvas.offsetWidth / 1.5, 100],
155+
// [canvas.offsetWidth / 8, 200],
156+
// [canvas.offsetWidth / 2.5, 200],
157+
// [canvas.offsetWidth / 1.2, 200],
158+
// [canvas.offsetWidth / 16, 300],
159+
// [canvas.offsetWidth / 4.5, 300],
160+
// ];
161+
162+
// points.forEach((point) => {
163+
// drawLine(ctx, point[0], point[1]);
164+
// });
165+
166+
// points.forEach((point) => {
167+
// drawNode(ctx, point[0], point[1]);
168+
// });
169+
170+
var tree = new BinaryTree(1)
171+
tree.insertLeft(2)
172+
tree.insertRight(7)
173+
tree.getLeft().insertLeft(3)
174+
tree.getLeft().insertRight(6)
175+
tree.getLeft().getLeft().insertLeft(4)
176+
tree.getLeft().getLeft().insertRight(5)
177+
tree.getRight().insertLeft(8)
178+
tree.getRight().insertRight(9)
179+
180+
let preOrderTree = postOrder(tree);
181+
for (let i = 0; i < preOrderTree.length; i++) {
182+
console.log(preOrderTree[i]);
183+
}
184+
185+
console.log("MIN", minRoot(tree));
186+
console.log("MAX", maxRoot(tree));
187+
}

0 commit comments

Comments
 (0)