Skip to content

Commit a6447dd

Browse files
committed
implement BFS for binarySearchTree
1 parent 4e73c5c commit a6447dd

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

bst.html

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,20 @@ <h2>Breadth First Search</h2>
120120
<p><strong>Question:</strong> How do you implement Breadth First Search</p>
121121
<p><strong>Answer:</strong></p>
122122
<pre><code>
123-
123+
BinarySearchTree.prototype.breadthFirstSearch = function() {
124+
var queue = [];
125+
queue.push(this);
126+
while (queue.length) {
127+
var node = queue.shift();
128+
console.log(node.value);
129+
if (node.left) {
130+
queue.push(node.left);
131+
}
132+
if (node.right) {
133+
queue.push(node.right);
134+
}
135+
}
136+
};
124137
</code></pre>
125138
<p>ref: <a href="http://stackoverflow.com/a/13633354/1535443">stackoverflow</a></p>
126139
<p>ref: <a href="https://github.com/duereg/js-algorithms">js algorithms</a></p>

0 commit comments

Comments
 (0)