Skip to content

Commit e57c1f3

Browse files
committed
added missing tree.ts to repo
1 parent 2174cd3 commit e57c1f3

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

frontend/src/app/tree.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
interface TreeNode<T> {
3+
value: T;
4+
children: TreeNode<T>[];
5+
}
6+
7+
class Tree<T> {
8+
_root?: TreeNode<T>;
9+
10+
get root(): TreeNode<T> {
11+
if (!this._root) {
12+
throw new Error("Tree is empty")
13+
}
14+
return this._root;
15+
}
16+
17+
constructor(root?: TreeNode<T>) {
18+
this._root = root;
19+
}
20+
21+
static empty<T>(): Tree<T> {
22+
return new Tree();
23+
}
24+
25+
static fromJSON<T>(json: any): Tree<T> {
26+
// there's no validation of the content
27+
return new Tree(json);
28+
}
29+
}
30+
31+
export {Tree, TreeNode};

0 commit comments

Comments
 (0)