Skip to content

Commit

Permalink
BST visual display
Browse files Browse the repository at this point in the history
  • Loading branch information
terrencejihoonjung committed Sep 14, 2024
1 parent f6eb523 commit 4e69b8a
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 12 deletions.
67 changes: 67 additions & 0 deletions src/components/DSPlaygrounds/BinarySearchTree/BinaryNode.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import React from "react";

Check failure on line 1 in src/components/DSPlaygrounds/BinarySearchTree/BinaryNode.tsx

View workflow job for this annotation

GitHub Actions / deploy

'React' is declared but its value is never read.
import { TreeNode } from "../../../entities";

type BinaryNodeProps = {
node: TreeNode;
x: number;
y: number;
level: number;
};

const VERTICAL_SPACING = 60;
const HORIZONTAL_SPACING = 40;

function BinaryNode({ node, x, y, level }: BinaryNodeProps) {
const leftChildX = x - HORIZONTAL_SPACING / (level + 1);
const rightChildX = x + HORIZONTAL_SPACING / (level + 1);
const childY = y + VERTICAL_SPACING;

return (
<g>
<circle cx={x} cy={y} r="20" fill="black" />
<text x={x} y={y} textAnchor="middle" dy=".3em" fill="white">
{node.value}
</text>

{node.left && (
<>
<line
x1={x}
y1={y + 20}
x2={leftChildX}
y2={childY - 20}
stroke="black"
strokeWidth="1"
/>
<BinaryNode
node={node.left}
x={leftChildX}
y={childY}
level={level + 1}
/>
</>
)}

{node.right && (
<>
<line
x1={x}
y1={y + 20}
x2={rightChildX}
y2={childY - 20}
stroke="black"
strokeWidth="1"
/>
<BinaryNode
node={node.right}
x={rightChildX}
y={childY}
level={level + 1}
/>
</>
)}
</g>
);
}

export default BinaryNode;
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { useState } from "react";
import { PlaygroundProps } from "../../entities";
import { PlaygroundProps, TreeNode } from "../../../entities";
import { Link } from "react-router-dom";
import PencilIcon from "../Icons/PencilIcon";
import map from "../../data/data-structures";

interface TreeNode {
value: number;
left: TreeNode | null;
right: TreeNode | null;
}
import PencilIcon from "../../Icons/PencilIcon";
import map from "../../../data/data-structures";
import BinaryNode from "./BinaryNode";

function BinarySearchTreePG({ className }: PlaygroundProps) {
const ds = map.get("binary-search-tree")!;
Expand Down Expand Up @@ -122,8 +117,12 @@ function BinarySearchTreePG({ className }: PlaygroundProps) {
</div>

{/* Visualization Window - To be implemented */}
<div className="relative p-4 grow w-full border border-black rounded-md">
{/* Tree visualization will go here */}
<div className="relative p-4 grow w-full border border-black rounded-md overflow-auto">
<svg width="100%" height="100%" viewBox="0 0 800 600">
<g transform="translate(400, 40)">
{root && <BinaryNode node={root} x={0} y={0} level={0} />}
</g>
</svg>
</div>

{/* Error Messages */}
Expand Down
8 changes: 8 additions & 0 deletions src/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ export type DynamicArrayState = {
export type PlaygroundProps = {
className?: string;
};

export type TreeNode = {
value: number;
left: TreeNode | null;
right: TreeNode | null;
x?: number;
y?: number;
};
2 changes: 1 addition & 1 deletion src/pages/BinarySearchTree.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import map from "../data/data-structures";
import Heading from "../components/Heading";
import BinarySearchTreePG from "../components/DSPlaygrounds/BinarySearchTreePG";
import BinarySearchTreePG from "../components/DSPlaygrounds/BinarySearchTree/BinarySearchTreePG";

function BinarySearchTree() {
const binarySearchTree = map.get("binary-search-tree")!;
Expand Down

0 comments on commit 4e69b8a

Please sign in to comment.