Skip to content

Commit e761506

Browse files
committed
tree
1 parent ce3e517 commit e761506

File tree

8 files changed

+105
-0
lines changed

8 files changed

+105
-0
lines changed

examples/chapter10/inorder.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script src="inorder.js"></script>
9+
</body>
10+
</html>

examples/chapter10/inorder.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const { Binarysearchtree } = PacktDataStructuresAlgorithms;
2+
const tree = new Binarysearchtree();
3+
tree.insert(11);
4+
tree.insert(7);
5+
tree.insert(15);
6+
tree.insert(5);
7+
tree.insert(3);
8+
tree.insert(9);
9+
tree.insert(8);
10+
tree.insert(10);
11+
tree.insert(13);
12+
tree.insert(12);
13+
tree.insert(14);
14+
tree.insert(20);
15+
tree.insert(18);
16+
tree.insert(25);
17+
console.log(tree);

examples/chapter10/postorder.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script src="01-IntroRecursion.js"></script>
9+
</body>
10+
</html>

examples/chapter10/postorder.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
let i = 0;
2+
function recursiveFn() {
3+
i++;
4+
recursiveFn();
5+
}
6+
7+
try {
8+
recursiveFn();
9+
} catch (ex) {
10+
console.log(`i = ${i} error: ${ex}`);
11+
}

examples/chapter10/preorder.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script src="04-Fibonacci.js"></script>
9+
</body>
10+
</html>

examples/chapter10/preorder.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
function fibonacci(n) {
2+
if (n < 1) return 0; // {1}
3+
if (n <= 2) return 1; // {2}
4+
return fibonacci(n - 1) + fibonacci(n - 2); // {3}
5+
}
6+
7+
console.log('fibonacci(2)', fibonacci(2));
8+
console.log('fibonacci(3)', fibonacci(3));
9+
console.log('fibonacci(4)', fibonacci(4));
10+
console.log('fibonacci(5)', fibonacci(5));
11+
12+
function fibonacciIterative(n) {
13+
let fibNMinus2 = 0;
14+
let fibNMinus1 = 1;
15+
let fibN = n;
16+
for (let i = 2; i <= n; i++) { // n >= 2
17+
fibN = fibNMinus1 + fibNMinus2; // f(n-1) + f(n-2)
18+
fibNMinus2 = fibNMinus1;
19+
fibNMinus1 = fibN;
20+
}
21+
return fibN;
22+
}
23+
24+
console.log('fibonacciIterative(2)', fibonacciIterative(2));
25+
console.log('fibonacciIterative(3)', fibonacciIterative(3));
26+
console.log('fibonacciIterative(4)', fibonacciIterative(4));
27+
console.log('fibonacciIterative(5)', fibonacciIterative(5));
28+
29+
function fibonacciMemoization(n) {
30+
const memo = [0, 1];
31+
const fibonacci = (n) => {
32+
if (memo[n] != null) return memo[n];
33+
return memo[n] = fibonacci(n - 1, memo) + fibonacci(n - 2, memo);
34+
};
35+
return fibonacci(n);
36+
}
37+
38+
console.log('fibonacciMemoization(2)', fibonacciMemoization(2));
39+
console.log('fibonacciMemoization(3)', fibonacciMemoization(3));
40+
console.log('fibonacciMemoization(4)', fibonacciMemoization(4));
41+
console.log('fibonacciMemoization(5)', fibonacciMemoization(5));
42+
43+
// https://jsperf.com/fibonacci-comparison-jsbook

examples/index.html

+3
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@
198198
<div class="page-content mdl-layout--fixed-drawer">
199199
<div class="mdl-layout__drawer is-visible">
200200
<nav class="mdl-navigation">
201+
<a class="mdl-navigation__link" href="chapter10/inorder.html">中序遍历 </a>
202+
<a class="mdl-navigation__link" href="chapter10/preorder.html">前序遍历 </a>
203+
<a class="mdl-navigation__link" href="chapter10/postorder.html">后序遍历 </a>
201204
</nav>
202205
</div>
203206
</div>

src/js/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export { default as AVLTree } from './data-structures/avl-tree';
4949
// chapter 10
5050
export { MinHeap } from './data-structures/heap';
5151
export { MaxHeap } from './data-structures/heap';
52+
// export { default as Binarysearchtree } from './data-structures/binary-search-tree';
5253
export { default as heapSort } from './algorithms/sorting/heap-sort';
5354

5455
// chapter 11

0 commit comments

Comments
 (0)