hufftree.js
/**
     * Insert a value into the tree at the given depth. Uses a level-order
     * traversal to fill the tree from left to right at a given level.
     * 
     * Returns 'true' if inserting the value was successful, otherwise false
     * 
     * @param {*} codeLength Depth of the tree at which to insert (length of huffman code)
     * @param {*} value Value to insert into the tree
     */
Insert a value into the tree at the given depth. Uses a level-order
traversal to fill the tree from left to right at a given level.

Returns 'true' if inserting the value was successful, otherwise false

@param {*} codeLength Depth of the tree at which to insert (length of huffman code)
@param {*} value Value to insert into the tree
Insert a value into the tree at the given depth . Uses a level - order traversal to fill the tree from left to right at a given level . Returns ' true ' if inserting the value was successful otherwise false @param { * } codeLength Depth of the tree at which to insert ( length of huffman code ) @param { * } value Value to insert into the tree
insertCode(codeLength, value) {
        if (this.data != undefined) {
            return false;   
        }
        if (codeLength === this.level) {
            if (this.data === undefined) {
                this.data = value;
                return true;
            } else {
                return false;   
            }
        } else {
            if (this.left === undefined) {
                this.left = new HuffTree(this.level + 1);
                this.right = new HuffTree(this.level + 1);
                return this.left.insertCode(codeLength, value);
            } else {
                if (this.left.insertCode(codeLength, value)) {
                    return true;
                } else {
                    return this.right.insertCode(codeLength, value);
                }
            }
        }
    }
insertCode ( codeLength , value ) { if ( this . data != undefined ) { return false ; } if ( codeLength === this . level ) { if ( this . data === undefined ) { this . data = value ; return true ; } else { return false ; } } else { if ( this . left === undefined ) { this . left = new HuffTree ( this . level + 1 ) ; this . right = new HuffTree ( this . level + 1 ) ; return this . left . insertCode ( codeLength , value ) ; } else { if ( this . left . insertCode ( codeLength , value ) ) { return true ; } else { return this . right . insertCode ( codeLength , value ) ; } } } }
Insert a value into the tree at the given depth.
Insert a value into the tree at the given depth .
// Invariant: leaf nodes can't have children // We're full, try the node to the right // Invariant: nodes always created in pairs. // Saves us from lots of checking for 'undefined' elsewhere