made with esnextbin
Last active
October 31, 2017 19:34
-
-
Save OscardR/86ca685aa6c1d5981e7149219716597b to your computer and use it in GitHub Desktop.
BinaryTree test in ESNext.bin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>ESNextbin Sketch</title> | |
<!-- put additional styles and scripts here --> | |
</head> | |
<body> | |
<!-- put markup and other contents here --> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BinaryTree { | |
value; | |
left; | |
right; | |
constructor(value) { | |
this.value = value; | |
} | |
insert(value, s = 0) { | |
if (this.value === undefined) { | |
this.value = value; | |
} else { | |
s++; | |
if (value < this.value) { | |
if (this.left) { | |
return this.left.insert(value, s); | |
} else { | |
this.left = new BinaryTree(value); | |
} | |
} else { | |
if (this.right) { | |
return this.right.insert(value, s); | |
} else { | |
this.right = new BinaryTree(value); | |
} | |
} | |
} | |
return s; | |
} | |
all() { | |
return (this.left ? | |
this.left.all() : []) | |
.concat([this.value]) | |
.concat(this.right ? | |
this.right.all() : []); | |
} | |
search(n, i = 0) { | |
if (n === this.value) { | |
return [true, i]; | |
} | |
if (n < this.value) { | |
if (this.left) { | |
return this.left.search(n, ++i); | |
} else { | |
return [false, i]; | |
} | |
} else { | |
if (this.right) { | |
return this.right.search(n, ++i); | |
} else { | |
return [false, i]; | |
} | |
} | |
} | |
} | |
const N = 1000, | |
MAX = 100, | |
ARROW = `→`; | |
class App { | |
static gimmeNumber() { | |
return Math.floor(Math.random() * App.MAX); | |
} | |
static insertElements(bt) { | |
document.write(`<ul>`); | |
for (let i = 0; i < App.N; i++) { | |
let n = App.gimmeNumber(), | |
s = bt.insert(n); | |
document.write(`<li>Inserting ${n} ${ARROW} ${s} steps</li>`); | |
} | |
document.write(`</ul>`); | |
} | |
static showSortedList(bt) { | |
document.write(`<p>Sorted list: [${bt.all().join(', ')}] ${ARROW} O(1)</p>`); | |
} | |
static searchRandomNumber(bt) { | |
let n = App.gimmeNumber(), | |
result = bt.search(n); | |
document.write(`<p>Is ${n} in the list? ${result[0]} ${ARROW} ${result[1]} steps</p>`); | |
} | |
static main(N = N, MAX = MAX) { | |
App.N = N; | |
App.MAX = MAX; | |
document.write(`<h1>BinaryTree Demo</h1>`); | |
let bt = new BinaryTree(); | |
App.insertElements(bt); | |
App.showSortedList(bt); | |
App.searchRandomNumber(bt); | |
} | |
} | |
App.main(20, 100); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "esnextbin-sketch", | |
"version": "0.0.0", | |
"dependencies": { | |
"babel-runtime": "6.22.0" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); | |
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); | |
var _createClass2 = require('babel-runtime/helpers/createClass'); | |
var _createClass3 = _interopRequireDefault(_createClass2); | |
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
var BinaryTree = function () { | |
function BinaryTree(value) { | |
(0, _classCallCheck3.default)(this, BinaryTree); | |
this.value = value; | |
} | |
(0, _createClass3.default)(BinaryTree, [{ | |
key: 'insert', | |
value: function insert(value) { | |
var s = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1]; | |
if (this.value === undefined) { | |
this.value = value; | |
} else { | |
s++; | |
if (value < this.value) { | |
if (this.left) { | |
return this.left.insert(value, s); | |
} else { | |
this.left = new BinaryTree(value); | |
} | |
} else { | |
if (this.right) { | |
return this.right.insert(value, s); | |
} else { | |
this.right = new BinaryTree(value); | |
} | |
} | |
} | |
return s; | |
} | |
}, { | |
key: 'all', | |
value: function all() { | |
return (this.left ? this.left.all() : []).concat([this.value]).concat(this.right ? this.right.all() : []); | |
} | |
}, { | |
key: 'search', | |
value: function search(n) { | |
var i = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1]; | |
if (n === this.value) { | |
return [true, i]; | |
} | |
if (n < this.value) { | |
if (this.left) { | |
return this.left.search(n, ++i); | |
} else { | |
return [false, i]; | |
} | |
} else { | |
if (this.right) { | |
return this.right.search(n, ++i); | |
} else { | |
return [false, i]; | |
} | |
} | |
} | |
}]); | |
return BinaryTree; | |
}(); | |
var N = 1000, | |
MAX = 100, | |
ARROW = '→'; | |
var App = function () { | |
function App() { | |
(0, _classCallCheck3.default)(this, App); | |
} | |
(0, _createClass3.default)(App, null, [{ | |
key: 'gimmeNumber', | |
value: function gimmeNumber() { | |
return Math.floor(Math.random() * App.MAX); | |
} | |
}, { | |
key: 'insertElements', | |
value: function insertElements(bt) { | |
document.write('<ul>'); | |
for (var i = 0; i < App.N; i++) { | |
var n = App.gimmeNumber(), | |
s = bt.insert(n); | |
document.write('<li>Inserting ' + n + ' ' + ARROW + ' ' + s + ' steps</li>'); | |
} | |
document.write('</ul>'); | |
} | |
}, { | |
key: 'showSortedList', | |
value: function showSortedList(bt) { | |
document.write('<p>Sorted list: [' + bt.all().join(', ') + '] ' + ARROW + ' O(1)</p>'); | |
} | |
}, { | |
key: 'searchRandomNumber', | |
value: function searchRandomNumber(bt) { | |
var n = App.gimmeNumber(), | |
result = bt.search(n); | |
document.write('<p>Is ' + n + ' in the list? ' + result[0] + ' ' + ARROW + ' ' + result[1] + ' steps</p>'); | |
} | |
}, { | |
key: 'main', | |
value: function main() { | |
var N = arguments.length <= 0 || arguments[0] === undefined ? N : arguments[0]; | |
var MAX = arguments.length <= 1 || arguments[1] === undefined ? MAX : arguments[1]; | |
App.N = N; | |
App.MAX = MAX; | |
document.write('<h1>BinaryTree Demo</h1>'); | |
var bt = new BinaryTree(); | |
App.insertElements(bt); | |
App.showSortedList(bt); | |
App.searchRandomNumber(bt); | |
} | |
}]); | |
return App; | |
}(); | |
App.main(20, 100); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment