Skip to content

small refactor and informational comments #5

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

Merged
merged 1 commit into from
Oct 9, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/binary-tree/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { beforeEach, describe, expect, test } from 'bun:test'
import BinaryTree, { Node } from '.'
import BinaryTree from '.'

describe('BinaryTree', () => {
let tree: BinaryTree<Number>
Expand Down
43 changes: 23 additions & 20 deletions src/binary-tree/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export class Node<Number> {
class Node<Number> {
value: Number
left: Node<Number> | null
right: Node<Number> | null
Expand All @@ -10,6 +10,8 @@ export class Node<Number> {
}

findMin(): Node<Number> {
// we know that the minimum value in a binary search tree is the leftmost node
// so we recurse to the left until we find a node that has no left child
if (!this.left) {
return this
} else {
Expand Down Expand Up @@ -40,21 +42,20 @@ export default class BinaryTree<Number> {
}

const recurseNodes = (node: Node<Number>): void => {
if (value < node.value) {
if (!node.left) {
node.left = new Node(value)
return
}

recurseNodes(node.left)
if (value < node.value && !node.left) {
// if the value we want to insert is less than the current node's value and
// the left child is null, we insert the value as the left child
node.left = new Node(value)
return
}

if (!node.right) {
} else if (!node.right) {
// if the value we want to insert is greater than the current node's value and
// the right child is null, we insert the value as the right child
node.right = new Node(value)
return
}

// if the value we want to insert is greater than or equal to the current node's
// value, we recurse to the right
recurseNodes(node.right)
}

Expand All @@ -65,13 +66,9 @@ export default class BinaryTree<Number> {
const recurseNodes = (node: Node<Number> | null): Node<Number> | null => {
if (!node) {
return null
}

if (node.value === value) {
} else if (node.value === value) {
return node
}

if (value < node.value) {
} else if (value < node.value) {
return recurseNodes(node.left)
}

Expand All @@ -85,20 +82,26 @@ export default class BinaryTree<Number> {
const recurseNodes = (node: Node<Number> | null): Node<Number> | null => {
if (!node) {
return null
}

if (value < node.value) {
} else if (value < node.value) {
// if the value we want to remove is less than the current node's value,
// we recurse to the left
node.left = recurseNodes(node.left)
} else if (value > node.value) {
// if the value we want to remove is greater than the current node's value,
// we recurse to the right
node.right = recurseNodes(node.right)
} else {
if (node.left === null && node.right === null) {
// if the node has no children, we can just remove it
node = null
} else if (node.left == null) {
// if the node has only a right child, we can replace the node with its right child
node = node.right
} else if (node.right == null) {
// if the node has only a left child, we can replace the node with its left child
node = node.left
} else {
// if the node has two children, we find the minimum value in the right subtree
const minRight = node.right.findMin()

node.value = minRight.value
Expand Down