MCPcopy Create free account
hub / github.com/denoland/std / #insertNode

Method #insertNode

data_structures/binary_search_tree.ts:360–384  ·  view source on GitHub ↗
(
    Node: typeof BinarySearchNode,
    value: T,
  )

Source from the content-addressed store, hash-verified

358 }
359
360 #insertNode(
361 Node: typeof BinarySearchNode,
362 value: T,
363 ): BinarySearchNode<T> | null {
364 if (!this.#root) {
365 this.#root = new Node(null, value);
366 this.#size++;
367 return this.#root;
368 } else {
369 let node: BinarySearchNode<T> = this.#root;
370 while (true) {
371 const order: number = this.#compare(value, node.value);
372 if (order === 0) break;
373 const direction: Direction = order < 0 ? "left" : "right";
374 if (node[direction]) {
375 node = node[direction]!;
376 } else {
377 node[direction] = new Node(node, value);
378 this.#size++;
379 return node[direction];
380 }
381 }
382 }
383 return null;
384 }
385
386 /** Removes the given node, and returns the node that was physically removed from the tree. */
387 #removeNode(

Callers 2

insertMethod · 0.95
BinarySearchTreeClass · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected