diff --git a/src/app/algorithms/page.tsx b/src/app/algorithms/page.tsx deleted file mode 100644 index 623aad5..0000000 --- a/src/app/algorithms/page.tsx +++ /dev/null @@ -1,31 +0,0 @@ -import Link from "next/link"; -import BubbleSort from "./bubblesort/page"; - -const Algorithms = () => { - return ( - <> -
-
- - The Parth Algorithms -
-
- - - Bubble sort - - - Insertion sort - - - Quick sort (WIP) - -
-
- - - ) - -} - -export default Algorithms; \ No newline at end of file diff --git a/src/app/algorithms/bubblesort/bubbleSort.ts b/src/app/dsa/bubblesort/bubbleSort.ts similarity index 100% rename from src/app/algorithms/bubblesort/bubbleSort.ts rename to src/app/dsa/bubblesort/bubbleSort.ts diff --git a/src/app/algorithms/bubblesort/page.tsx b/src/app/dsa/bubblesort/page.tsx similarity index 100% rename from src/app/algorithms/bubblesort/page.tsx rename to src/app/dsa/bubblesort/page.tsx diff --git a/src/app/algorithms/insertionsort/insertionSort.ts b/src/app/dsa/insertionsort/insertionSort.ts similarity index 100% rename from src/app/algorithms/insertionsort/insertionSort.ts rename to src/app/dsa/insertionsort/insertionSort.ts diff --git a/src/app/algorithms/insertionsort/page.tsx b/src/app/dsa/insertionsort/page.tsx similarity index 100% rename from src/app/algorithms/insertionsort/page.tsx rename to src/app/dsa/insertionsort/page.tsx diff --git a/src/app/dsa/page.tsx b/src/app/dsa/page.tsx new file mode 100644 index 0000000..1007e09 --- /dev/null +++ b/src/app/dsa/page.tsx @@ -0,0 +1,42 @@ +import Link from "next/link"; +import BubbleSort from "./bubblesort/page"; + +const Algorithms = () => { + return ( + <> +
+
+ + The Parth Structures and Algorithms +
+ +
+
+ Algorithms +
+
+ + + Bubble sort + + + Insertion sort + + + Quick sort (WIP) + + +
+ +
+ Data Structures +
+
+
+ + + ) + +} + +export default Algorithms; \ No newline at end of file diff --git a/src/app/algorithms/quicksort/page.tsx b/src/app/dsa/quicksort/page.tsx similarity index 100% rename from src/app/algorithms/quicksort/page.tsx rename to src/app/dsa/quicksort/page.tsx diff --git a/src/app/algorithms/quicksort/quicksort.ts b/src/app/dsa/quicksort/quicksort.ts similarity index 100% rename from src/app/algorithms/quicksort/quicksort.ts rename to src/app/dsa/quicksort/quicksort.ts diff --git a/src/app/dsa/tree/tree.ts b/src/app/dsa/tree/tree.ts new file mode 100644 index 0000000..6731953 --- /dev/null +++ b/src/app/dsa/tree/tree.ts @@ -0,0 +1,154 @@ +import { root } from "postcss" + +type Node = { + value: number, + leftNode?: Node | null, + rightNode?: Node | null, +} + +// 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 => { + 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) => { + 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); +} + +type deleteNodeProps = { + value: number, + root: Node, + parentNode?: Node, +} + +const deleteNode = (props: deleteNodeProps) => { + + const { value } = props + const { root } = props + const { parentNode } = props + + + + const findNextNode = (root: Node) => { + if (root.leftNode) findNextNode(root.leftNode); + return root + + } + // find target + if (value < root.value) { + if (!root.leftNode) { + console.log('node not found') + return; + } + deleteNode({ value, root: root.leftNode, parentNode: root }) + } + else if (value > root.value) { + if (!root.rightNode) { + console.log('node not found') + return; + } + + deleteNode({ value, root: root.rightNode, parentNode: root }) + } + else { + // target found + + if (parentNode) { + // two children + if (root.leftNode && root.rightNode) { + const nextNode = findNextNode(root.rightNode) + const tempValue = nextNode.value + deleteNode({ value: nextNode.value, root: root }) + root.value = tempValue + + } + // only one child + else if (root.leftNode || root.rightNode) { + + // if it's on the left then the parent node's left one needs to be reassigned + if (root.value < parentNode.value) { + parentNode.leftNode = root.leftNode || root.rightNode + } + else { + parentNode.rightNode = root.leftNode || root.rightNode + } + + + } + // no children + if (!root.leftNode && !root.rightNode) { + if (root.value < parentNode.value) { + parentNode.leftNode = null + } + if (root.value >= parentNode.value) { + parentNode.rightNode = null + } + } + } + + + + } + + +} + + + + + +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); +debugger; +deleteNode({ value: 3, root: startNode }) +console.log('after delete') +readTree(startNode, console.log); + + + + + + diff --git a/src/app/page.tsx b/src/app/page.tsx index 81f872f..1ca0dda 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -3,7 +3,7 @@ import { redirect } from "next/navigation"; const Home = () => { - redirect('/algorithms') + redirect('/dsa') }