-
Notifications
You must be signed in to change notification settings - Fork 677
feat: add callback functionality for binary search tree #6730
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
Changes from 5 commits
d9443d5
e3231db
b8b071c
20f91c2
a2fd74c
fd4426a
e0b98bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| // Copyright 2018-2025 the Deno authors. MIT license. | ||
| // This module is browser compatible. | ||
|
|
||
| /** | ||
| * A generic Binary Search Tree (BST) node class. | ||
| * | ||
| * @example Creating a new BSTNode. | ||
| * ```ts | ||
| * import { BSTNode } from "@std/data-structures"; | ||
| * import { assertEquals } from "@std/assert"; | ||
| * | ||
| * const node = new BSTNode<number>(null, 42); | ||
| * assertEquals(node.value, 42); | ||
| * assertEquals(node.left, null); | ||
| * assertEquals(node.right, null); | ||
| * assertEquals(node.parent, null); | ||
| * ``` | ||
| * | ||
| * @typeparam T The type of the values stored in the binary tree. | ||
| */ | ||
| export class BSTNode<T> { | ||
|
Contributor
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. We usually avoid using this type of acronym in API names. BinarySearchTree used to be BSTree but we renamed it later #2400 I think this should be named Also I think this should be typescript
Author
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. OK, renamed to BinarySearchTreeNode and made into an interface. |
||
| /** | ||
| * The left child node, or null if there is no left child. | ||
| * | ||
| * @example Checking the left child of a node in a binary search tree. | ||
| * ```ts | ||
| * import { BinarySearchTree } from "@std/data-structures"; | ||
| * import { assertEquals } from "@std/assert"; | ||
| * | ||
| * const tree = new BinarySearchTree<number>(); | ||
| * | ||
| * assertEquals(tree.insert(42), true); | ||
| * assertEquals(tree.insert(21), true); | ||
| * | ||
| * const root = tree.getRoot(); | ||
| * const leftChild = root?.left; | ||
| * | ||
| * assertEquals(leftChild?.value, 21); | ||
| * ``` | ||
| */ | ||
| left: BSTNode<T> | null; | ||
|
|
||
| /** | ||
| * The right child node, or null if there is no right child. | ||
| * | ||
| * @example Checking the right child of a node in a binary search tree. | ||
| * ```ts | ||
| * import { BinarySearchTree } from "@std/data-structures"; | ||
| * import { assertEquals } from "@std/assert"; | ||
| * | ||
| * const tree = new BinarySearchTree<number>(); | ||
| * | ||
| * assertEquals(tree.insert(21), true); | ||
| * assertEquals(tree.insert(42), true); | ||
| * | ||
| * const root = tree.getRoot(); | ||
| * const leftChild = root?.left; | ||
| * | ||
| * assertEquals(leftChild?.value, 42); | ||
| * ``` | ||
| */ | ||
| right: BSTNode<T> | null; | ||
|
|
||
| /** | ||
| * The parent of this node, or null if there is no parent. | ||
| * | ||
| * @example Checking the parent of a node in a binary search tree. | ||
| * ```ts | ||
| * import { BinarySearchTree } from "@std/data-structures"; | ||
| * import { assertEquals } from "@std/assert"; | ||
| * | ||
| * const tree = new BinarySearchTree<number>(); | ||
| * | ||
| * assertEquals(tree.insert(42), true); | ||
| * assertEquals(tree.insert(21), true); | ||
| * | ||
| * const root = tree.getRoot(); | ||
| * const leftChild = root?.left; | ||
| * | ||
| * assertEquals(leftChild?.parent?.value, 42); | ||
| * ``` | ||
| */ | ||
| parent: BSTNode<T> | null; | ||
|
|
||
| /** | ||
| * The value stored at this node. | ||
| * | ||
| * @example Accessing the value of a node in a binary search tree. | ||
| * ```ts | ||
| * import { BinarySearchTree } from "@std/data-structures"; | ||
| * import { assertEquals } from "@std/assert"; | ||
| * | ||
| * const tree = new BinarySearchTree<number>(); | ||
| * assertEquals(tree.insert(42), true); | ||
| * | ||
| * const root = tree.getRoot(); | ||
| * assertEquals(root?.value, 42); | ||
| * ``` | ||
| */ | ||
| value: T; | ||
|
|
||
| /** | ||
| * Creates a new BSTNode. | ||
| * @param parent The parent node, or null if this is the root node. | ||
| * @param value The value of the node. | ||
| */ | ||
| constructor(parent: BSTNode<T> | null, value: T) { | ||
| this.left = null; | ||
| this.right = null; | ||
| this.parent = parent; | ||
| this.value = value; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Copyright 2018-2025 the Deno authors. MIT license. | ||
| // This module is browser compatible. | ||
|
|
||
| import { assertStrictEquals } from "@std/assert"; | ||
| import { BSTNode } from "./bst_node.ts"; | ||
|
|
||
| let parent: BSTNode<number>; | ||
| let child: BSTNode<number>; | ||
| function beforeEach() { | ||
| parent = new BSTNode(null, 5); | ||
| child = new BSTNode(parent, 7); | ||
| parent.right = child; | ||
| } | ||
|
|
||
| Deno.test("BSTNode", () => { | ||
| beforeEach(); | ||
| assertStrictEquals(parent.parent, null); | ||
| assertStrictEquals(parent.left, null); | ||
| assertStrictEquals(parent.right, child); | ||
| assertStrictEquals(parent.value, 5); | ||
|
|
||
| assertStrictEquals(child.parent, parent); | ||
| assertStrictEquals(child.left, null); | ||
| assertStrictEquals(child.right, null); | ||
| assertStrictEquals(child.value, 7); | ||
| }); |
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.
Nicely documented 👍