一棵简单的无序树; 在下图中:
标记为7的节点具有两个子节点, 标记为2和6; 一个父节点,标记为2,作为根节点, 在顶部,没有父节点。遍历采用中序遍历。
import Comparator from '../../utils/comparator/Comparator'; import HashTable from '../hash-table/HashTable'; export default class BinaryTreeNode { /** * @param {*} [value] - node value. */ constructor(value = null) { this.left = null; this.right = null; this.parent = null; this.value = value; // Any node related meta information may be stored here. this.meta = new HashTable(); // This comparator is used to compare binary tree nodes with each other. this.nodeComparator = new Comparator(); } /** * @return {number} */ get leftHeight() { if (!this.left) { return 0; } return this.left.height + 1; } /** * @return {number} */ get rightHeight() { if (!this.right) { return 0; }