Skip to content

Commit 7d5a53c

Browse files
committed
New JavaScript examples
1 parent 96a40c7 commit 7d5a53c

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

JavaScript/2-five.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
function Tree(data) {
4+
this.count = 1;
5+
this.root = new Node(null, data);
6+
this.root.tree = this;
7+
}
8+
9+
function Node(parent, data) {
10+
this.data = data;
11+
this.parent = parent;
12+
this.count = 0;
13+
if (parent) {
14+
this.tree = parent.tree;
15+
this.tree.count++;
16+
if (parent.count < 1) {
17+
parent.first = this;
18+
}
19+
if (parent.last) {
20+
parent.last.next = this;
21+
this.prev = parent.last;
22+
}
23+
parent.last = this;
24+
parent.count++;
25+
}
26+
this.prev = null;
27+
this.next = null;
28+
this.first = null;
29+
this.last = null;
30+
}
31+
32+
let tree = new Tree({ name: 'root' });
33+
let n1 = new Node(tree.root, { name: 'n1' });
34+
let n2 = new Node(tree.root, { name: 'n2' });
35+
let n3 = new Node(tree.root, { name: 'n3' });
36+
37+
console.dir(tree, { depth: null });

JavaScript/3-native.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
function Node(parent, name) {
4+
this.name = name;
5+
if (parent) {
6+
this.parent = parent;
7+
parent[name] = this;
8+
}
9+
}
10+
11+
let root = new Node(null, 'root');
12+
let n1 = new Node(root, 'n1' );
13+
let n2 = new Node(root, 'n2' );
14+
let n3 = new Node(root, 'n3' );
15+
16+
console.dir(root, { depth: null });

0 commit comments

Comments
 (0)