mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2026-03-13 08:51:02 +08:00
Create getters and setters for meta data in binary tree node.
This commit is contained in:
@@ -5,7 +5,7 @@ export default class BinaryTreeNode {
|
||||
* @param {*} [value] - node value.
|
||||
* @param {Object} meta - any meta information that needs to be attached to the node.
|
||||
*/
|
||||
constructor(value = null, meta = null) {
|
||||
constructor(value = null, meta = {}) {
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
this.parent = null;
|
||||
@@ -157,6 +157,29 @@ export default class BinaryTreeNode {
|
||||
return traverse;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} property
|
||||
* @param {*} value
|
||||
* @return {BinaryTreeNode}
|
||||
*/
|
||||
setMeta(property, value) {
|
||||
this.meta[property] = value;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param property
|
||||
* @return {*}
|
||||
*/
|
||||
getMeta(property) {
|
||||
if (!this.meta || !Object.prototype.hasOwnProperty.call(this.meta, property)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.meta[property];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user