Last active
August 10, 2016 10:44
-
-
Save tamimcsedu19/b24f4f146dee2e19840cf89cd8e6cf55 to your computer and use it in GitHub Desktop.
B plus tree implementation
This file contains hidden or 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
import math | |
class BTree(): | |
def __init__(self,n): | |
self.root = None | |
self.n = n | |
def find(self,V): | |
C = self.root | |
while C['isLeaf'] == False: | |
i = None | |
j=0 | |
while True: | |
if j == self.n: | |
break | |
if C['K'][j] is None: | |
break | |
if V <= C['K'][j]: | |
i=j | |
break | |
if i == None: | |
C = C['P'][j-1] | |
elif V == C['K'][i]: | |
C = C['P'][i+1] | |
else: | |
C = C['P'][i] | |
return C | |
def count(self,L): | |
n = 0 | |
for i in range(len(L)): | |
if L[i] is not None: | |
n+=1 | |
return n | |
def insert(self,K,P): | |
L = None | |
if self.root == None: | |
L = {'isLeaf':True,'K':[None]*self.n,'P':[None]*self.n} | |
self.root = L | |
else: | |
L = self.find(K) | |
if self.count(L['K']) < self.n-1: | |
self.insert_in_leaf(L,K,P) | |
else: | |
Lp = {'isLeaf':True,'K':[None]*self.n,'P':[None]*self.n} | |
T = {'isLeaf':True,'K':[None]*self.n,'P':[None]*self.n} | |
T['K'] = list(L['K']) | |
T['P'] = list(L['P']) | |
self.insert_in_leaf(T,K,P) | |
Lp['P'][-1] = L['P'][-1] | |
L['P'][-1] = Lp | |
for i in range(self.n-1): | |
L['P'][i] = None | |
L['K'][i] = None | |
for i in range(int(math.ceil(self.n/2))): | |
L['P'][i] = T['P'][i] | |
L['K'][i] = T['K'][i] | |
j = 0 | |
for i in range(int(math.ceil(self.n/2)),len(T['P'])-1): | |
Lp['P'][j] = T['P'][i] | |
Lp['K'][j] = T['K'][i] | |
L['isLeaf'] = False | |
self.insert_in_parent(L,Lp['K'][0],Lp) | |
def insert_in_leaf(self,L,K,P): | |
if self.count(L['K']) == 0 or K < L['K'][0]: | |
L['K'] = [K] + L['K'] | |
L['P'] = [P] + L['P'] | |
L['K'].pop() | |
L['P'].pop() | |
else: | |
j = 0 | |
while L['K'][j] is not None and L['K'][j] < K: | |
j+=1 | |
L['K'] = L['K'][0:j] + [K] + L['K'][j:] | |
L['P'] = L['P'][0:j] + [P] + L['P'][j:] | |
L['K'].pop() | |
L['P'].pop() | |
def insert_in_parent(self,N,Kp,Np): | |
if N == self.root: | |
R = {'isLeaf':False,'K':[None]*self.n,'P':[None]*self.n} | |
R['K'][0] = Kp | |
R['P'][0] = N | |
R['P'][1] = Np | |
self.root = R | |
return | |
P = N['parent'] | |
if count(P['K']) < self.n-1: | |
j = 0 | |
while P['P'][j] != N: | |
j+=1 | |
P['K'] = P['K'][0:j] + [Kp] + P['K'][j:] | |
P['P'] = P['P'][0:j] + [Np] + P['P'][j:] | |
P['K'].pop() | |
P['P'].pop() | |
else: | |
T = {} | |
j = 0 | |
while P['P'][j] != N: | |
j+=1 | |
T['K'] = P['K'][0:j] + [Kp] + P['K'][j:] | |
T['P'] = P['P'][0:j] + [Np] + P['P'][j:] | |
for i in range(math.ceil(n/2)): | |
P['P'][i] = T['P'][i] | |
Kpp = T['K'][math.ceil(n/2)-1] | |
Pp = {'isLeaf':False,'K':[None]*self.n,'P':[None]*self.n} | |
j = 0 | |
for i in range(math.ceil(n/2),len(T['P'])): | |
Pp['P'][j] = T['P'][i] | |
insert_in_parent(P,Kpp,Pp) | |
t = BTree(4) | |
for i in range(4): | |
record = {'val':i} | |
t.insert(i,record) | |
print(t.root) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment