-
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
Delete node function for tree #6
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,154 @@ | ||
import { root } from "postcss" | ||
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. This import of |
||
|
||
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) => { | ||
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 (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 }) | ||
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. Recursive deletion in |
||
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); | ||
|
||
|
||
|
||
|
||
|
||
|
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 file still point to
/algorithms/...
paths, but should be updated to/dsa/...
to reflect the new directory structure and ensure they lead to the correct pages.