Skip to content

Instantly share code, notes, and snippets.

@albovieira
Created December 8, 2019 03:23
Show Gist options
  • Save albovieira/daf68708e5bd9df005caea5aded6dada to your computer and use it in GitHub Desktop.
Save albovieira/daf68708e5bd9df005caea5aded6dada to your computer and use it in GitHub Desktop.
tree in ts
import { Func } from 'mocha';
//Ref: https://code.tutsplus.com/articles/data-structures-with-javascript-tree--cms-23393
class NodeTree {
readonly name: string;
readonly value: any;
private parent: string | null;
private children: NodeTree[];
constructor(name: string, value: any) {
this.name = name;
this.value = value;
this.parent = null;
this.children = [];
}
getChildren() {
return this.children;
}
getChild(index?: number) {
return this.children[index];
}
addParent(parent: NodeTree) {
this.parent! = parent.name;
return this;
}
addChild(child: NodeTree) {
this.children!.push(child);
return this;
}
parentIsRoot() {
return this.parent === 'root';
}
}
class Tree {
private root: NodeTree;
constructor() {
this.root = new NodeTree('root', null);
}
show() {
return this.root;
}
add(name: string, value: any, parent?: any, callback?: Function) {
const child = new NodeTree(name, value);
const rootChildren = this.root.getChildren();
if (!parent) {
let node;
try {
node = this.transverseByNodeName(rootChildren, name, value);
} catch (error) {
console.log('error', error);
}
if (node) {
return this;
}
child.addParent(this.root);
this.root.addChild(child);
return this;
}
const parentNode = this.transverseByParent(rootChildren, parent);
child.addParent(parentNode);
parentNode.addChild(child);
if (callback) {
callback();
}
return this;
}
contains() {}
remove(node: any, parent: any) {}
transverseByParent(nodes: NodeTree[], parent: any) {
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i];
if (node.name === parent.name && node.value === parent.value) {
return node;
}
if (node.getChildren().length > 0) {
const n = this.transverseByParent(node.getChildren(), parent);
if (n) return n;
}
}
return null;
}
transverseByNodeName(nodes: NodeTree[], name: string, value: string) {
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i];
if (node.name === name && node.value === value) {
return node;
}
if (node.getChildren().length > 0) {
const n = this.transverseByNodeName(node.getChildren(), name, value);
if (n) return n;
}
}
throw new Error('Node not found');
}
}
const tree = new Tree();
const datas = [
{
ranking: 1,
filters: [
{ name: 'airline', value: 'azul' },
{
name: 'isOta',
value: true,
callback: () => {
console.log('ranking 1');
}
}
]
},
{
ranking: 2,
filters: [
{ name: 'airline', value: 'azul' },
{ name: 'isOta', value: false },
{
name: 'hasBaggage',
value: true,
callback: () => {
console.log('ranking 2 azul');
}
}
]
},
{
ranking: 3,
filters: [
{ name: 'airline', value: 'gol' },
{ name: 'isOta', value: true },
{
name: 'hasBaggage',
value: true
},
{
name: 'isInternational',
value: false,
callback: () => {
console.log('ranking 3 gol');
}
}
]
},
{
ranking: 4,
filters: [
{ name: 'airline', value: 'gol' },
{ name: 'isOta', value: false },
{
name: 'hasBaggage',
value: true
},
{
name: 'isInternational',
value: true,
callback: () => {
console.log('ranking 2 gol');
}
}
]
}
];
datas.forEach(data => {
const { ranking, filters } = data;
const sizeFilter = filters.length;
filters.forEach((filter, key) => {
const { name, value, callback } = filter;
const parent = filters[key - 1] || null;
tree.add(name, value, parent, callback);
});
});
console.log('done');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment