Code coverage report for lib/algorithms/4-binaryTrees/makeList.js

Statements: 90.91% (20 / 22)      Branches: 84.62% (11 / 13)      Functions: 100% (3 / 3)      Lines: 100% (18 / 18)      Ignored: none     

All files » lib/algorithms/4-binaryTrees/ » makeList.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32      1   1 4 4 4 4   1 8     1 30 13 13   13   13 4     13 13       1
//4.4 Given a binary tree, design an algorithm to create a linked list at each depth.
//    If a tree has n depth, create n linked lists
 
var linkedList = require("../../../lib/dataStructures/linkedList.js");
 
var makeList = function(node) {
    Iif(node === null) return null;
    var list = getNode();
    makeList(list, node);
    return list;
 
    function getNode() {
        return { data: null, next: null };
    }
 
    function makeList(list, node) {
        if(node === null) return;
        Iif(list === null) throw "umpossible!";
        if(list.data === null) list.data = new linkedList();
 
        list.data.add(node.data);
 
        if((list.next === null) && ((node.left !== null) || (node.right !== null))) {
            list.next = getNode();
        }
 
        makeList(list.next, node.left);
        makeList(list.next, node.right);
    }
};
 
module.exports = makeList;