binary-search-tree/red-black-tree

View on GitHub
src/family/uncle.js

Summary

Maintainability
A
0 mins
Test Coverage
import assert from 'assert';

import Node from '../types/Node.js';

import grandparent from './grandparent.js';

/**
 * Computes the uncle of the input node when the grandparent is guaranteed to
 * exist.
 *
 * @param {Node} node - The input node.
 * @returns {Node}
 */
const uncle = (node) => {
    assert(node instanceof Node);
    const g = grandparent(node);
    assert(g !== null);
    return node.parent === g.left ? g.right : g.left;
};

export default uncle;