Created
March 22, 2020 20:56
-
-
Save njofce/213629437806e8bdbde7c564667725f6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| export class TreeNode<T> { | |
| private _data: T; | |
| private _parent: TreeNode<T>; | |
| private _children: TreeNode<T>[]; | |
| constructor(data: T, parent: TreeNode<T>, children: TreeNode<T>[] = []) { | |
| this._data = data; | |
| this._parent = parent; | |
| this._children = children; | |
| } | |
| get data(): T { | |
| return this._data; | |
| } | |
| get parent(): TreeNode<T> { | |
| return this._parent; | |
| } | |
| get children(): TreeNode<T>[] { | |
| return this._children; | |
| } | |
| addChild(child: TreeNode<T>) { | |
| this._children.push(child); | |
| } | |
| getLevel() { | |
| let level = 0; | |
| let currentParent = this._parent; | |
| while(currentParent != null) { | |
| level++; | |
| currentParent = currentParent.parent; | |
| } | |
| return level; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment