Last active
December 19, 2015 02:38
-
-
Save giannitedesco/5883968 to your computer and use it in GitHub Desktop.
Little python library to convert between inorder, BFS and vEB tree layouts. The idea is to do this without constructing any trees. That is to say, in a manner appropriate for implicit layouts, eg. for a database.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import deque | |
class Bfs(int): | |
def left(self): | |
if self.height == 0: | |
raise ValueError | |
return Bfs(self * 2, height = self.height) | |
def right(self): | |
if self.height == 0: | |
raise ValueError | |
return Bfs(self * 2 + 1, height = self.height) | |
def parent(self): | |
if self.height == 0: | |
raise ValueError | |
return Bfs(self >> 1, height = self.height) | |
def __iter__(self): | |
class BfsIter: | |
def __init__(self, start): | |
self.__cur = deque([start]) | |
self.__nxt = deque([]) | |
def next(self, retry = True): | |
try: | |
ret = self.__cur.popleft() | |
try: | |
self.__nxt.append(ret.left()) | |
self.__nxt.append(ret.right()) | |
except ValueError: | |
pass | |
return ret | |
except IndexError: | |
if not retry: | |
raise StopIteration | |
self.__cur = self.__nxt | |
self.__nxt = deque([]) | |
return self.next(retry = False) | |
return BfsIter(self) | |
def __getitem__(self, compare): | |
if compare <= 0: | |
return self.left() | |
else: | |
return self.right() | |
def __new__(cls, *args, **kwargs): | |
height = kwargs.get('height', 0) | |
if len(args): | |
ret = super(Bfs, cls).__new__(cls, args[0]) | |
else: | |
ret = super(Bfs, cls).__new__(cls, height and 1 or 0) | |
ret.height = height | |
if ret > (1 << ret.height): | |
raise ValueError | |
return ret | |
if __name__ == '__main__': | |
for x in Bfs(3, height = 4): | |
print x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import deque | |
class Inorder(int): | |
def __ffs(self, i): | |
ret = 0 | |
while i: | |
if i & 1: | |
return ret | |
i >>= 1 | |
ret += 1 | |
if ret > self.height: | |
raise ValueError | |
def rank(self): | |
return self.height - self.__ffs(self) | |
def h(self): | |
return self.__ffs(self) + 1 | |
def left(self): | |
if self.height == 0: | |
raise ValueError | |
s = self.h() - 2 | |
if s < 0: | |
raise ValueError | |
return Inorder(self - (1 << s), height = self.height) | |
def right(self): | |
if self.height == 0: | |
raise ValueError | |
s = self.h() - 2 | |
if s < 0: | |
raise ValueError | |
return Inorder(self + (1 << s), height = self.height) | |
def parent(self): | |
if self.height == 0: | |
raise ValueError | |
s = self.h() | |
if s < 1: | |
raise ValueError | |
x = 1 << s | |
if self & x: | |
return Inorder(self - (x >> 1), height = self.height) | |
else: | |
return Inorder(self + (x >> 1), height = self.height) | |
def __iter__(self): | |
class InorderIter: | |
def __init__(self, cur, end, h): | |
self.cur = cur | |
self.end = end | |
self.h = h | |
def next(self): | |
self.cur += 1 | |
if self.cur >= self.end: | |
raise StopIteration | |
return Inorder(self.cur, height = self.h) | |
s = self.h() | |
if s < 1: | |
raise ValueError | |
x = 1 << (s - 1) | |
return InorderIter(self - x, self + x, self.height) | |
def __getitem__(self, compare): | |
if compare <= 0: | |
return self.left() | |
else: | |
return self.right() | |
def __new__(cls, *args, **kwargs): | |
height = kwargs.get('height', 0) | |
if len(args): | |
ret = super(Inorder, cls).__new__(cls, args[0]) | |
else: | |
ret = super(Inorder, cls).__new__(cls, height and \ | |
((1 << height) / 2) or 0) | |
ret.height = height | |
if ret < 0 or ret > (1 << ret.height): | |
raise ValueError | |
return ret | |
if __name__ == '__main__': | |
x = Inorder(height = 4) | |
print x, x.left(), x.right() | |
a = x.left().left().parent() | |
b = x.right().right().parent() | |
for x in x.right().left(): | |
print '%s -> %s'%(x, x.parent()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment