-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
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"> | ||
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; |
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 => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
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) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The callback parameter in |
||
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); | ||
|
||
|
||
|
||
|
||
|
There was a problem hiding this comment.
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 thehref
attributes to point to the correct paths under/dsa
to ensure the links work correctly.