Skip to content

Commit 73dbfef

Browse files
committed
created draw tree, info cards, inserting into tree
1 parent 1ddf601 commit 73dbfef

File tree

7 files changed

+189
-73
lines changed

7 files changed

+189
-73
lines changed

src/components/BST.astro

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
---
2-
import BSTNode from './BST_node.astro';
32
---
43

54
<div class="tree">
@@ -11,8 +10,8 @@ import BSTNode from './BST_node.astro';
1110
.tree {
1211
display: flex;
1312
flex-direction: column;
14-
gap: 50px;
15-
height: 100vh;
13+
gap: 20px;
14+
height: 30vh;
1615
}
1716
div div {
1817
display: flex;

src/components/BST_node.astro

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/components/button.astro

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
interface Props {
3+
href?: string;
4+
target?: string;
5+
}
6+
7+
const {href, target } = Astro.props;
8+
---
9+
<a href={href} target={target}>
10+
<slot />
11+
</a>
12+
<style>
13+
a {
14+
color: black;
15+
font-weight: bold;
16+
background-color: var(--secondary);
17+
border-radius: var(--border-radius);
18+
width: 30%;
19+
padding: 10px;
20+
margin-top: 30px;
21+
text-align: center;
22+
text-decoration: none;
23+
display: inline-block;
24+
transition-duration: 0.4s;
25+
transition-timing-function: ease-in-out;
26+
}
27+
28+
a:hover {
29+
background: linear-gradient(90deg, var(--primary), var(--secondary));
30+
cursor: pointer;
31+
}
32+
</style>

src/components/infopanel.astro

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<div>
2+
<slot>
3+
4+
</slot>
5+
</div>
6+
<style>
7+
div {
8+
display: flex;
9+
flex-direction: column;
10+
background-color: var(--background-secondary);
11+
padding: 20px;
12+
border-radius: var(--border-radius);
13+
border: 1px solid var(--secondary);
14+
}
15+
</style>

src/layouts/Layout.astro

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@ const { title } = Astro.props;
2323
</html>
2424
<style is:global>
2525
:root {
26-
--background-secondary: #000000ff;
26+
--background-secondary: rgb(32, 54, 100);
2727
--background: #14213dff;
2828
--secondary: #fca311ff;
2929
--primary: #e5e5e5ff;
3030
--white: #ffffffff;
31+
--border-radius: 20px;
3132
}
3233
* {
3334
color: var(--primary);
35+
margin: 0;
36+
padding: 0;
3437
}
3538
html {
3639
font-family: system-ui, sans-serif;
@@ -50,9 +53,9 @@ const { title } = Astro.props;
5053
}
5154
h1, h2, h3, h4 {
5255
text-transform: uppercase;
53-
line-height: 0;
56+
/* line-height: 0; */
5457
text-align: left;
55-
margin-bottom: 1em;
58+
/* margin-bottom: 1em; */
5659
}
5760
h2, h3, h4 {
5861
color: var(--secondary);

src/pages/index.astro

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
11
---
22
import Layout from '../layouts/Layout.astro';
33
import BST from '../components/BST.astro';
4+
import InfoPanel from '../components/infopanel.astro';
5+
import Button from '../components/button.astro';
46
---
57

68
<Layout title="Welcome to Astro.">
79
<main>
810
<h1>Binary Search Tree</h1>
911
<h2>Visualizer</h2>
1012
<BST />
13+
<InfoPanel>
14+
<h2>Inserting into a Binary Tree</h2>
15+
<form id="insert-form" onsubmit="return false;" method="post" name="myForm">
16+
<label for="fname">Value you want to add: </label>
17+
<input id="insert-form-input" type="text" name="fname"><br><br>
18+
<input type="submit" value="Submit">
19+
</form>
20+
<Button>Insert</Button>
21+
</InfoPanel>
22+
<InfoPanel>
23+
<h2>PreOrder Representation</h2>
24+
<code id="preorder-list"></code>
25+
<Button>Preorder Traversals</Button>
26+
</InfoPanel>
27+
<InfoPanel>
28+
<h2>InOrder Representation</h2>
29+
<code id="inorder-list"></code>
30+
</InfoPanel>
31+
<InfoPanel>
32+
<h2>PostOrder Representation</h2>
33+
<code id="postorder-list"></code>
34+
</InfoPanel>
1135
</main>
1236
</Layout>
1337

@@ -21,4 +45,4 @@ import BST from '../components/BST.astro';
2145
font-size: 20px;
2246
line-height: 1.6;
2347
}
24-
</style>
48+
</style>

src/typescript/BST.ts

Lines changed: 109 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,23 @@ const canvas = document.getElementById("main-canvas") as HTMLCanvasElement;
5959
}
6060
}
6161

