We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Median Updates
Median Updates
Sort by
recency
|
189 Discussions
|
Please Login in order to post a comment
`
class Node: def init(self, data): self.data = data self.left = None self.right = None self.height = 1 self.size = 1
class AVLTree: def init(self): self.root = None
def median(s, x): avlTree = AVLTree() for op, val in zip(s, x): if op == "a": avlTree.insert(val) elif op == "r": avlTree.delete(val)
Done it with the AVL Tree. Every node of the tree tracks not only height but also size which allows to calculate the index of the node and therefore the median.
` class AVL: def init(self): self.root = None
The rest of the AVL tree implementation you may find on geeksforgeeks.
i got this one
https://ideone.com/YWjB9B
Simple solution
The multi-set solution is O(n) for add and O(n) for remove (thus no better than using a sorted linkedlist). This is a heap based solution that's O(log n) for add but still O(n) for remove.