Skip to content

Commit

Permalink
Ensure find and indexOf perform splay (#904)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
m4ushold authored Sep 30, 2024
1 parent 9bf42e4 commit 56ba2bb
Show file tree
Hide file tree
Showing 3 changed files with 259,882 additions and 13 deletions.
16 changes: 3 additions & 13 deletions packages/sdk/src/util/splay_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ export class SplayTree<V> {
`out of index range: pos: ${pos} > node.length: ${node.getLength()}`,
);
}
this.splayNode(node)
return [node, pos];
}

Expand All @@ -225,19 +226,8 @@ export class SplayTree<V> {
return -1;
}

let index = 0;
let current: SplayNode<V> | undefined = node;
let prev: SplayNode<V> | undefined;
while (current) {
if (!prev || prev === current.getRight()) {
index +=
current.getLength() +
(current.hasLeft() ? current.getLeftWeight() : 0);
}
prev = current;
current = current.getParent();
}
return index - node.getLength();
this.splayNode(node)
return this.root!.getLeftWeight();
}

/**
Expand Down
Loading

0 comments on commit 56ba2bb

Please sign in to comment.