Skip to content

Commit

Permalink
Changes to tree
Browse files Browse the repository at this point in the history
  • Loading branch information
ari7946 committed Oct 28, 2019
1 parent de85c9b commit c3a5ff7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
20 changes: 19 additions & 1 deletion exercises/bst/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@
// class. Contains should accept a 'data' argument
// and return the Node in the tree with the same value.

class Node {}
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}

insert(data) {
if (data < this.data && this.left) {
this.left.insert(data);
} else if (data < this.data) {
this.left = new Node(data);
} else if (data > this.data && this.right) {
this.right.insert(data);
} else if (data > this.data) {
this.right = new Node(data);
}
}
}

module.exports = Node;
20 changes: 19 additions & 1 deletion exercises/levelwidth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@
// 4 5
// Answer: [1, 3, 2]

function levelWidth(root) {}
function levelWidth(root) {
const counters = [0];
const stopper = 's';
const arr = [root, stopper];

while (arr.length > 1) {
if (arr[0] === stopper) {
counters.push(0);
let node = arr.shift();
arr.push(node);
} else {
let node = arr.shift();
arr.push(...node.children);
counters[counters.length - 1] += 1;
}
}

return counters;
}

module.exports = levelWidth;

0 comments on commit c3a5ff7

Please sign in to comment.