avl-tree-typed 1.19.5 → 1.19.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +204 -10
- package/dist/avl-tree.d.ts +86 -0
- package/dist/avl-tree.js +337 -0
- package/dist/index.d.ts +1 -8
- package/dist/index.js +15 -11
- package/package.json +2 -2
- package/tsconfig.json +4 -6
package/README.md
CHANGED
|
@@ -1,30 +1,180 @@
|
|
|
1
1
|
# What
|
|
2
|
+
|
|
2
3
|
## Brief
|
|
3
|
-
|
|
4
|
+
Javascript & TypeScript Data Structure Library.
|
|
5
|
+
|
|
6
|
+
Binary Tree, Binary Search Tree (BST), AVL Tree, Tree Multiset, Segment Tree, Binary Indexed Tree, Graph, Directed Graph, Undirected Graph, Linked List, Singly Linked List, Doubly Linked List, Queue, Object Deque, Array Deque, Stack, Hash, Coordinate Set, Coordinate Map, Heap, Priority Queue, Max Priority Queue, Min Priority Queue, Trie
|
|
7
|
+
|
|
8
|
+
## Algorithms list only a few out, you can discover more in API docs
|
|
4
9
|
|
|
10
|
+
DFS, DFSIterative, BFS, morris, Bellman-Ford Algorithm, Dijkstra's Algorithm, Floyd-Warshall Algorithm, Tarjan's Algorithm
|
|
11
|
+
|
|
12
|
+
## Code design
|
|
13
|
+
By strictly adhering to object-oriented design (BinaryTree -> BST -> AVLTree -> TreeMultiset), you can seamlessly inherit the existing data structures to implement the customized ones you need. Object-oriented design stands as the optimal approach to data structure design.
|
|
5
14
|
|
|
6
15
|
# How
|
|
7
16
|
|
|
8
17
|
## install
|
|
9
|
-
###
|
|
18
|
+
### yarn
|
|
19
|
+
|
|
10
20
|
```bash
|
|
11
|
-
|
|
21
|
+
yarn add data-structure-typed
|
|
12
22
|
```
|
|
13
|
-
|
|
23
|
+
|
|
24
|
+
### npm
|
|
25
|
+
|
|
14
26
|
```bash
|
|
15
|
-
|
|
27
|
+
npm install data-structure-typed
|
|
16
28
|
```
|
|
17
29
|
|
|
18
|
-
### snippet
|
|
30
|
+
### Binary Search Tree (BST) snippet
|
|
31
|
+
|
|
19
32
|
#### TS
|
|
20
33
|
```typescript
|
|
34
|
+
import {BST, BSTNode} from 'data-structure-typed';
|
|
35
|
+
|
|
36
|
+
const bst = new BST();
|
|
37
|
+
bst.add(11);
|
|
38
|
+
bst.add(3);
|
|
39
|
+
bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
40
|
+
bst.size === 16; // true
|
|
41
|
+
bst.has(6); // true
|
|
42
|
+
const node6 = bst.get(6);
|
|
43
|
+
bst.getHeight(6) === 2; // true
|
|
44
|
+
bst.getHeight() === 5; // true
|
|
45
|
+
bst.getDepth(6) === 3; // true
|
|
46
|
+
const leftMost = bst.getLeftMost();
|
|
47
|
+
leftMost?.id === 1; // true
|
|
48
|
+
expect(leftMost?.id).toBe(1);
|
|
49
|
+
bst.remove(6);
|
|
50
|
+
bst.get(6); // null
|
|
51
|
+
bst.isAVLBalanced(); // true or false
|
|
52
|
+
const bfsIDs = bst.BFS();
|
|
53
|
+
bfsIDs[0] === 11; // true
|
|
54
|
+
expect(bfsIDs[0]).toBe(11);
|
|
55
|
+
|
|
56
|
+
const objBST = new BST<BSTNode<{ id: number, keyA: number }>>();
|
|
57
|
+
objBST.add(11, {id: 11, keyA: 11});
|
|
58
|
+
objBST.add(3, {id: 3, keyA: 3});
|
|
59
|
+
|
|
60
|
+
objBST.addMany([{id: 15, keyA: 15}, {id: 1, keyA: 1}, {id: 8, keyA: 8},
|
|
61
|
+
{id: 13, keyA: 13}, {id: 16, keyA: 16}, {id: 2, keyA: 2},
|
|
62
|
+
{id: 6, keyA: 6}, {id: 9, keyA: 9}, {id: 12, keyA: 12},
|
|
63
|
+
{id: 14, keyA: 14}, {id: 4, keyA: 4}, {id: 7, keyA: 7},
|
|
64
|
+
{id: 10, keyA: 10}, {id: 5, keyA: 5}]);
|
|
65
|
+
|
|
66
|
+
objBST.remove(11);
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
const avlTree = new AVLTree();
|
|
70
|
+
avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
|
|
71
|
+
avlTree.isAVLBalanced(); // true
|
|
72
|
+
avlTree.remove(10);
|
|
73
|
+
avlTree.isAVLBalanced(); // true
|
|
21
74
|
|
|
22
75
|
```
|
|
23
76
|
#### JS
|
|
24
77
|
```javascript
|
|
78
|
+
const {BST, BSTNode} = require('data-structure-typed');
|
|
79
|
+
|
|
80
|
+
const bst = new BST();
|
|
81
|
+
bst.add(11);
|
|
82
|
+
bst.add(3);
|
|
83
|
+
bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
84
|
+
bst.size === 16; // true
|
|
85
|
+
bst.has(6); // true
|
|
86
|
+
const node6 = bst.get(6);
|
|
87
|
+
bst.getHeight(6) === 2; // true
|
|
88
|
+
bst.getHeight() === 5; // true
|
|
89
|
+
bst.getDepth(6) === 3; // true
|
|
90
|
+
const leftMost = bst.getLeftMost();
|
|
91
|
+
leftMost?.id === 1; // true
|
|
92
|
+
expect(leftMost?.id).toBe(1);
|
|
93
|
+
bst.remove(6);
|
|
94
|
+
bst.get(6); // null
|
|
95
|
+
bst.isAVLBalanced(); // true or false
|
|
96
|
+
const bfsIDs = bst.BFS();
|
|
97
|
+
bfsIDs[0] === 11; // true
|
|
98
|
+
expect(bfsIDs[0]).toBe(11);
|
|
99
|
+
|
|
100
|
+
const objBST = new BST();
|
|
101
|
+
objBST.add(11, {id: 11, keyA: 11});
|
|
102
|
+
objBST.add(3, {id: 3, keyA: 3});
|
|
103
|
+
|
|
104
|
+
objBST.addMany([{id: 15, keyA: 15}, {id: 1, keyA: 1}, {id: 8, keyA: 8},
|
|
105
|
+
{id: 13, keyA: 13}, {id: 16, keyA: 16}, {id: 2, keyA: 2},
|
|
106
|
+
{id: 6, keyA: 6}, {id: 9, keyA: 9}, {id: 12, keyA: 12},
|
|
107
|
+
{id: 14, keyA: 14}, {id: 4, keyA: 4}, {id: 7, keyA: 7},
|
|
108
|
+
{id: 10, keyA: 10}, {id: 5, keyA: 5}]);
|
|
109
|
+
|
|
110
|
+
objBST.remove(11);
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
const avlTree = new AVLTree();
|
|
114
|
+
avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
|
|
115
|
+
avlTree.isAVLBalanced(); // true
|
|
116
|
+
avlTree.remove(10);
|
|
117
|
+
avlTree.isAVLBalanced(); // true
|
|
118
|
+
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Directed Graph simple snippet
|
|
25
122
|
|
|
123
|
+
#### TS or JS
|
|
124
|
+
```typescript
|
|
125
|
+
import {DirectedGraph} from 'data-structure-typed';
|
|
126
|
+
|
|
127
|
+
const graph = new DirectedGraph();
|
|
128
|
+
|
|
129
|
+
graph.addVertex('A');
|
|
130
|
+
graph.addVertex('B');
|
|
131
|
+
|
|
132
|
+
graph.hasVertex('A'); // true
|
|
133
|
+
graph.hasVertex('B'); // true
|
|
134
|
+
graph.hasVertex('C'); // false
|
|
135
|
+
|
|
136
|
+
graph.addEdge('A', 'B');
|
|
137
|
+
graph.hasEdge('A', 'B'); // true
|
|
138
|
+
graph.hasEdge('B', 'A'); // false
|
|
139
|
+
|
|
140
|
+
graph.removeEdgeSrcToDest('A', 'B');
|
|
141
|
+
graph.hasEdge('A', 'B'); // false
|
|
142
|
+
|
|
143
|
+
graph.addVertex('C');
|
|
144
|
+
|
|
145
|
+
graph.addEdge('A', 'B');
|
|
146
|
+
graph.addEdge('B', 'C');
|
|
147
|
+
|
|
148
|
+
const topologicalOrderIds = graph.topologicalSort(); // ['A', 'B', 'C']
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### Undirected Graph snippet
|
|
152
|
+
|
|
153
|
+
#### TS or JS
|
|
154
|
+
```typescript
|
|
155
|
+
import {UndirectedGraph} from 'data-structure-typed';
|
|
156
|
+
|
|
157
|
+
const graph = new UndirectedGraph();
|
|
158
|
+
graph.addVertex('A');
|
|
159
|
+
graph.addVertex('B');
|
|
160
|
+
graph.addVertex('C');
|
|
161
|
+
graph.addVertex('D');
|
|
162
|
+
graph.removeVertex('C');
|
|
163
|
+
graph.addEdge('A', 'B');
|
|
164
|
+
graph.addEdge('B', 'D');
|
|
165
|
+
|
|
166
|
+
const dijkstraResult = graph.dijkstra('A');
|
|
167
|
+
Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.id) // ['A', 'B', 'D']
|
|
26
168
|
```
|
|
27
169
|
|
|
170
|
+

