Is the update complexity O(log N) for this persistent map implementation?

Revision en5, by ReaLNero, 2024-11-19 01:51:23

I have a really succinct implementation for a persistent map with $$$O(log N)$$$ querying and (hopefully) $$$O(log N)$$$ amortized average-case insertion. Is this correct, or is the insertion actually $$$O(log^2 N)$$$?

I know there's the pathological case where if you keep doing insertion for the version of the LinkedList where the sum of dict sizes is a power of two, then you might end up repeatedly copying the whole dictionary. Let's say that's not the average case.

Also, for anyone unfamiliar -- Python dictionaries are hashmaps, so insertion/querying is amortized $$$O(1)$$$,

class LinkedList:
  parent: LinkedList | None
  curr_dict: dict[str, int]

def update(parent: LinkedList, key: str, value: int) -> LinkedList:
  curr_dict = {key: value}
  while parent.parent is not null and len(parent.curr_dict) <= len(curr_dict):
    curr_dict |= parent.curr_dict
    parent = parent.parent
  return LinkedList(parent=parent, curr_dict=curr_dict)

def query(node: LinkedList, key: str) -> int | None:
  while node is not None:
    if value := curr_dict.get(key)
      return value
    node = node.parent
  return None

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en8 English ReaLNero 2024-11-19 02:16:53 115
en7 English ReaLNero 2024-11-19 02:11:55 20
en6 English ReaLNero 2024-11-19 02:09:03 184
en5 English ReaLNero 2024-11-19 01:51:23 38
en4 English ReaLNero 2024-11-19 01:50:51 88
en3 English ReaLNero 2024-11-19 01:49:59 12 Tiny change: 'erying is O(1)\n\n```pyt' -> 'erying is amortized $O(1)$\n\n```pyt'
en2 English ReaLNero 2024-11-19 01:49:13 291
en1 English ReaLNero 2024-11-19 01:46:43 863 Initial revision (published)