62-
function drawNode(ctx: CanvasRenderingContext2D, position: Vector2D) {
62+
function drawNode(ctx: CanvasRenderingContext2D, rootValue: number, position: Vector2D) {
63+
ctx.fillStyle = "#000000ff";
64+
ctx.textAlign = "center";
65+
ctx.textBaseline = "middle";
66+
ctx.font = "20px system-ui, sans-serif"
67+
ctx.fillText(rootValue.toString(), position.x, position.y);
68+
69+
ctx.fillStyle = "white";
70+
6371
ctx.beginPath();
64-
ctx.arc(position.x, position.y, 10, 0, Math.PI * 2, true);
72+
ctx.arc(position.x, position.y, 20, 0, Math.PI * 2, true);
6573

6674
ctx.fill();
6775
}
6876
function connectPoints(ctx: CanvasRenderingContext2D, position1: Vector2D, position2: Vector2D) {
77+
ctx.lineWidth = 5;
78+
6979
ctx.beginPath();
7080
ctx.moveTo(position1.x, position1.y);
7181
ctx.lineTo(position2.x, position2.y);
@@ -135,53 +145,103 @@ const canvas = document.getElementById("main-canvas") as HTMLCanvasElement;
135145

136146
return result
137147
}
148+
function insert(tree: BinaryTree, value: number) {
149+
if (value > tree.getRoot()) {
150+
if (tree.getLeft() === null && tree.getRight() === null) {
151+
tree.insertRight(value);
152+
} else {
153+
insert(tree.getRight(), value)
154+
}
155+
} else {
156+
if (tree.getLeft() === null && tree.getRight() === null) {
157+
tree.insertLeft(value);
158+
} else {
159+
insert(tree.getLeft(), value)
160+
}
161+
}
162+
}
163+
function drawTree(ctx: CanvasRenderingContext2D, tree: BinaryTree, canvasWidth: number, scale: number, i=0, rootPosition: any=null) {
164+
if (tree) {
165+
if (rootPosition === null) {
166+
rootPosition = new Vector2D(canvasWidth / 2, 50);
167+
}
168+
169+
drawNode(ctx, tree.getRoot(), rootPosition);
170+
171+
if (tree.getLeft()) {
172+
let leftPosition = new Vector2D(rootPosition.x - (100 / (i + 1)) * scale, rootPosition.y + 50);
173+
174+
drawNode(ctx, tree.getLeft().getRoot(), leftPosition);
175+
connectPoints(ctx, rootPosition, leftPosition);
176+
drawTree(ctx, tree.getLeft(), canvasWidth, scale, i + 1, leftPosition);
177+
}
178+
if (tree.getRight()) {
179+
let rightPosition = new Vector2D(rootPosition.x + (100 / (i + 1)) * scale, rootPosition.y + 50);
180+
181+
drawNode(ctx, tree.getRight().getRoot(), rightPosition);
182+
connectPoints(ctx, rootPosition, rightPosition);
183+
drawTree(ctx, tree.getRight(), canvasWidth, scale, i + 1, rightPosition);
184+
}
185+
}
186+
}
187+
function updateTraversals(tree: BinaryTree) {
188+
var preOrderElement = document.getElementById('preorder-list') as HTMLElement;
189+
preOrderElement.innerText = preOrder(tree).toString();
190+
191+
var inOrderElement = document.getElementById('inorder-list') as HTMLElement;
192+
inOrderElement.innerText = inOrder(tree).toString();
193+
194+
var postOrderElement = document.getElementById('postorder-list') as HTMLElement;
195+
postOrderElement.innerText = postOrder(tree).toString();
196+
}
138197

139-
if (canvas.getContext) {
140-
const ctx = canvas.getContext("2d") as CanvasRenderingContext2D;
198+
function buildCanvas() {
199+
if (canvas.getContext) {
200+
const ctx = canvas.getContext("2d") as CanvasRenderingContext2D;
201+
202+
var scale = 1;
203+
canvas.style.width = '50vw';
204+
canvas.style.height = '50vh';
205+
canvas.width = canvas.offsetWidth;
206+
canvas.height = canvas.offsetHeight;
207+
208+
ctx.globalCompositeOperation='destination-over';
209+
210+
ctx.fillStyle = "white";
211+
ctx.strokeStyle = "#fca311ff";
212+
213+
var tree = new BinaryTree(1);
214+
tree.insertLeft(2);
215+
tree.insertRight(7);
216+
tree.getLeft().insertLeft(3);
217+
tree.getLeft().insertRight(6);
218+
tree.getLeft().getLeft().insertLeft(4);
219+
tree.getLeft().getLeft().insertRight(5);
220+
tree.getRight().insertLeft(8);
221+
tree.getRight().insertRight(9);
222+
223+
updateTraversals(tree);
224+
225+
console.log("MIN", minRoot(tree));
226+
console.log("MAX", maxRoot(tree));
227+
228+
drawTree(ctx, tree, canvas.width, scale);
229+
230+
var number = document.getElementById('insert-form-input') as HTMLInputElement;
231+
var numberForm = document.getElementById('insert-form') as HTMLElement;
232+
233+
// if (numberForm.addEventListener){
234+
235+
// }
236+
numberForm.addEventListener("submit", function (){
237+
console.log(number.value);
238+
insert(tree, parseInt(number.value))
239+
drawTree(ctx, tree, canvas.width, scale);
240+
updateTraversals(tree);
241+
}, false);
242+
}
243+
}
141244

142-
var scale = 1;
143-
canvas.style.width = '50vw';
144-
canvas.style.height = '50vh';
145-
canvas.width = canvas.offsetWidth;
146-
canvas.height = canvas.offsetHeight;
245+
buildCanvas();
147246

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-
}
247+
window.addEventListener("resize", buildCanvas);

0 commit comments

Comments
 (0)