committed by
Oliver Eyton-Williams
parent
ca29fb664e
commit
5f894a3231
@ -116,6 +116,45 @@ BinarySearchTree.prototype = {
|
|||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// solution required
|
var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
|
||||||
|
function Node(value) {
|
||||||
|
this.value = value;
|
||||||
|
this.left = null;
|
||||||
|
this.right = null;
|
||||||
|
}
|
||||||
|
function BinarySearchTree() {
|
||||||
|
this.root = null;
|
||||||
|
this.result = [];
|
||||||
|
|
||||||
|
this.inorder = function(node) {
|
||||||
|
if (!node) node = this.root;
|
||||||
|
if (!node) return null;
|
||||||
|
|
||||||
|
if (node.left) this.inorder(node.left);
|
||||||
|
this.result.push(node.value)
|
||||||
|
if (node.right) this.inorder(node.right);
|
||||||
|
return this.result;
|
||||||
|
|
||||||
|
};
|
||||||
|
this.preorder = function(node) {
|
||||||
|
if (!node) node = this.root;
|
||||||
|
if (!node) return null;
|
||||||
|
|
||||||
|
this.result.push(node.value);
|
||||||
|
if (node.left) this.preorder(node.left);
|
||||||
|
if (node.right) this.preorder(node.right);
|
||||||
|
return this.result;
|
||||||
|
};
|
||||||
|
this.postorder = function(node) {
|
||||||
|
if (!node) node = this.root;
|
||||||
|
if (!node) return null;
|
||||||
|
|
||||||
|
if (node.left) this.postorder(node.left);
|
||||||
|
if (node.right) this.postorder(node.right);
|
||||||
|
this.result.push(node.value);
|
||||||
|
|
||||||
|
return this.result;
|
||||||
|
};
|
||||||
|
}
|
||||||
```
|
```
|
||||||
</section>
|
</section>
|
||||||
|
Reference in New Issue
Block a user