Skip to content

Commit 56ba2bb

Browse files
authored
Ensure find and indexOf perform splay (#904)
Enhance splay tree efficiency by incorporating splay operations after node location in Find and IndexOf functions. This change maintains the tree's self-balancing property and ensures amortized O(log n) time complexity for future operations. Simplify IndexOf() by leveraging the splay operation, reducing additional traversal logic and improving overall performance.
1 parent 9bf42e4 commit 56ba2bb

File tree

3 files changed

+259882
-13
lines changed

3 files changed

+259882
-13
lines changed

packages/sdk/src/util/splay_tree.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ export class SplayTree<V> {
211211
`out of index range: pos: ${pos} > node.length: ${node.getLength()}`,
212212
);
213213
}
214+
this.splayNode(node)
214215
return [node, pos];
215216
}
216217

@@ -225,19 +226,8 @@ export class SplayTree<V> {
225226
return -1;
226227
}
227228

228-
let index = 0;
229-
let current: SplayNode<V> | undefined = node;
230-
let prev: SplayNode<V> | undefined;
231-
while (current) {
232-
if (!prev || prev === current.getRight()) {
233-
index +=
234-
current.getLength() +
235-
(current.hasLeft() ? current.getLeftWeight() : 0);
236-
}
237-
prev = current;
238-
current = current.getParent();
239-
}
240-
return index - node.getLength();
229+
this.splayNode(node)
230+
return this.root!.getLeftWeight();
241231
}
242232

243233
/**

0 commit comments

Comments
 (0)