Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 67c18f5

Browse files
committed
chapter 10: trees
1 parent b4d028b commit 67c18f5

20 files changed

+1208
-0
lines changed

‎src/10-tree/01-bst.js‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// src/10-tree/01-bst.js
2+
const BinarySearchTree = require('./binary-search-tree');
3+
4+
class Student {
5+
constructor(idNumber, name, gradeLevel, address) {
6+
this.idNumber = idNumber;
7+
this.name = name;
8+
this.gradeLevel = gradeLevel;
9+
}
10+
}
11+
12+
// create student comparator to compare idNumber
13+
const studentComparator = (a, b) => a.idNumber - b.idNumber;
14+
15+
const studentTree = new BinarySearchTree(studentComparator);
16+
17+
studentTree.insert(new Student(11, 'Darcy', 10));
18+
studentTree.insert(new Student(7, 'Tory', 10));
19+
studentTree.insert(new Student(5, 'Caleb', 10));
20+
studentTree.insert(new Student(9, 'Sofia', 10));
21+
studentTree.insert(new Student(15, 'Max', 10));
22+
23+
// 11
24+
// / \
25+
// 7 15
26+
// / \
27+
// 5 9
28+
29+
studentTree.insert(new Student(12, 'Seth', 10));
30+
31+
// 11
32+
// / \
33+
// 7 15
34+
// / \ /
35+
// 5 9 12
36+
37+
38+
// to see the output of this file use the command: node src/10-tree/01-bst.js

‎src/10-tree/02-in-order-traversal.js‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// src/10-tree/02-in-order-traversal.js
2+
const BinarySearchTree = require('./binary-search-tree');
3+
4+
class Student {
5+
constructor(idNumber, firstName, lastName) {
6+
this.idNumber = idNumber;
7+
this.lastName = lastName;
8+
this.firstName = firstName;
9+
}
10+
}
11+
12+
// create student comparator to compare lastName using localCompare
13+
const studentComparator = (a, b) => a.lastName.localeCompare(b.lastName);
14+
15+
const studentTree = new BinarySearchTree(studentComparator);
16+
17+
studentTree.insert(new Student(9, 'Sofia', 'Cygnus'));
18+
studentTree.insert(new Student(12, 'Seth', 'Capella'));
19+
studentTree.insert(new Student(11, 'Darcy', 'Vega'));
20+
studentTree.insert(new Student(7, 'Tory', 'Vega'));
21+
studentTree.insert(new Student(5, 'Caleb', 'Altair'));
22+
studentTree.insert(new Student(15, 'Max', 'Rigel'));
23+
24+
25+
// in order traversal
26+
const classRoster = [];
27+
const addToRoster = (studentData) => {
28+
classRoster.push(`${studentData.idNumber}: ${studentData.lastName}, ${studentData.firstName}`);
29+
}
30+
studentTree.inOrderTraverse(addToRoster);
31+
32+
console.log(classRoster);
33+
// [
34+
// '5: Altair, Caleb',
35+
// '12: Capella, Seth',
36+
// '9: Cygnus, Sofia',
37+
// '15: Rigel, Max',
38+
// '11: Vega, Darcy',
39+
// '7: Vega, Tory'
40+
// ]
41+
42+
// to see the output of this file use the command: node src/10-tree/02-in-order-traversal.js
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// src/10-tree/03-pre-order-traversal.js
2+
const BinarySearchTree = require('./binary-search-tree');
3+
4+
class Employee {
5+
constructor(id, name, title) {
6+
this.id = id;
7+
this.name = name;
8+
this.title = title;
9+
}
10+
}
11+
12+
// create title constant
13+
const TITLE = {
14+
CEO: 1,
15+
VP: 2,
16+
MANAGER: 3,
17+
STAFF: 4
18+
}
19+
20+
// create employee comparator to compare title
21+
const employeeComparator = (a, b) => a.id - b.id;
22+
23+
const employeeTree = new BinarySearchTree(employeeComparator);
24+
25+
employeeTree.insert(new Employee(10, 'Gandalf', TITLE.CEO));
26+
employeeTree.insert(new Employee(5, 'Frodo', TITLE.VP));
27+
employeeTree.insert(new Employee(3,'Legolas', TITLE.MANAGER));
28+
employeeTree.insert(new Employee(1, 'Aragorn', TITLE.STAFF));
29+
employeeTree.insert(new Employee(4, 'Gimli', TITLE.STAFF));
30+
31+
employeeTree.insert(new Employee(14, 'Arya', TITLE.VP));
32+
employeeTree.insert(new Employee(12, 'John', TITLE.MANAGER));
33+
employeeTree.insert(new Employee(11, 'Brienne', TITLE.STAFF));
34+
employeeTree.insert(new Employee(13, 'Tyrion', TITLE.STAFF));
35+
36+
// pre order traversal
37+
const sendEmergencyNotification = (employee, message) => {
38+
console.log(`Notifying ${employee.name}: ${message}`);
39+
}
40+
const emergencyMessage = 'Tornado warning in the area. Seek shelter immediately!';
41+
employeeTree.preOrderTraverse((node) => sendEmergencyNotification(node, emergencyMessage));
42+
43+
// Notifying Gandalf: Tornado warning in the area. Seek shelter immediately!
44+
// Notifying Frodo: Tornado warning in the area. Seek shelter immediately!
45+
// Notifying Legolas: Tornado warning in the area. Seek shelter immediately!
46+
// Notifying Arya: Tornado warning in the area. Seek shelter immediately!
47+
// Notifying Aragorn: Tornado warning in the area. Seek shelter immediately!
48+
// Notifying John: Tornado warning in the area. Seek shelter immediately!
49+
// Notifying Gimli: Tornado warning in the area. Seek shelter immediately!
50+
// Notifying Brienne: Tornado warning in the area. Seek shelter immediately!
51+
// Notifying Tyrion: Tornado warning in the area. Seek shelter immediately!
52+
53+
// to see the output of this file use the command: node src/10-tree/03-pre-order-traversal.js
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// src/10-tree/04-post-order-traversal.js
2+
const BinarySearchTree = require('./binary-search-tree');
3+
4+
class FileOrDirectory {
5+
constructor(name, isDirectory, size = 0) {
6+
this.name = name;
7+
this.isDirectory = isDirectory; // true for directory, false for file
8+
}
9+
}
10+
const fileDirectoryComparator = (a, b) => a.name.localeCompare(b.name);
11+
12+
const fileSystemTree = new BinarySearchTree(fileDirectoryComparator);
13+
fileSystemTree.insert(new FileOrDirectory('Project', true));
14+
fileSystemTree.insert(new FileOrDirectory('Documents', true));
15+
fileSystemTree.insert(new FileOrDirectory('Code', true));
16+
fileSystemTree.insert(new FileOrDirectory('notes.txt', false));
17+
fileSystemTree.insert(new FileOrDirectory('design.pdf', false));
18+
fileSystemTree.insert(new FileOrDirectory('app.js', false));
19+
20+
21+
// post order traversal
22+
const deleteFileOrDirectory = (fileDirectory) => {
23+
console.log(`Deleting ${fileDirectory.name}`);
24+
}
25+
fileSystemTree.postOrderTraverse(deleteFileOrDirectory);
26+
27+
// Deleting app.js
28+
// Deleting design.pdf
29+
// Deleting Code
30+
// Deleting notes.txt
31+
// Deleting Documents
32+
// Deleting Project
33+
34+
// to see the output of this file use the command: node src/10-tree/04-post-order-traversal.js

