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

Delete node function for tree #6

Open
wants to merge 3 commits 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 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.

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.
154 changes: 154 additions & 0 deletions src/app/dsa/tree/tree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import { root } from "postcss"
Copy link

Choose a reason for hiding this comment

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

This import of root from postcss appears to be unused and should be removed to keep the code clean and relevant.


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) => {
Copy link

Choose a reason for hiding this comment

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

The findNextNode function incorrectly assumes the next node is always the left-most child. This does not account for the right children, which should be considered when finding the next node in in-order traversal. Consider revising this function to correctly handle finding the next 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 })
Copy link

Choose a reason for hiding this comment

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

Recursive deletion in deleteNode function might cause issues if the node to delete is the root itself. Consider handling the case where the node to delete is the root to prevent potential infinite recursion or incorrect tree manipulation.

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);






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