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

Method #insertNode

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

Source from the content-addressed store, hash-verified

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

Callers 2

insertMethod · 0.95
BinarySearchTreeClass · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected