nlptoolkit-datastructure 1.0.8 → 1.0.9
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/dist/CounterHashMap.js +108 -118
- package/dist/CounterHashMap.js.map +1 -1
- package/dist/LRUCache.js +55 -63
- package/dist/LRUCache.js.map +1 -1
- package/dist/Queue.js +57 -64
- package/dist/Queue.js.map +1 -1
- package/dist/Stack.js +40 -50
- package/dist/Stack.js.map +1 -1
- package/dist/heap/Heap.js +113 -119
- package/dist/heap/Heap.js.map +1 -1
- package/dist/heap/HeapNode.js +19 -28
- package/dist/heap/HeapNode.js.map +1 -1
- package/dist/heap/MaxHeap.js +11 -21
- package/dist/heap/MaxHeap.js.map +1 -1
- package/dist/heap/MinHeap.js +11 -21
- package/dist/heap/MinHeap.js.map +1 -1
- package/dist/index.js +16 -22
- package/dist/index.js.map +1 -1
- package/dist/tree/AvlTree.js +160 -170
- package/dist/tree/AvlTree.js.map +1 -1
- package/dist/tree/AvlTreeNode.js +11 -20
- package/dist/tree/AvlTreeNode.js.map +1 -1
- package/dist/tree/BTree.js +74 -82
- package/dist/tree/BTree.js.map +1 -1
- package/dist/tree/BTreeNode.js +168 -173
- package/dist/tree/BTreeNode.js.map +1 -1
- package/dist/tree/Tree.js +81 -90
- package/dist/tree/Tree.js.map +1 -1
- package/dist/tree/TreeNode.js +11 -20
- package/dist/tree/TreeNode.js.map +1 -1
- package/package.json +3 -2
- package/tsconfig.json +4 -4
- package/source/tsconfig.json +0 -13
package/dist/tree/AvlTree.js
CHANGED
|
@@ -1,188 +1,178 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AvlTree = void 0;
|
|
4
|
+
const Tree_1 = require("./Tree");
|
|
5
|
+
const AvlTreeNode_1 = require("./AvlTreeNode");
|
|
6
|
+
const Stack_1 = require("../Stack");
|
|
7
|
+
/**
|
|
8
|
+
* <p>AVL (Adelson-Velskii and Landis) tree is a balanced binary search tree structure. The balance property is very simple
|
|
9
|
+
* and ensures the depth of the tree is in the level of O(log N).</p>
|
|
10
|
+
*
|
|
11
|
+
* <p>In AVL tree, the heights of the left and right subtrees of each node can differ by at most 1. If the heights of the
|
|
12
|
+
* left and right subtrees of a single node differ by more than 1, then that binary search tree is not an AVL tree.</p>
|
|
13
|
+
* @param T Type of the data stored in the tree node.
|
|
14
|
+
*/
|
|
15
|
+
class AvlTree extends Tree_1.Tree {
|
|
16
|
+
constructor(comparator) {
|
|
17
|
+
super(comparator);
|
|
5
18
|
}
|
|
6
|
-
|
|
7
|
-
|
|
19
|
+
height(d) {
|
|
20
|
+
if (d == null) {
|
|
21
|
+
return 0;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
return d.height;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* In rotate left, we move node k1 one level up, since due to the binary search tree
|
|
29
|
+
* property k2 > k1, we move node k2 one level down. The links updated are:
|
|
30
|
+
* <ul>
|
|
31
|
+
* <li>Since k2 > B > k1, the left child of node k2 is now the old right child of k1</li>
|
|
32
|
+
* <li>The right child of k1 is now k2 </li>
|
|
33
|
+
* </ul>
|
|
34
|
+
* Note that, the root node of the subtree is now k1. In order to modify the parent link of k2, the new root of the
|
|
35
|
+
* subtree is returned by the function.
|
|
36
|
+
* @param k2 Root of the subtree, which does not satisfy the AVL tree property.
|
|
37
|
+
* @return The new root of the subtree
|
|
38
|
+
*/
|
|
39
|
+
rotateLeft(k2) {
|
|
40
|
+
let k1 = k2.left;
|
|
41
|
+
k2.left = k1.right;
|
|
42
|
+
k1.right = k2;
|
|
43
|
+
k2.height = Math.max(this.height(k2.left), this.height(k2.right)) + 1;
|
|
44
|
+
k1.height = Math.max(this.height(k1.left), k1.right.height) + 1;
|
|
45
|
+
return k1;
|
|
8
46
|
}
|
|
9
|
-
})(function (require, exports) {
|
|
10
|
-
"use strict";
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.AvlTree = void 0;
|
|
13
|
-
const Tree_1 = require("./Tree");
|
|
14
|
-
const AvlTreeNode_1 = require("./AvlTreeNode");
|
|
15
|
-
const Stack_1 = require("../Stack");
|
|
16
47
|
/**
|
|
17
|
-
*
|
|
18
|
-
*
|
|
48
|
+
* In order to restore the AVL tree property, we move node k2 one level up, since due to the binary search tree
|
|
49
|
+
* property k2 > k1, we move node k1 one level down. The links updated are:
|
|
50
|
+
* <ul>
|
|
51
|
+
* <li>Since k2 > B > k1, the right child of node k1 is now the old left child of k2.</li>
|
|
52
|
+
* <li>The left child of k2 is now k1</li>
|
|
53
|
+
* </ul>
|
|
54
|
+
* Note that, the root node of the subtree is now k2. In order to modify the parent link of k1, the new root of the
|
|
55
|
+
* subtree is returned by the function.
|
|
56
|
+
* @param k1 Root of the subtree, which does not satisfy the AVL tree property.
|
|
57
|
+
* @return The new root of the subtree
|
|
58
|
+
*/
|
|
59
|
+
rotateRight(k1) {
|
|
60
|
+
let k2 = k1.right;
|
|
61
|
+
k1.right = k2.left;
|
|
62
|
+
k2.left = k1;
|
|
63
|
+
k2.height = Math.max(k2.left.height, this.height(k2.right)) + 1;
|
|
64
|
+
k1.height = Math.max(this.height(k1.left), this.height(k1.right)) + 1;
|
|
65
|
+
return k2;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* <p>In the first phase we will do single right rotation on the subtree rooted with k1. With this rotation, the left
|
|
69
|
+
* child of node k2 will be k1, whereas the right child of node k1 will be B (the old left child of node k2).</p>
|
|
70
|
+
*
|
|
71
|
+
* <p>In the second phase, we will do single left rotation on the subtree rooted with k3. With this rotation, the
|
|
72
|
+
* right child of node k2 will be k3, whereas the left child of node k3 will be C (the old right child of k2).</p>
|
|
19
73
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* @param
|
|
74
|
+
* Note that, the new root node of the subtree is now k2. In order to modify the parent link of k3, the new root of
|
|
75
|
+
* the subtree is returned by the function.
|
|
76
|
+
* @param k3 Root of the subtree, which does not satisfy the AVL tree property.
|
|
77
|
+
* @return The new root of the subtree
|
|
23
78
|
*/
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
79
|
+
doubleRotateLeft(k3) {
|
|
80
|
+
k3.left = this.rotateRight(k3.left);
|
|
81
|
+
return this.rotateLeft(k3);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* <p>In the first phase we will do single right rotation on the subtree rooted with k3. With this rotation, the right
|
|
85
|
+
* child of node k2 will be k3, whereas the left child of node k3 will be C (the old right child of node k2).</p>
|
|
86
|
+
*
|
|
87
|
+
* <p>In the second phase, we will do single right rotation on the subtree rooted with k1. With this rotation, the left
|
|
88
|
+
* child of node k2 will be k1, whereas the left child of node k1 will be B (the old left child of k2).</p>
|
|
89
|
+
*
|
|
90
|
+
* Note that, the new root node of the subtree is now k2. In order to modify the parent link of k1, the new root of
|
|
91
|
+
* the subtree is returned by the function.
|
|
92
|
+
* @param k1 Root of the subtree, which does not satisfy the AVL tree property.
|
|
93
|
+
* @return The new root of the subtree
|
|
94
|
+
*/
|
|
95
|
+
doubleRotateRight(k1) {
|
|
96
|
+
k1.right = this.rotateLeft(k1.right);
|
|
97
|
+
return this.rotateRight(k1);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* <p>First we will proceed with the stages the same when we add a new node into a binary search tree. For this, we
|
|
101
|
+
* start from the root node and traverse in down manner. The current node we visit is represented with x and the
|
|
102
|
+
* previous node is represented with y. At each time we compare the value of the current node with the value of the
|
|
103
|
+
* new node. If the value of the new node is smaller than the value of the current node, the new node will be
|
|
104
|
+
* inserted into the left subtree of the current node. For this, we will continue with the left child to process. If
|
|
105
|
+
* the reverse is true, that is, if the value of the new node is larger than the value of the current node, the new
|
|
106
|
+
* node will be inserted into the right subtree of the current node. In this case, we will continue with the right
|
|
107
|
+
* child to process. At each step, we store the current node in a separate stack.</p>
|
|
108
|
+
*
|
|
109
|
+
* <p>When we insert a new node into an AVL tree, we need to change the heights of the nodes and check if the AVL tree
|
|
110
|
+
* property is satisfied or not. Only the height of the nodes, which we visit while we are finding the place for the
|
|
111
|
+
* new node, can be changed. So, what we should do is to pop those nodes from the stack one by one and change the
|
|
112
|
+
* heights of those nodes.</p>
|
|
113
|
+
*
|
|
114
|
+
* <p>Similarly, the nodes, which we visit while we are finding the place for the new node, may no longer satisfy the
|
|
115
|
+
* AVL tree property. So what we should do is to pop those nodes from the stack one by one and calculate the
|
|
116
|
+
* difference of the heights of their left and right subtrees. If the height difference is 2, the AVL tree property
|
|
117
|
+
* is not satisfied. If we added the new node into the left subtree of the left child, we need to do left single
|
|
118
|
+
* rotation, if we added into the right subtree of the left child, we need to do left double rotation, if we added
|
|
119
|
+
* into the left subtree of the right child, we need to do right double rotation, if we added into the right subtree
|
|
120
|
+
* of the right child, we need to the right single rotation. Since the root node of the subtree will be changed
|
|
121
|
+
* after a rotation, the new child of y will be the new root node t.</p>
|
|
122
|
+
* @param node Node to be inserted.
|
|
123
|
+
*/
|
|
124
|
+
insert(node) {
|
|
125
|
+
let LEFT = 1, RIGHT = 2;
|
|
126
|
+
let y = null;
|
|
127
|
+
let x = this.root;
|
|
128
|
+
let t;
|
|
129
|
+
let dir1 = 0, dir2 = 0;
|
|
130
|
+
let c = new Stack_1.Stack();
|
|
131
|
+
while (x != null) {
|
|
132
|
+
y = x;
|
|
133
|
+
c.push(y);
|
|
134
|
+
dir1 = dir2;
|
|
135
|
+
if (this.comparator(node.data, x.data) < 0) {
|
|
136
|
+
x = x.left;
|
|
137
|
+
dir2 = LEFT;
|
|
31
138
|
}
|
|
32
139
|
else {
|
|
33
|
-
|
|
140
|
+
x = x.right;
|
|
141
|
+
dir2 = RIGHT;
|
|
34
142
|
}
|
|
35
143
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
k1.height = Math.max(this.height(k1.left), k1.right.height) + 1;
|
|
54
|
-
return k1;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* In order to restore the AVL tree property, we move node k2 one level up, since due to the binary search tree
|
|
58
|
-
* property k2 > k1, we move node k1 one level down. The links updated are:
|
|
59
|
-
* <ul>
|
|
60
|
-
* <li>Since k2 > B > k1, the right child of node k1 is now the old left child of k2.</li>
|
|
61
|
-
* <li>The left child of k2 is now k1</li>
|
|
62
|
-
* </ul>
|
|
63
|
-
* Note that, the root node of the subtree is now k2. In order to modify the parent link of k1, the new root of the
|
|
64
|
-
* subtree is returned by the function.
|
|
65
|
-
* @param k1 Root of the subtree, which does not satisfy the AVL tree property.
|
|
66
|
-
* @return The new root of the subtree
|
|
67
|
-
*/
|
|
68
|
-
rotateRight(k1) {
|
|
69
|
-
let k2 = k1.right;
|
|
70
|
-
k1.right = k2.left;
|
|
71
|
-
k2.left = k1;
|
|
72
|
-
k2.height = Math.max(k2.left.height, this.height(k2.right)) + 1;
|
|
73
|
-
k1.height = Math.max(this.height(k1.left), this.height(k1.right)) + 1;
|
|
74
|
-
return k2;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* <p>In the first phase we will do single right rotation on the subtree rooted with k1. With this rotation, the left
|
|
78
|
-
* child of node k2 will be k1, whereas the right child of node k1 will be B (the old left child of node k2).</p>
|
|
79
|
-
*
|
|
80
|
-
* <p>In the second phase, we will do single left rotation on the subtree rooted with k3. With this rotation, the
|
|
81
|
-
* right child of node k2 will be k3, whereas the left child of node k3 will be C (the old right child of k2).</p>
|
|
82
|
-
*
|
|
83
|
-
* Note that, the new root node of the subtree is now k2. In order to modify the parent link of k3, the new root of
|
|
84
|
-
* the subtree is returned by the function.
|
|
85
|
-
* @param k3 Root of the subtree, which does not satisfy the AVL tree property.
|
|
86
|
-
* @return The new root of the subtree
|
|
87
|
-
*/
|
|
88
|
-
doubleRotateLeft(k3) {
|
|
89
|
-
k3.left = this.rotateRight(k3.left);
|
|
90
|
-
return this.rotateLeft(k3);
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* <p>In the first phase we will do single right rotation on the subtree rooted with k3. With this rotation, the right
|
|
94
|
-
* child of node k2 will be k3, whereas the left child of node k3 will be C (the old right child of node k2).</p>
|
|
95
|
-
*
|
|
96
|
-
* <p>In the second phase, we will do single right rotation on the subtree rooted with k1. With this rotation, the left
|
|
97
|
-
* child of node k2 will be k1, whereas the left child of node k1 will be B (the old left child of k2).</p>
|
|
98
|
-
*
|
|
99
|
-
* Note that, the new root node of the subtree is now k2. In order to modify the parent link of k1, the new root of
|
|
100
|
-
* the subtree is returned by the function.
|
|
101
|
-
* @param k1 Root of the subtree, which does not satisfy the AVL tree property.
|
|
102
|
-
* @return The new root of the subtree
|
|
103
|
-
*/
|
|
104
|
-
doubleRotateRight(k1) {
|
|
105
|
-
k1.right = this.rotateLeft(k1.right);
|
|
106
|
-
return this.rotateRight(k1);
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* <p>First we will proceed with the stages the same when we add a new node into a binary search tree. For this, we
|
|
110
|
-
* start from the root node and traverse in down manner. The current node we visit is represented with x and the
|
|
111
|
-
* previous node is represented with y. At each time we compare the value of the current node with the value of the
|
|
112
|
-
* new node. If the value of the new node is smaller than the value of the current node, the new node will be
|
|
113
|
-
* inserted into the left subtree of the current node. For this, we will continue with the left child to process. If
|
|
114
|
-
* the reverse is true, that is, if the value of the new node is larger than the value of the current node, the new
|
|
115
|
-
* node will be inserted into the right subtree of the current node. In this case, we will continue with the right
|
|
116
|
-
* child to process. At each step, we store the current node in a separate stack.</p>
|
|
117
|
-
*
|
|
118
|
-
* <p>When we insert a new node into an AVL tree, we need to change the heights of the nodes and check if the AVL tree
|
|
119
|
-
* property is satisfied or not. Only the height of the nodes, which we visit while we are finding the place for the
|
|
120
|
-
* new node, can be changed. So, what we should do is to pop those nodes from the stack one by one and change the
|
|
121
|
-
* heights of those nodes.</p>
|
|
122
|
-
*
|
|
123
|
-
* <p>Similarly, the nodes, which we visit while we are finding the place for the new node, may no longer satisfy the
|
|
124
|
-
* AVL tree property. So what we should do is to pop those nodes from the stack one by one and calculate the
|
|
125
|
-
* difference of the heights of their left and right subtrees. If the height difference is 2, the AVL tree property
|
|
126
|
-
* is not satisfied. If we added the new node into the left subtree of the left child, we need to do left single
|
|
127
|
-
* rotation, if we added into the right subtree of the left child, we need to do left double rotation, if we added
|
|
128
|
-
* into the left subtree of the right child, we need to do right double rotation, if we added into the right subtree
|
|
129
|
-
* of the right child, we need to the right single rotation. Since the root node of the subtree will be changed
|
|
130
|
-
* after a rotation, the new child of y will be the new root node t.</p>
|
|
131
|
-
* @param node Node to be inserted.
|
|
132
|
-
*/
|
|
133
|
-
insert(node) {
|
|
134
|
-
let LEFT = 1, RIGHT = 2;
|
|
135
|
-
let y = null;
|
|
136
|
-
let x = this.root;
|
|
137
|
-
let t;
|
|
138
|
-
let dir1 = 0, dir2 = 0;
|
|
139
|
-
let c = new Stack_1.Stack();
|
|
140
|
-
while (x != null) {
|
|
141
|
-
y = x;
|
|
142
|
-
c.push(y);
|
|
143
|
-
dir1 = dir2;
|
|
144
|
-
if (this.comparator(node.data, x.data) < 0) {
|
|
145
|
-
x = x.left;
|
|
146
|
-
dir2 = LEFT;
|
|
147
|
-
}
|
|
148
|
-
else {
|
|
149
|
-
x = x.right;
|
|
150
|
-
dir2 = RIGHT;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
this.insertChild(y, node);
|
|
154
|
-
while (!c.isEmpty()) {
|
|
155
|
-
x = c.pop();
|
|
156
|
-
if (x != null) {
|
|
157
|
-
x.height = Math.max(this.height(x.left), this.height(x.right)) + 1;
|
|
158
|
-
if (Math.abs(this.height(x.left) - this.height(x.right)) == 2) {
|
|
159
|
-
if (dir1 == LEFT) {
|
|
160
|
-
if (dir2 == LEFT) {
|
|
161
|
-
t = this.rotateLeft(x);
|
|
162
|
-
}
|
|
163
|
-
else {
|
|
164
|
-
t = this.doubleRotateLeft(x);
|
|
165
|
-
}
|
|
144
|
+
this.insertChild(y, node);
|
|
145
|
+
while (!c.isEmpty()) {
|
|
146
|
+
x = c.pop();
|
|
147
|
+
if (x != null) {
|
|
148
|
+
x.height = Math.max(this.height(x.left), this.height(x.right)) + 1;
|
|
149
|
+
if (Math.abs(this.height(x.left) - this.height(x.right)) == 2) {
|
|
150
|
+
if (dir1 == LEFT) {
|
|
151
|
+
if (dir2 == LEFT) {
|
|
152
|
+
t = this.rotateLeft(x);
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
t = this.doubleRotateLeft(x);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
if (dir2 == LEFT) {
|
|
160
|
+
t = this.doubleRotateRight(x);
|
|
166
161
|
}
|
|
167
162
|
else {
|
|
168
|
-
|
|
169
|
-
t = this.doubleRotateRight(x);
|
|
170
|
-
}
|
|
171
|
-
else {
|
|
172
|
-
t = this.rotateRight(x);
|
|
173
|
-
}
|
|
163
|
+
t = this.rotateRight(x);
|
|
174
164
|
}
|
|
175
|
-
y = c.pop();
|
|
176
|
-
this.insertChild(y, t);
|
|
177
|
-
break;
|
|
178
165
|
}
|
|
166
|
+
y = c.pop();
|
|
167
|
+
this.insertChild(y, t);
|
|
168
|
+
break;
|
|
179
169
|
}
|
|
180
170
|
}
|
|
181
171
|
}
|
|
182
|
-
insertData(item) {
|
|
183
|
-
this.insert(new AvlTreeNode_1.AvlTreeNode(item));
|
|
184
|
-
}
|
|
185
172
|
}
|
|
186
|
-
|
|
187
|
-
|
|
173
|
+
insertData(item) {
|
|
174
|
+
this.insert(new AvlTreeNode_1.AvlTreeNode(item));
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
exports.AvlTree = AvlTree;
|
|
188
178
|
//# sourceMappingURL=AvlTree.js.map
|
package/dist/tree/AvlTree.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AvlTree.js","sourceRoot":"","sources":["../../source/tree/AvlTree.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"AvlTree.js","sourceRoot":"","sources":["../../source/tree/AvlTree.ts"],"names":[],"mappings":";;;AAAA,iCAA4B;AAC5B,+CAA0C;AAC1C,oCAA+B;AAE/B;;;;;;;GAOG;AACH,MAAa,OAAW,SAAQ,WAAO;IAEnC,YAAY,UAA6C;QACrD,KAAK,CAAC,UAAU,CAAC,CAAA;IACrB,CAAC;IAEM,MAAM,CAAC,CAAiB;QAC3B,IAAI,CAAC,IAAI,IAAI,EAAC,CAAC;YACX,OAAO,CAAC,CAAA;QACZ,CAAC;aAAM,CAAC;YACJ,OAAO,CAAC,CAAC,MAAM,CAAA;QACnB,CAAC;IACL,CAAC;IAED;;;;;;;;;;;OAWG;IACK,UAAU,CAAC,EAAkB;QACjC,IAAI,EAAE,GAAqC,EAAE,CAAC,IAAI,CAAA;QAClD,EAAE,CAAC,IAAI,GAAG,EAAE,CAAC,KAAK,CAAA;QAClB,EAAE,CAAC,KAAK,GAAG,EAAE,CAAA;QACb,EAAE,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAkB,EAAE,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,MAAM,CAAkB,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAA;QACvG,EAAE,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAkB,EAAE,CAAC,IAAI,CAAC,EAAoB,EAAE,CAAC,KAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;QACnG,OAAO,EAA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G,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAA;wBAC1B,CAAC;6BAAM,CAAC;4BACJ,CAAC,GAAG,IAAI,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAA;wBAChC,CAAC;oBACL,CAAC;yBAAM,CAAC;wBACJ,IAAI,IAAI,IAAI,IAAI,EAAC,CAAC;4BACd,CAAC,GAAG,IAAI,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAA;wBACjC,CAAC;6BAAM,CAAC;4BACJ,CAAC,GAAG,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;wBAC5B,CAAC;oBACL,CAAC;oBACD,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC;oBACZ,IAAI,CAAC,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;oBACvB,MAAM;gBACV,CAAC;YACL,CAAC;QACL,CAAC;IACL,CAAC;IAEM,UAAU,CAAE,IAAQ;QACvB,IAAI,CAAC,MAAM,CAAC,IAAI,yBAAW,CAAI,IAAI,CAAC,CAAC,CAAA;IACzC,CAAC;CAEJ;AArKD,0BAqKC"}
|
package/dist/tree/AvlTreeNode.js
CHANGED
|
@@ -1,22 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AvlTreeNode = void 0;
|
|
4
|
+
const TreeNode_1 = require("./TreeNode");
|
|
5
|
+
class AvlTreeNode extends TreeNode_1.TreeNode {
|
|
6
|
+
height;
|
|
7
|
+
constructor(data) {
|
|
8
|
+
super(data);
|
|
9
|
+
this.height = 1;
|
|
5
10
|
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
})(function (require, exports) {
|
|
10
|
-
"use strict";
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.AvlTreeNode = void 0;
|
|
13
|
-
const TreeNode_1 = require("./TreeNode");
|
|
14
|
-
class AvlTreeNode extends TreeNode_1.TreeNode {
|
|
15
|
-
constructor(data) {
|
|
16
|
-
super(data);
|
|
17
|
-
this.height = 1;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
exports.AvlTreeNode = AvlTreeNode;
|
|
21
|
-
});
|
|
11
|
+
}
|
|
12
|
+
exports.AvlTreeNode = AvlTreeNode;
|
|
22
13
|
//# sourceMappingURL=AvlTreeNode.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AvlTreeNode.js","sourceRoot":"","sources":["../../source/tree/AvlTreeNode.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"AvlTreeNode.js","sourceRoot":"","sources":["../../source/tree/AvlTreeNode.ts"],"names":[],"mappings":";;;AAAA,yCAAoC;AAEpC,MAAa,WAAe,SAAQ,mBAAW;IAE3C,MAAM,CAAS;IAEf,YAAa,IAAO;QAChB,KAAK,CAAC,IAAI,CAAC,CAAA;QACX,IAAI,CAAC,MAAM,GAAG,CAAC,CAAA;IACnB,CAAC;CAEJ;AATD,kCASC"}
|
package/dist/tree/BTree.js
CHANGED
|
@@ -1,93 +1,85 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BTree = void 0;
|
|
4
|
+
const BTreeNode_1 = require("./BTreeNode");
|
|
5
|
+
/**
|
|
6
|
+
* <p>In the computer science literature, the structures such as AVL tree, splay tree, red-black tree are proposed, which
|
|
7
|
+
* show the search tree property and also remain balanced after insertion and deletion operations.</p>
|
|
8
|
+
*
|
|
9
|
+
* <p>Another possibility of constructing a balanced tree structure is to store not only a single value but more than one
|
|
10
|
+
* value in a node. These type of tree structures are generalizations of the binary trees and called d-ary tree
|
|
11
|
+
* structures in the computer science literature. 2-3-4 trees, B-tree, B+ trees can be given as example d-ary tree
|
|
12
|
+
* structures. B+ trees, is one of the d-ary tree structures and used often in database systems.</p>
|
|
13
|
+
*
|
|
14
|
+
* <p>B+ tree is a dynamic search tree structure and consists of two parts, an index part and a data part. The index part
|
|
15
|
+
* is of d-ary tree structure, each node stores d {@literal <} m {@literal <} 2d values. d is a parameter of B+ tree, shows the capacity of B+
|
|
16
|
+
* tree and called as the degree of the tree. The root node is the single exception to this rule and can store
|
|
17
|
+
* 1 {@literal <} m {@literal <} 2d values. Each node also contains m + 1 links to point to its m + 1 child nodes. With the help of these
|
|
18
|
+
* links, the tree can be traversed in top-down manner. Let Pi represent the link pointing to the node i and Ki
|
|
19
|
+
* represent the i'th value in the same node, the i'th child and the ascendants of this child can take values between
|
|
20
|
+
* the interval Ki {@literal <} K {@literal <} Ki+1. The data are stored in the leaf nodes and due to the definition of a tree, the leaf nodes
|
|
21
|
+
* can not have children.</p>
|
|
22
|
+
* @param T Type of the data stored in the B tree node.
|
|
23
|
+
*/
|
|
24
|
+
class BTree {
|
|
25
|
+
root = null;
|
|
26
|
+
comparator;
|
|
27
|
+
d;
|
|
28
|
+
/**
|
|
29
|
+
* Constructor of the tree. According to the comparator, the tree could contain any object.
|
|
30
|
+
* @param d Parameter d in d-ary tree.
|
|
31
|
+
* @param comparator Comparator function to compare two elements.
|
|
32
|
+
*/
|
|
33
|
+
constructor(d, comparator) {
|
|
34
|
+
this.comparator = comparator;
|
|
35
|
+
this.d = d;
|
|
8
36
|
}
|
|
9
|
-
})(function (require, exports) {
|
|
10
|
-
"use strict";
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.BTree = void 0;
|
|
13
|
-
const BTreeNode_1 = require("./BTreeNode");
|
|
14
37
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* is of d-ary tree structure, each node stores d {@literal <} m {@literal <} 2d values. d is a parameter of B+ tree, shows the capacity of B+
|
|
25
|
-
* tree and called as the degree of the tree. The root node is the single exception to this rule and can store
|
|
26
|
-
* 1 {@literal <} m {@literal <} 2d values. Each node also contains m + 1 links to point to its m + 1 child nodes. With the help of these
|
|
27
|
-
* links, the tree can be traversed in top-down manner. Let Pi represent the link pointing to the node i and Ki
|
|
28
|
-
* represent the i'th value in the same node, the i'th child and the ascendants of this child can take values between
|
|
29
|
-
* the interval Ki {@literal <} K {@literal <} Ki+1. The data are stored in the leaf nodes and due to the definition of a tree, the leaf nodes
|
|
30
|
-
* can not have children.</p>
|
|
31
|
-
* @param T Type of the data stored in the B tree node.
|
|
38
|
+
* We start searching from the root node, the node with which we compare the searched value at each stage is
|
|
39
|
+
* represented by b, and we continue the search until we arrive the leaf nodes. In order to understand the subtree
|
|
40
|
+
* of node b where our searched value resides, we need to compare the searched value with the values Ki. For this,
|
|
41
|
+
* the function named position is given. If the searched value is larger than the last value of node b, we need to
|
|
42
|
+
* continue the search with the rightmost child. If the searched value is smaller than the i. value of node b, we
|
|
43
|
+
* need to continue the search with the i. child. As a last step, the function returns the leaf node of node b.
|
|
44
|
+
* @param value Value searched in B+ tree.
|
|
45
|
+
* @return If the value exists in the tree, the function returns the node that contains the node. Otherwise, it
|
|
46
|
+
* returns null.
|
|
32
47
|
*/
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
constructor(d, comparator) {
|
|
40
|
-
this.root = null;
|
|
41
|
-
this.comparator = comparator;
|
|
42
|
-
this.d = d;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* We start searching from the root node, the node with which we compare the searched value at each stage is
|
|
46
|
-
* represented by b, and we continue the search until we arrive the leaf nodes. In order to understand the subtree
|
|
47
|
-
* of node b where our searched value resides, we need to compare the searched value with the values Ki. For this,
|
|
48
|
-
* the function named position is given. If the searched value is larger than the last value of node b, we need to
|
|
49
|
-
* continue the search with the rightmost child. If the searched value is smaller than the i. value of node b, we
|
|
50
|
-
* need to continue the search with the i. child. As a last step, the function returns the leaf node of node b.
|
|
51
|
-
* @param value Value searched in B+ tree.
|
|
52
|
-
* @return If the value exists in the tree, the function returns the node that contains the node. Otherwise, it
|
|
53
|
-
* returns null.
|
|
54
|
-
*/
|
|
55
|
-
search(value) {
|
|
56
|
-
let b = this.root;
|
|
57
|
-
while (b != null && !b.leaf) {
|
|
58
|
-
let child = b.position(value, this.comparator);
|
|
59
|
-
if (child < b.m && b.K[child] == value) {
|
|
60
|
-
return b;
|
|
61
|
-
}
|
|
62
|
-
b = b.children[child];
|
|
63
|
-
}
|
|
64
|
-
if (b != null) {
|
|
65
|
-
let child = b.position(value, this.comparator);
|
|
66
|
-
if (child < b.m && b.K[child] == value) {
|
|
67
|
-
return b;
|
|
68
|
-
}
|
|
48
|
+
search(value) {
|
|
49
|
+
let b = this.root;
|
|
50
|
+
while (b != null && !b.leaf) {
|
|
51
|
+
let child = b.position(value, this.comparator);
|
|
52
|
+
if (child < b.m && b.K[child] == value) {
|
|
53
|
+
return b;
|
|
69
54
|
}
|
|
70
|
-
|
|
55
|
+
b = b.children[child];
|
|
71
56
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
57
|
+
if (b != null) {
|
|
58
|
+
let child = b.position(value, this.comparator);
|
|
59
|
+
if (child < b.m && b.K[child] == value) {
|
|
60
|
+
return b;
|
|
75
61
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
insertData(data) {
|
|
66
|
+
if (this.root == null) {
|
|
67
|
+
this.root = new BTreeNode_1.BTreeNode(this.d, undefined, undefined, undefined);
|
|
68
|
+
}
|
|
69
|
+
if (this.root.leaf) {
|
|
70
|
+
let s = this.root.insertLeaf(data, this.comparator);
|
|
71
|
+
if (s != null) {
|
|
72
|
+
let tmp = this.root;
|
|
73
|
+
this.root = new BTreeNode_1.BTreeNode(this.d, tmp, s, tmp.K[this.d]);
|
|
82
74
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
let s = this.root.insertNode(data, this.comparator, true);
|
|
78
|
+
if (s != null) {
|
|
79
|
+
this.root = s;
|
|
88
80
|
}
|
|
89
81
|
}
|
|
90
82
|
}
|
|
91
|
-
|
|
92
|
-
|
|
83
|
+
}
|
|
84
|
+
exports.BTree = BTree;
|
|
93
85
|
//# sourceMappingURL=BTree.js.map
|
package/dist/tree/BTree.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BTree.js","sourceRoot":"","sources":["../../source/tree/BTree.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"BTree.js","sourceRoot":"","sources":["../../source/tree/BTree.ts"],"names":[],"mappings":";;;AAAA,2CAAsC;AAEtC;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAa,KAAK;IAEd,IAAI,GAAyB,IAAI,CAAC;IAClC,UAAU,CAAoC;IAC9C,CAAC,CAAS;IAEV;;;;OAIG;IACH,YAAY,CAAS,EAAE,UAA6C;QAChE,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;QAC5B,IAAI,CAAC,CAAC,GAAG,CAAC,CAAA;IACd,CAAC;IAED;;;;;;;;;;OAUG;IACI,MAAM,CAAC,KAAQ;QAClB,IAAI,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC;QAClB,OAAO,CAAC,IAAI,IAAI,IAAI,CAAC,CAAC,CAAC,IAAI,EAAC,CAAC;YACzB,IAAI,KAAK,GAAG,CAAC,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,UAAU,CAAC,CAAA;YAC9C,IAAI,KAAK,GAAG,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