|
| 1 | +(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ |
| 2 | +"use strict"; |
| 3 | + |
| 4 | +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); |
| 5 | + |
| 6 | +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } |
| 7 | + |
| 8 | +/** |
| 9 | + * @typedef {{value:T,lNode: BTreeNodeStruct | BTreeNode, rNode: BTreeNodeStruct | BTreeNode}} BTreeNodeStruct |
| 10 | + * @interface |
| 11 | + * @private |
| 12 | + */ |
| 13 | + |
| 14 | +var bTreeNodeStruct = {}; |
| 15 | + |
| 16 | +/** |
| 17 | + * Binary Tree node class, contains 2 child nodes and single value. |
| 18 | + * @class |
| 19 | + * @public |
| 20 | + * @example |
| 21 | + * var node = new BTreeNode({ value: 15 }); |
| 22 | + * var node3 = new BTreeNode({ value: 30 }); |
| 23 | + * var node2 = new BTreeNode({ value: 20, rNode: node, lNode: node3 }); |
| 24 | + * console.log(node2.lNode.value); // 30 |
| 25 | + */ |
| 26 | + |
| 27 | +var BTreeNode = function () { |
| 28 | + /** |
| 29 | + * |
| 30 | + * @param {BTreeNodeStruct} attr attributes to initialize the node. |
| 31 | + */ |
| 32 | + function BTreeNode() { |
| 33 | + var attr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; |
| 34 | + |
| 35 | + _classCallCheck(this, BTreeNode); |
| 36 | + |
| 37 | + /** |
| 38 | + * @property value |
| 39 | + * Contains actual value |
| 40 | + * @type {any} |
| 41 | + * @public |
| 42 | + */ |
| 43 | + this.value = attr.value || null; |
| 44 | + |
| 45 | + /** |
| 46 | + * @property lNode |
| 47 | + * Contains left child node |
| 48 | + * @type {BTreeNode} |
| 49 | + * @public |
| 50 | + */ |
| 51 | + this.lNode = attr.lNode || null; |
| 52 | + |
| 53 | + if (!this.lNode instanceof BTreeNode) { |
| 54 | + this.lNode = new BTreeNode(this.lNode); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @property rNode |
| 59 | + * Contains right child node |
| 60 | + * @type {BTreeNode} |
| 61 | + * @public |
| 62 | + */ |
| 63 | + this.rNode = attr.rNode || null; |
| 64 | + |
| 65 | + if (!this.rNode instanceof BTreeNode) { |
| 66 | + this.rNode = new BTreeNode(this.rNode); |
| 67 | + } |
| 68 | + |
| 69 | + if (!this.validate()) { |
| 70 | + throw new Error("A BTree node must have a valid value, cannot be null or undefined"); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Validates a BTree node, it must have a valid value (no undefined nor null). |
| 76 | + * @public |
| 77 | + * @returns {boolean} |
| 78 | + * @example |
| 79 | + * new BTreeNode(); // throws error saying `A BTree node must have a valid value, cannot be null or undefined` |
| 80 | + * var node = new BTreeNode({ value: 10 }); |
| 81 | + * console.log(node.validate()); // true |
| 82 | + */ |
| 83 | + |
| 84 | + |
| 85 | + _createClass(BTreeNode, [{ |
| 86 | + key: "validate", |
| 87 | + value: function validate() { |
| 88 | + return this.value != void 0 || this.value != null; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Converts current node and all children nodes in json format. |
| 93 | + * @public |
| 94 | + * @returns {BTreeNodeStruct} |
| 95 | + * @example |
| 96 | + * var node = new BTreeNode({ value: 10 }); |
| 97 | + * var lNode = new BTreeNode({ value: 15, lNode: lNode }); |
| 98 | + * console.log(node.toJSON()); // {value:15,lNode: {value: 10,lNode:null,rNode:null},rNode:null} |
| 99 | + */ |
| 100 | + |
| 101 | + }, { |
| 102 | + key: "toJSON", |
| 103 | + value: function toJSON() { |
| 104 | + return { |
| 105 | + value: this.value && typeof this.value.toJSON === "function" ? this.value.toJSON() : this.value, |
| 106 | + lNode: this.lNode === null ? null : this.lNode.toJSON(), |
| 107 | + rNode: this.rNode === null ? null : this.rNode.toJSON() |
| 108 | + }; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Converts current node and all children nodes in json format and append them together. |
| 113 | + * Goes in direction of U -> L -> R |
| 114 | + * @public |
| 115 | + * @returns {string} |
| 116 | + * @example |
| 117 | + * var node = new BTreeNode({ value: 10 }); |
| 118 | + * var lNode = new BTreeNode({ value: 15, lNode: lNode }); |
| 119 | + * console.log(node.toString()); // "1015" |
| 120 | + */ |
| 121 | + |
| 122 | + }, { |
| 123 | + key: "toString", |
| 124 | + value: function toString() { |
| 125 | + var leftStr = this.lNode === null ? "" : this.lNode.toString(); |
| 126 | + var rightStr = this.rNode === null ? "" : this.rNode.toString(); |
| 127 | + var currStr = typeof this.value.toString === "function" ? this.value.toString() : String(this.value); |
| 128 | + |
| 129 | + return this.value.toString() + leftStr + rightStr; |
| 130 | + } |
| 131 | + }]); |
| 132 | + |
| 133 | + return BTreeNode; |
| 134 | +}(); |
| 135 | + |
| 136 | +if (typeof module != "undefined") { |
| 137 | + module.exports = BTreeNode; |
| 138 | +} |
| 139 | +if (typeof window != "undefined") { |
| 140 | + window.DSinJS = window.DSinJS || {}; |
| 141 | + window.DSinJS.BTreeNode = BTreeNode; |
| 142 | +} |
| 143 | +},{}]},{},[1]); |
0 commit comments