https://github.com/alexdremov/treearray
Swift tree-based array implementation. Efficient for random insertions/deletions
https://github.com/alexdremov/treearray
algorithms array efficiency efficient-algorithm ios ios-app ios-swift optimization optimization-algorithms optimizations swift swift-collection swift-package-manager treap tree-structure
Last synced: 3 months ago
JSON representation
Swift tree-based array implementation. Efficient for random insertions/deletions
- Host: GitHub
- URL: https://github.com/alexdremov/treearray
- Owner: alexdremov
- Created: 2023-01-11T19:30:43.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-01-17T22:11:48.000Z (over 2 years ago)
- Last Synced: 2025-01-17T08:29:36.237Z (5 months ago)
- Topics: algorithms, array, efficiency, efficient-algorithm, ios, ios-app, ios-swift, optimization, optimization-algorithms, optimizations, swift, swift-collection, swift-package-manager, treap, tree-structure
- Language: Swift
- Homepage:
- Size: 1.05 MB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# TreeArray
Swift implementation of implicit treap. Data structure with efficient random insert / remove.
## Usage
TreeArray **behaves like a usual array** but has a different implementation under the hood that allows some operations to work faster. You can just replace `Array` with `TreeArray` and it should work as expected.
```swift
import TreeArrayvar foo: TreeArray = [1, 2, 3, 4, 5]
foo.insert(0, at: 0)
print(foo) // [0, 1, 2, 3, 4, 5]
```## Complexity
According to performance tests, the visible difference starts to appear around 16k elements. After that, `TreeArray` outperform `Array` and `Deque` on random insertions and deletions.
| **Operation** | **Complexity** | **Complexity Array** |
|------------------------------|----------------|----------------------|
| append | `O(log n)` | `O(1)` |
| subscript | `O(log n)` | `O(1)` |
| random insert | `O(log n)` | `O(n)` |
| random delete | `O(log n)` | `O(n)` |
| iteration (iterator) | `O(n)` | `O(n)` |
| iteration (subscript) | `O(n * log n)` | `O(n)` |
| build from array | `O(n)` | `O(n)` |
| build from unknown-sized seq | `O(n * log n)` | `O(n)` |
| reverse | `O(n)` | `O(n)` |
| contains | `O(n)` | `O(n)` |
| append array | `O(m + log n)` | `O(m)` |
| insert array | `O(m + log n)` | `O(m + n)` |## Comparison
The data structure is built upon a self-balancing random search tree. It allows performing random insertions and deletions efficiently with the cost of worse performance on other operations. In the directory [Benchmarks/results](Benchmarks/results) you can explore full comparison results. Here, I will note only the most important cases.
### Random insertions
### Random removals

### Prepend

### Remove first

However, on other tests, it works worse. For example, iteration or build.

