Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

insert and read tree #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 0 additions & 31 deletions src/app/algorithms/page.tsx

This file was deleted.

File renamed without changes.
42 changes: 42 additions & 0 deletions src/app/dsa/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Link from "next/link";
import BubbleSort from "./bubblesort/page";

const Algorithms = () => {
return (
<>
<div className="h-full w-full flex flex-col gap-3 justify-center items-center">
<div className="text-2xl font-bold">

The Parth Structures and Algorithms
</div>

<div className="flex flex-col gap-2 text-md items-center">
<div className="italic">
Algorithms
</div>
<div className="flex flex-col underline items-center">

<Link href="/algorithms/bubblesort">
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The links in this component still point to the old /algorithms path. Update the href attributes to point to the correct paths under /dsa to ensure the links work correctly.

Suggested change
<Link href="/algorithms/bubblesort">
<Link href="/dsa/bubblesort">

Bubble sort
</Link>
<Link href="/algorithms/insertionsort">
Insertion sort
</Link>
<Link href="/algorithms/quicksort">
Quick sort (WIP)
</Link>

</div>

<div className="italic">
Data Structures
</div>
</div>
</div>
</>

)

}

export default Algorithms;
File renamed without changes.
71 changes: 71 additions & 0 deletions src/app/dsa/tree/tree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { root } from "postcss"

type Node = {
value: number,
leftNode?: Node,
rightNode?: Node,
}

// type CreateProps = {
// root?: Node
// value: number
// leftNode: Node
// rightNode: Node
// }

// takes a value and a tree (root) and returns a new tree (root)
const insertNode = (value: number, root: Node): Node => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The insertNode function does not handle the case where the inserted value is equal to the current node's value. Consider adding logic to handle this scenario to prevent potential issues.

if (value < root.value) {
if (root.leftNode) insertNode(value, root.leftNode)
else root.leftNode = { value }
}
else {
if (root.rightNode) insertNode(value, root.rightNode)
else root.rightNode = { value }
}

return root


}
const readTree = (root: Node, callback: Function) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The callback parameter in readTree is typed as Function, which is not type-safe. Consider defining a more specific function type for better type safety and clarity.

callback('parent:', root.value)
if (root.leftNode) callback('left: ', root.leftNode.value);
if (root.rightNode) callback('right: ', root.rightNode.value);
if (root.leftNode) readTree(root.leftNode, callback);
if (root.rightNode) readTree(root.rightNode, callback);
}





const startArr = [1, 2, 5, 6, 8, 3, 9, 4, 7, 10]

const startNode: Node = {
value: 5,
leftNode: {
value: 3,
leftNode: {
value: 2
},
// rightNode: {
// value: 4
// }
},
rightNode: {
value: 7,
leftNode: {
value: 6,
}
}

}

insertNode(4, startNode);
readTree(startNode, console.log);





2 changes: 1 addition & 1 deletion src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { redirect } from "next/navigation";

const Home = () => {
redirect('/algorithms')
redirect('/dsa')

}

Expand Down