import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `<h1>Hello {{name}}</h1>`
})
export class AppComponent { name = 'Angular'; }


/*
Copyright 2017 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent }  from './app.component';
import { TreeNodeCom } from './tree-node.com';


@NgModule({
  imports:      [ BrowserModule ],
  declarations: [ TreeNodeCom ],
  bootstrap:    [ TreeNodeCom ]
})
export class AppModule { }


/*
Copyright 2017 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app/app.module';

platformBrowserDynamic().bootstrapModule(AppModule);


/*
Copyright 2017 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
<!DOCTYPE html>
<html>
  <head>
    <title>Angular Quickstart</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
      body {color:#369;font-family: Arial,Helvetica,sans-serif;}
    </style>

    <!-- Polyfills -->
    <script src="https://unpkg.com/core-js/client/shim.min.js"></script>

    <script src="https://unpkg.com/zone.js@0.7.4?main=browser"></script>
    <script src="https://unpkg.com/systemjs@0.19.39/dist/system.src.js"></script>
    <script src="systemjs.config.js"></script>
    <script>
      System.import('main.js').catch(function(err){ console.error(err); });
    </script>
  </head>

  <body>
    <tree-node-com>Loading AppComponent content here ...</tree-node-com>
  </body>

</html>


<!-- 
Copyright 2017 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
-->
/**
 * WEB ANGULAR VERSION
 * (based on systemjs.config.js in angular.io)
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
  System.config({
    // DEMO ONLY! REAL CODE SHOULD NOT TRANSPILE IN THE BROWSER
    transpiler: 'ts',
    typescriptOptions: {
      // Copy of compiler options in standard tsconfig.json
      "target": "es5",
      "module": "commonjs",
      "moduleResolution": "node",
      "sourceMap": true,
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "lib": ["es2015", "dom"],
      "noImplicitAny": true,
      "suppressImplicitAnyIndexErrors": true
    },
    meta: {
      'typescript': {
        "exports": "ts"
      }
    },
    paths: {
      // paths serve as alias
      'npm:': 'https://unpkg.com/'
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      app: 'app',

      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/router/upgrade': 'npm:@angular/router/bundles/router-upgrade.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
      '@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',
      '@angular/upgrade/static': 'npm:@angular/upgrade/bundles/upgrade-static.umd.js',

      // other libraries
      'rxjs':                      'npm:rxjs@5.0.1',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
      'ts':                        'npm:plugin-typescript@5.2.7/lib/plugin.js',
      'typescript':                'npm:typescript@2.0.10/lib/typescript.js',

    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        main: './main.ts',
        defaultExtension: 'ts'
      },
      rxjs: {
        defaultExtension: 'js'
      }
    }
  });

})(this);

/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
import { Component, OnInit, Input } from '@angular/core';

import { TreeNode } from './tree-node';

@Component({
	selector: 'tree-node-com',
	template: `
		<div *ngIf="root">{{title}}</div>
		<ul>{{tree.e}}({{tree.size()}})
			<li *ngFor="let n of tree.c">
				<tree-node-com [tree]="n"></tree-node-com>
			</li>
		</ul>`
})
export class TreeNodeCom implements OnInit {
	title: string = 'Tree - Tree Structure';

	@Input() tree;
	root: boolean = false;

	constructor() { }

	ngOnInit() {
		if (!this.tree) {
			this.root = true;
			this.tree = new TreeNode('/');
			this.initTree();
		}
		this.tree.log();
	}

	initTree() {
		// Create 2 level of node
		for (let i = 0; i < 5; i++) {
			let n = this.tree.add(`${i}`);
			for (let j = 0; j < 5; j++) {
				n.add(j);
			}
		}
	}
}
export class TreeNode {
	// Parent Node
	p: TreeNode;
	// Children Node
	c: TreeNode[];
	// Element
	e: any;
	// Number of element in tree
	private n: number;

	constructor(e: any) {
		this.p = undefined;
		this.e = e;
		this.n = 1;
		this.c = [];
	}

	toString(): string {
		if (typeof (this.e) === 'string') {
			return this.e;
		}
		return this.e.toString();
	}

	equals(n: TreeNode): boolean {
		if (typeof n.e !== typeof this.e) {
			return false;
		}
		return (n.e.toString() === this.e.toString());
	}

	/*
	* Add a child node 
	*/
	add(e: any): TreeNode {
		let n = new TreeNode(e);
		n.n = 1;
		return this.addNode(n);
	}
	addNode(n: TreeNode): TreeNode {
		n.p = this;
		this.c.push(n);
		this.updateN(n.size());
		return n;
	}
	addToPath(e: any, p: string): TreeNode {
		let n = new TreeNode(e);
		return this.addNodeToPath(n, p);
	}
	addNodeToPath(n: TreeNode, p: string): TreeNode {
		// Place holder
		return n;
	}

	/*
	* Delete child node 
	*/
	del(e: any): boolean { return true; }
	delNode(n: TreeNode): boolean {
		this.updateN(-n.size());
		return true;
	}
	delToPath(p: string): boolean {
		// Place holder
		return true;
	}
	delAll(): void {
		this.c = [];
	}

	/*
	* Get full path of this node 
	*/
	get(path: string): TreeNode {
		let node: TreeNode = undefined;
		return node;
	}

	size(): number {
		return this.n;
	}

	updateN(n: number) {
		this.n += n;
		if (this.p) {
			this.p.updateN(n);
		}
	}

	log() {
		this.printTree(this, 0);
	}
	private printTree(n: TreeNode, t: number) {
		let tab = '';
		for (let i = 0; i < t; i++) {
			tab += '\t';
		}
		// Print myself
		console.log(`${tab}${n.e}(${n.size()})`);
		// Print children
		if (n.c) {
			n.c.forEach(j => this.printTree(j, t + 1));
		}
	}
}
#Angular2 Tree Node

A simple recursive tree node component.