|
|
171
|
+
|
|
172
|
+

|
|
173
|
+
|
|
174
|
+

|
|
175
|
+
|
|
176
|
+

|
|
177
|
+
|
|
28
178
|
|
|
29
179
|
## API docs & Examples
|
|
30
180
|
|
|
@@ -32,6 +182,10 @@ yarn add avl-tree-typed
|
|
|
32
182
|
|
|
33
183
|
[Live Examples](https://data-structure-typed-examples.vercel.app)
|
|
34
184
|
|
|
185
|
+
<a href="https://data-structure-typed-examples.vercel.app" target="_blank">Live Examples</a>
|
|
186
|
+
|
|
187
|
+
[//]: # ([Examples Repository](https://github.com/zrwusa/data-structure-typed-examples))
|
|
188
|
+
|
|
35
189
|
<a href="https://github.com/zrwusa/data-structure-typed-examples" target="_blank">Examples Repository</a>
|
|
36
190
|
|
|
37
191
|
## Data Structures
|
|
@@ -49,10 +203,10 @@ yarn add avl-tree-typed
|
|
|
49
203
|
<tbody>
|
|
50
204
|
<tr>
|
|
51
205
|
<td>Binary Tree</td>
|
|
52
|
-
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""
|
|
53
|
-
</td>
|
|
54
|
-
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""
|
|
55
|
-
</td>
|
|
206
|
+
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt="">
|
|
207
|
+
</img></td>
|
|
208
|
+
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt="">
|
|
209
|
+
</img></td>
|
|
56
210
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/BinaryTree.html"><span>Binary Tree</span></a></td>
|
|
57
211
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
58
212
|
</tr>
|
|
@@ -499,6 +653,46 @@ yarn add avl-tree-typed
|
|
|
499
653
|
|
|
500
654
|

|
|
501
655
|
|
|
656
|
+
[//]: # ()
|
|
657
|
+
|
|
658
|
+
[//]: # ()
|
|
659
|
+
[//]: # ()
|
|
660
|
+
|
|
661
|
+
[//]: # ()
|
|
662
|
+
[//]: # ()
|
|
663
|
+
|
|
664
|
+
[//]: # ()
|
|
665
|
+
[//]: # ()
|
|
666
|
+
|
|
667
|
+
[//]: # ()
|
|
668
|
+
[//]: # ()
|
|
669
|
+
|
|
670
|
+
[//]: # ()
|
|
671
|
+
[//]: # ()
|
|
672
|
+
|
|
673
|
+
[//]: # ()
|
|
674
|
+
[//]: # ()
|
|
675
|
+
|
|
676
|
+
[//]: # ()
|
|
677
|
+
[//]: # ()
|
|
678
|
+
|
|
679
|
+
[//]: # ()
|
|
680
|
+
[//]: # ()
|
|
681
|
+
|
|
682
|
+
[//]: # ()
|
|
683
|
+
[//]: # ()
|
|
684
|
+
|
|
685
|
+
[//]: # ()
|
|
686
|
+
[//]: # ()
|
|
687
|
+
|
|
688
|
+
[//]: # ()
|
|
689
|
+
[//]: # ()
|
|
690
|
+
|
|
691
|
+
[//]: # ()
|
|
692
|
+
|
|
693
|
+
[//]: # ()
|
|
694
|
+
|
|
695
|
+
[//]: # ()
|
|
502
696
|
|
|
503
697
|
|
|
504
698
|
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
import type { AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeId } from 'data-structure-typed';
|
|
9
|
+
import { BST, BSTNode, IAVLTree, IAVLTreeNode } from 'data-structure-typed';
|
|
10
|
+
export declare class AVLTreeNode<T = any, NEIGHBOR extends AVLTreeNode<T, NEIGHBOR> = AVLTreeNodeNested<T>> extends BSTNode<T, NEIGHBOR> implements IAVLTreeNode<T, NEIGHBOR> {
|
|
11
|
+
}
|
|
12
|
+
export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends BST<N> implements IAVLTree<N> {
|
|
13
|
+
/**
|
|
14
|
+
* This is a constructor function for an AVL tree data structure in TypeScript.
|
|
15
|
+
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
16
|
+
* constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
|
|
17
|
+
* options.
|
|
18
|
+
*/
|
|
19
|
+
constructor(options?: AVLTreeOptions);
|
|
20
|
+
/**
|
|
21
|
+
* The function creates a new AVL tree node with the given id and value.
|
|
22
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
23
|
+
* identify each node in the tree.
|
|
24
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
|
|
25
|
+
* that will be stored in the node.
|
|
26
|
+
* @returns a new AVLTreeNode object with the specified id and value.
|
|
27
|
+
*/
|
|
28
|
+
createNode(id: BinaryTreeNodeId, val?: N['val']): N;
|
|
29
|
+
/**
|
|
30
|
+
* The function overrides the add method of a binary tree node and balances the tree after inserting a new node.
|
|
31
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add.
|
|
32
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It is of type
|
|
33
|
+
* `N['val']`, which means it should be of the same type as the `val` property of the nodes in the binary tree.
|
|
34
|
+
* @returns The method is returning the inserted node, or null or undefined if the insertion was not successful.
|
|
35
|
+
*/
|
|
36
|
+
add(id: BinaryTreeNodeId, val?: N['val']): N | null | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* The function overrides the remove method of the Binary Search Tree class, performs the removal operation, and
|
|
39
|
+
* then balances the tree if necessary.
|
|
40
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node that needs to be
|
|
41
|
+
* removed from the AVL tree.
|
|
42
|
+
* @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean parameter that
|
|
43
|
+
* determines whether the left sum of all nodes in the AVL tree should be updated after removing a node. If
|
|
44
|
+
* `isUpdateAllLeftSum` is set to `true`, the left sum of all nodes will be recalculated.
|
|
45
|
+
* @returns The method is returning an array of `AVLTreeDeleted<N>` objects.
|
|
46
|
+
*/
|
|
47
|
+
remove(id: BinaryTreeNodeId, isUpdateAllLeftSum?: boolean): BinaryTreeDeletedResult<N>[];
|
|
48
|
+
/**
|
|
49
|
+
* The balance factor of a given AVL tree node is calculated by subtracting the height of its left subtree from the
|
|
50
|
+
* height of its right subtree.
|
|
51
|
+
* @param node - The parameter "node" is of type N, which represents a node in an AVL tree.
|
|
52
|
+
* @returns The balance factor of the given AVL tree node.
|
|
53
|
+
*/
|
|
54
|
+
balanceFactor(node: N): number;
|
|
55
|
+
/**
|
|
56
|
+
* The function updates the height of a node in an AVL tree based on the heights of its left and right subtrees.
|
|
57
|
+
* @param node - The parameter `node` is an AVLTreeNode object, which represents a node in an AVL tree.
|
|
58
|
+
*/
|
|
59
|
+
updateHeight(node: N): void;
|
|
60
|
+
/**
|
|
61
|
+
* The `balancePath` function balances the AVL tree by performing appropriate rotations based on the balance factor of
|
|
62
|
+
* each node in the path from the given node to the root.
|
|
63
|
+
* @param node - The `node` parameter is an AVLTreeNode object, which represents a node in an AVL tree.
|
|
64
|
+
*/
|
|
65
|
+
balancePath(node: N): void;
|
|
66
|
+
/**
|
|
67
|
+
* The `balanceLL` function performs a left-left rotation on an AVL tree to balance it.
|
|
68
|
+
* @param A - The parameter A is an AVLTreeNode object.
|
|
69
|
+
*/
|
|
70
|
+
balanceLL(A: N): void;
|
|
71
|
+
/**
|
|
72
|
+
* The `balanceLR` function performs a left-right rotation to balance an AVL tree.
|
|
73
|
+
* @param A - A is an AVLTreeNode object.
|
|
74
|
+
*/
|
|
75
|
+
balanceLR(A: N): void;
|
|
76
|
+
/**
|
|
77
|
+
* The `balanceRR` function performs a right-right rotation on an AVL tree to balance it.
|
|
78
|
+
* @param A - The parameter A is an AVLTreeNode object.
|
|
79
|
+
*/
|
|
80
|
+
balanceRR(A: N): void;
|
|
81
|
+
/**
|
|
82
|
+
* The `balanceRL` function performs a right-left rotation to balance an AVL tree.
|
|
83
|
+
* @param A - A is an AVLTreeNode object.
|
|
84
|
+
*/
|
|
85
|
+
balanceRL(A: N): void;
|
|
86
|
+
}
|
package/dist/avl-tree.js
ADDED
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
var __values = (this && this.__values) || function(o) {
|
|
18
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
19
|
+
if (m) return m.call(o);
|
|
20
|
+
if (o && typeof o.length === "number") return {
|
|
21
|
+
next: function () {
|
|
22
|
+
if (o && i >= o.length) o = void 0;
|
|
23
|
+
return { value: o && o[i++], done: !o };
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.AVLTree = exports.AVLTreeNode = void 0;
|
|
30
|
+
var data_structure_typed_1 = require("data-structure-typed");
|
|
31
|
+
var AVLTreeNode = /** @class */ (function (_super) {
|
|
32
|
+
__extends(AVLTreeNode, _super);
|
|
33
|
+
function AVLTreeNode() {
|
|
34
|
+
return _super !== null && _super.apply(this, arguments) || this;
|
|
35
|
+
}
|
|
36
|
+
return AVLTreeNode;
|
|
37
|
+
}(data_structure_typed_1.BSTNode));
|
|
38
|
+
exports.AVLTreeNode = AVLTreeNode;
|
|
39
|
+
var AVLTree = /** @class */ (function (_super) {
|
|
40
|
+
__extends(AVLTree, _super);
|
|
41
|
+
/**
|
|
42
|
+
* This is a constructor function for an AVL tree data structure in TypeScript.
|
|
43
|
+
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
44
|
+
* constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
|
|
45
|
+
* options.
|
|
46
|
+
*/
|
|
47
|
+
function AVLTree(options) {
|
|
48
|
+
return _super.call(this, options) || this;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The function creates a new AVL tree node with the given id and value.
|
|
52
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
53
|
+
* identify each node in the tree.
|
|
54
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
|
|
55
|
+
* that will be stored in the node.
|
|
56
|
+
* @returns a new AVLTreeNode object with the specified id and value.
|
|
57
|
+
*/
|
|
58
|
+
AVLTree.prototype.createNode = function (id, val) {
|
|
59
|
+
return new AVLTreeNode(id, val);
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* The function overrides the add method of a binary tree node and balances the tree after inserting a new node.
|
|
63
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add.
|
|
64
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It is of type
|
|
65
|
+
* `N['val']`, which means it should be of the same type as the `val` property of the nodes in the binary tree.
|
|
66
|
+
* @returns The method is returning the inserted node, or null or undefined if the insertion was not successful.
|
|
67
|
+
*/
|
|
68
|
+
AVLTree.prototype.add = function (id, val) {
|
|
69
|
+
var inserted = _super.prototype.add.call(this, id, val);
|
|
70
|
+
if (inserted)
|
|
71
|
+
this.balancePath(inserted);
|
|
72
|
+
return inserted;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* The function overrides the remove method of the Binary Search Tree class, performs the removal operation, and
|
|
76
|
+
* then balances the tree if necessary.
|
|
77
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node that needs to be
|
|
78
|
+
* removed from the AVL tree.
|
|
79
|
+
* @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean parameter that
|
|
80
|
+
* determines whether the left sum of all nodes in the AVL tree should be updated after removing a node. If
|
|
81
|
+
* `isUpdateAllLeftSum` is set to `true`, the left sum of all nodes will be recalculated.
|
|
82
|
+
* @returns The method is returning an array of `AVLTreeDeleted<N>` objects.
|
|
83
|
+
*/
|
|
84
|
+
AVLTree.prototype.remove = function (id, isUpdateAllLeftSum) {
|
|
85
|
+
var e_1, _a;
|
|
86
|
+
var deletedResults = _super.prototype.remove.call(this, id, isUpdateAllLeftSum);
|
|
87
|
+
try {
|
|
88
|
+
for (var deletedResults_1 = __values(deletedResults), deletedResults_1_1 = deletedResults_1.next(); !deletedResults_1_1.done; deletedResults_1_1 = deletedResults_1.next()) {
|
|
89
|
+
var needBalanced = deletedResults_1_1.value.needBalanced;
|
|
90
|
+
if (needBalanced) {
|
|
91
|
+
this.balancePath(needBalanced);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
96
|
+
finally {
|
|
97
|
+
try {
|
|
98
|
+
if (deletedResults_1_1 && !deletedResults_1_1.done && (_a = deletedResults_1.return)) _a.call(deletedResults_1);
|
|
99
|
+
}
|
|
100
|
+
finally { if (e_1) throw e_1.error; }
|
|
101
|
+
}
|
|
102
|
+
return deletedResults;
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* The balance factor of a given AVL tree node is calculated by subtracting the height of its left subtree from the
|
|
106
|
+
* height of its right subtree.
|
|
107
|
+
* @param node - The parameter "node" is of type N, which represents a node in an AVL tree.
|
|
108
|
+
* @returns The balance factor of the given AVL tree node.
|
|
109
|
+
*/
|
|
110
|
+
AVLTree.prototype.balanceFactor = function (node) {
|
|
111
|
+
if (!node.right) // node has no right subtree
|
|
112
|
+
return -node.height;
|
|
113
|
+
else if (!node.left) // node has no left subtree
|
|
114
|
+
return +node.height;
|
|
115
|
+
else
|
|
116
|
+
return node.right.height - node.left.height;
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* The function updates the height of a node in an AVL tree based on the heights of its left and right subtrees.
|
|
120
|
+
* @param node - The parameter `node` is an AVLTreeNode object, which represents a node in an AVL tree.
|
|
121
|
+
*/
|
|
122
|
+
AVLTree.prototype.updateHeight = function (node) {
|
|
123
|
+
if (!node.left && !node.right) // node is a leaf
|
|
124
|
+
node.height = 0;
|
|
125
|
+
else if (!node.left) {
|
|
126
|
+
// node has no left subtree
|
|
127
|
+
var rightHeight = node.right ? node.right.height : 0;
|
|
128
|
+
node.height = 1 + rightHeight;
|
|
129
|
+
}
|
|
130
|
+
else if (!node.right) // node has no right subtree
|
|
131
|
+
node.height = 1 + node.left.height;
|
|
132
|
+
else
|
|
133
|
+
node.height = 1 + Math.max(node.right.height, node.left.height);
|
|
134
|
+
};
|
|
135
|
+
/**
|
|
136
|
+
* The `balancePath` function balances the AVL tree by performing appropriate rotations based on the balance factor of
|
|
137
|
+
* each node in the path from the given node to the root.
|
|
138
|
+
* @param node - The `node` parameter is an AVLTreeNode object, which represents a node in an AVL tree.
|
|
139
|
+
*/
|
|
140
|
+
AVLTree.prototype.balancePath = function (node) {
|
|
141
|
+
var path = this.getPathToRoot(node);
|
|
142
|
+
for (var i = path.length - 1; i >= 0; i--) {
|
|
143
|
+
var A = path[i];
|
|
144
|
+
this.updateHeight(A);
|
|
145
|
+
switch (this.balanceFactor(A)) {
|
|
146
|
+
case -2:
|
|
147
|
+
if (A && A.left) {
|
|
148
|
+
if (this.balanceFactor(A.left) <= 0) {
|
|
149
|
+
this.balanceLL(A); // Perform LL rotation
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
this.balanceLR(A); // Perform LR rotation
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
break;
|
|
156
|
+
case +2:
|
|
157
|
+
if (A && A.right) {
|
|
158
|
+
if (this.balanceFactor(A.right) >= 0) {
|
|
159
|
+
this.balanceRR(A); // Perform RR rotation
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
this.balanceRL(A); // Perform RL rotation
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
/**
|
|
169
|
+
* The `balanceLL` function performs a left-left rotation on an AVL tree to balance it.
|
|
170
|
+
* @param A - The parameter A is an AVLTreeNode object.
|
|
171
|
+
*/
|
|
172
|
+
AVLTree.prototype.balanceLL = function (A) {
|
|
173
|
+
var parentOfA = A.parent;
|
|
174
|
+
var B = A.left; // A is left-heavy and B is left-heavy
|
|
175
|
+
A.parent = B;
|
|
176
|
+
if (B && B.right) {
|
|
177
|
+
B.right.parent = A;
|
|
178
|
+
}
|
|
179
|
+
if (B)
|
|
180
|
+
B.parent = parentOfA;
|
|
181
|
+
if (A === this.root) {
|
|
182
|
+
if (B)
|
|
183
|
+
this._setRoot(B);
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
if ((parentOfA === null || parentOfA === void 0 ? void 0 : parentOfA.left) === A) {
|
|
187
|
+
parentOfA.left = B;
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
if (parentOfA)
|
|
191
|
+
parentOfA.right = B;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
if (B) {
|
|
195
|
+
A.left = B.right; // Make T2 the left subtree of A
|
|
196
|
+
B.right = A; // Make A the left child of B
|
|
197
|
+
}
|
|
198
|
+
this.updateHeight(A);
|
|
199
|
+
if (B)
|
|
200
|
+
this.updateHeight(B);
|
|
201
|
+
};
|
|
202
|
+
/**
|
|
203
|
+
* The `balanceLR` function performs a left-right rotation to balance an AVL tree.
|
|
204
|
+
* @param A - A is an AVLTreeNode object.
|
|
205
|
+
*/
|
|
206
|
+
AVLTree.prototype.balanceLR = function (A) {
|
|
207
|
+
var parentOfA = A.parent;
|
|
208
|
+
var B = A.left; // A is left-heavy
|
|
209
|
+
var C = null;
|
|
210
|
+
if (B) {
|
|
211
|
+
C = B.right; // B is right-heavy
|
|
212
|
+
}
|
|
213
|
+
if (A)
|
|
214
|
+
A.parent = C;
|
|
215
|
+
if (B)
|
|
216
|
+
B.parent = C;
|
|
217
|
+
if (C) {
|
|
218
|
+
if (C.left) {
|
|
219
|
+
C.left.parent = B;
|
|
220
|
+
}
|
|
221
|
+
if (C.right) {
|
|
222
|
+
C.right.parent = A;
|
|
223
|
+
}
|
|
224
|
+
C.parent = parentOfA;
|
|
225
|
+
}
|
|
226
|
+
if (A === this.root) {
|
|
227
|
+
if (C)
|
|
228
|
+
this._setRoot(C);
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
if (parentOfA) {
|
|
232
|
+
if (parentOfA.left === A) {
|
|
233
|
+
parentOfA.left = C;
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
parentOfA.right = C;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
if (C) {
|
|
241
|
+
A.left = C.right; // Make T3 the left subtree of A
|
|
242
|
+
if (B)
|
|
243
|
+
B.right = C.left; // Make T2 the right subtree of B
|
|
244
|
+
C.left = B;
|
|
245
|
+
C.right = A;
|
|
246
|
+
}
|
|
247
|
+
this.updateHeight(A); // Adjust heights
|
|
248
|
+
B && this.updateHeight(B);
|
|
249
|
+
C && this.updateHeight(C);
|
|
250
|
+
};
|
|
251
|
+
/**
|
|
252
|
+
* The `balanceRR` function performs a right-right rotation on an AVL tree to balance it.
|
|
253
|
+
* @param A - The parameter A is an AVLTreeNode object.
|
|
254
|
+
*/
|
|
255
|
+
AVLTree.prototype.balanceRR = function (A) {
|
|
256
|
+
var parentOfA = A.parent;
|
|
257
|
+
var B = A.right; // A is right-heavy and B is right-heavy
|
|
258
|
+
A.parent = B;
|
|
259
|
+
if (B) {
|
|
260
|
+
if (B.left) {
|
|
261
|
+
B.left.parent = A;
|
|
262
|
+
}
|
|
263
|
+
B.parent = parentOfA;
|
|
264
|
+
}
|
|
265
|
+
if (A === this.root) {
|
|
266
|
+
if (B)
|
|
267
|
+
this._setRoot(B);
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
if (parentOfA) {
|
|
271
|
+
if (parentOfA.left === A) {
|
|
272
|
+
parentOfA.left = B;
|
|
273
|
+
}
|
|
274
|
+
else {
|
|
275
|
+
parentOfA.right = B;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
if (B) {
|
|
280
|
+
A.right = B.left; // Make T2 the right subtree of A
|
|
281
|
+
B.left = A;
|
|
282
|
+
}
|
|
283
|
+
this.updateHeight(A);
|
|
284
|
+
B && this.updateHeight(B);
|
|
285
|
+
};
|
|
286
|
+
/**
|
|
287
|
+
* The `balanceRL` function performs a right-left rotation to balance an AVL tree.
|
|
288
|
+
* @param A - A is an AVLTreeNode object.
|
|
289
|
+
*/
|
|
290
|
+
AVLTree.prototype.balanceRL = function (A) {
|
|
291
|
+
var parentOfA = A.parent;
|
|
292
|
+
var B = A.right; // A is right-heavy
|
|
293
|
+
var C = null;
|
|
294
|
+
if (B) {
|
|
295
|
+
C = B.left; // B is left-heavy
|
|
296
|
+
}
|
|
297
|
+
A.parent = C;
|
|
298
|
+
if (B)
|
|
299
|
+
B.parent = C;
|
|
300
|
+
if (C) {
|
|
301
|
+
if (C.left) {
|
|
302
|
+
C.left.parent = A;
|
|
303
|
+
}
|
|
304
|
+
if (C.right) {
|
|
305
|
+
C.right.parent = B;
|
|
306
|
+
}
|
|
307
|
+
C.parent = parentOfA;
|
|
308
|
+
}
|
|
309
|
+
if (A === this.root) {
|
|
310
|
+
if (C)
|
|
311
|
+
this._setRoot(C);
|
|
312
|
+
}
|
|
313
|
+
else {
|
|
314
|
+
if (parentOfA) {
|
|
315
|
+
if (parentOfA.left === A) {
|
|
316
|
+
parentOfA.left = C;
|
|
317
|
+
}
|
|
318
|
+
else {
|
|
319
|
+
parentOfA.right = C;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
if (C)
|
|
324
|
+
A.right = C.left; // Make T2 the right subtree of A
|
|
325
|
+
if (B && C)
|
|
326
|
+
B.left = C.right; // Make T3 the left subtree of B
|
|
327
|
+
if (C)
|
|
328
|
+
C.left = A;
|
|
329
|
+
if (C)
|
|
330
|
+
C.right = B;
|
|
331
|
+
this.updateHeight(A); // Adjust heights
|
|
332
|
+
B && this.updateHeight(B);
|
|
333
|
+
C && this.updateHeight(C);
|
|
334
|
+
};
|
|
335
|
+
return AVLTree;
|
|
336
|
+
}(data_structure_typed_1.BST));
|
|
337
|
+
exports.AVLTree = AVLTree;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
2
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* data-structure-typed
|
|
6
|
-
*
|
|
7
|
-
* @author Tyler Zeng
|
|
8
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
9
|
-
* @license MIT License
|
|
10
|
-
*/
|
|
11
|
-
var data_structure_typed_1 = require("data-structure-typed");
|
|
12
|
-
Object.defineProperty(exports, "AVLTreeNode", { enumerable: true, get: function () { return data_structure_typed_1.AVLTreeNode; } });
|
|
13
|
-
Object.defineProperty(exports, "AVLTree", { enumerable: true, get: function () { return data_structure_typed_1.AVLTree; } });
|
|
17
|
+
__exportStar(require("./avl-tree"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "avl-tree-typed",
|
|
3
|
-
"version": "1.19.
|
|
3
|
+
"version": "1.19.32",
|
|
4
4
|
"description": "AVLTree(Adelson-Velsky and Landis Tree). Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"typescript": "^4.9.5"
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
|
-
"data-structure-typed": "^1.19.
|
|
56
|
+
"data-structure-typed": "^1.19.3",
|
|
57
57
|
"zod": "^3.22.2"
|
|
58
58
|
}
|
|
59
59
|
}
|
package/tsconfig.json
CHANGED
|
@@ -3,10 +3,9 @@
|
|
|
3
3
|
"declaration": true,
|
|
4
4
|
"outDir": "./dist",
|
|
5
5
|
"module": "commonjs",
|
|
6
|
-
"target": "
|
|
6
|
+
"target": "es5",
|
|
7
7
|
"lib": [
|
|
8
|
-
|
|
9
|
-
"esnext"
|
|
8
|
+
"esnext",
|
|
10
9
|
],
|
|
11
10
|
"strict": true,
|
|
12
11
|
"esModuleInterop": true,
|
|
@@ -31,9 +30,8 @@
|
|
|
31
30
|
"src",
|
|
32
31
|
],
|
|
33
32
|
"exclude": [
|
|
34
|
-
|
|
33
|
+
// "node_modules/data-structure-typed",
|
|
35
34
|
"node_modules",
|
|
36
35
|
"dist"
|
|
37
36
|
]
|
|
38
|
-
}
|
|
39
|
-
|
|
37
|
+
}
|