‎src/10-tree/05-avl.js‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// src/10-tree/05-avl.js
2+
3+
const AVLTree = require('./avl-tree');
4+
5+
class Student {
6+
constructor(idNumber, name, gradeLevel, address) {
7+
this.idNumber = idNumber;
8+
this.name = name;
9+
this.gradeLevel = gradeLevel;
10+
}
11+
toString() {
12+
return `${this.idNumber} - ${this.name}`;
13+
}
14+
}
15+
16+
// create student comparator to compare idNumber
17+
const studentComparator = (a, b) => a.idNumber - b.idNumber;
18+
19+
const studentTree = new AVLTree(studentComparator);
20+
21+
studentTree.insert(new Student(11, 'Darcy', 10));
22+
studentTree.insert(new Student(7, 'Tory', 10));
23+
studentTree.insert(new Student(5, 'Caleb', 10));
24+
studentTree.insert(new Student(9, 'Sofia', 10));
25+
studentTree.insert(new Student(15, 'Max', 10));
26+
studentTree.insert(new Student(13, 'Geraldine', 10));
27+
studentTree.insert(new Student(12, 'Seth', 10));
28+
29+
30+
// 11
31+
// / \
32+
// 7 13
33+
// / \ / \
34+
// 5 9 12 15
35+
36+
// to see the output of this file use the command: node src/10-tree/05-avl.js

‎src/10-tree/06-red-black.js‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// src/10-tree/06-red-black.js
2+
3+
const RedBlackTree = require('./red-black-tree');
4+
5+
class Student {
6+
constructor(idNumber, name, gradeLevel, address) {
7+
this.idNumber = idNumber;
8+
this.name = name;
9+
this.gradeLevel = gradeLevel;
10+
}
11+
toString() {
12+
return `${this.idNumber} - ${this.name}`;
13+
}
14+
}
15+
16+
// create student comparator to compare idNumber
17+
const studentComparator = (a, b) => a.idNumber - b.idNumber;
18+
19+
const studentTree = new RedBlackTree(studentComparator);
20+
21+
studentTree.insert(new Student(11, 'Darcy', 10));
22+
studentTree.insert(new Student(7, 'Tory', 10));
23+
studentTree.insert(new Student(5, 'Caleb', 10));
24+
studentTree.insert(new Student(9, 'Sofia', 10));
25+
studentTree.insert(new Student(15, 'Max', 10));
26+
studentTree.insert(new Student(13, 'Geraldine', 10));
27+
studentTree.insert(new Student(12, 'Seth', 10));
28+
29+
studentTree.print();
30+
31+
// console.log('--- Removing 7');
32+
// studentTree.remove(new Student(7, 'Tory', 10));
33+
// studentTree.print();
34+
35+
// to see the output of this file use the command: node src/10-tree/06-red-black.js

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /