Advertisement
crummy

Untitled

Oct 13th, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function (tree) {
  2.                     // http://programmers.stackexchange.com/questions/214227/
  3.                     var stack = [],
  4.                         replies = tree.replies.slice(1, tree.replies.length),
  5.                         root = tree.replies[0];
  6.                     root.children = [];
  7.                     stack.push(root);
  8.                     replies.forEach(function (reply) {
  9.                         var delta = reply.depth - stack.length;
  10.                         reply.children = [];
  11.                         if (delta > 0) {
  12.                             assert(delta === 1);
  13.                             stack.push(stack.front().children.front());
  14.                         }
  15.                         while (delta < 0) {
  16.                             stack.pop();
  17.                             delta++;
  18.                         }
  19.                         stack.front().children.push(reply);
  20.                     });
  21.                     this.rootReply = root;
  22.                 },
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement