def insert2(B, x): if B == None: return btree.BTree([x]) else: if B.nbkeys == 2 * B.degree - 1: # root split B = btree.BTree([], [B]) split(B, 0) __insert2(B, x) return B
def insert(B, x): ''' inserts x in B (if not already in B) returns B (needed: in case of new root!) ''' if B == None: return btree.BTree([x]) else: if B.nbkeys == 2 * B.degree - 1: # root split B = btree.BTree([], [B]) split(B, 0) __insert(B, x) return B
def rbt_to_24tree(B): if B == None: return None else: N = btree.BTree([], []) if B.left and B.left.red: pass else: pass if B.right and B.right.red: pass else: pass if N.children[0] == None: # delete the list of None in leaves! N.children = [] return N
def split(B, i): ''' splits the child i of B conditions: - B is a nonempty tree and its root is not a 2t-node. - The child i of B exists and its root is a 2t-node. ''' mid = B.degree - 1 L = B.children[i] R = btree.BTree() # keys (L.keys, x, R.keys) = (L.keys[:mid], L.keys[mid], L.keys[mid + 1:]) # children if L.children == []: R.children = [] # useless L[:2] = [] if L is []! else: (L.children, R.children) = (L.children[:mid + 1], L.children[mid + 1:]) # root B.keys.insert(i, x) B.children.insert(i + 1, R)
def redBlackTo24tree(B): if B == None: return None else: N = btree.BTree() if B.left and B.left.red: pass else: pass if B.right and B.right.red: pass else: pass if N.children[0] == None: N.children = [] return N