Non Built-in Data Structures And Algorithms in Python3

Unordered MultiSet

1
2
3
4
5
6
7
8
9
10
11
12
13
class MultiSet:
def __init__(self):
self.data = dict()

def add(self, val):
self.data[val] = 1 + (self.data[val] if val in self.data else 0)

def remove(self, val):
if not val in self.data:
return
self.data[val] -= 1
if self.data[val] == 0:
del self.data[val]

Fenwick Tree / Binary-indexed Tree

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class BIT:
def __init__(self, size):
self.size = size
self.data = [0] * size

def add(self, pos, val):
while pos < self.size:
self.data[pos] += val
pos += pos & (-pos)

def query(self, pos):
res = 0
while pos > 0:
res += self.data[pos]
pos &= pos - 1
return res

Binary Heap / Priority Queue

Sample

Skip List

Sample 1
Sample 2

Red Black Tree

Sample 1
Sample 2

Next Permutation

Non Built-in Data Structures And Algorithms in Python3

https://devblog.citruxonve.net/posts/a6fa13e5/

Author

CitruXonve

Posted on

04/24/2022

Updated on

07/19/2023

Licensed under

Comments