Advertisement
Guest User

Axel PERCIBALLI Python B+Tree

a guest
Dec 16th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.88 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #-*- coding:UTF-8-*-
  3. import sys
  4.  
  5. class Item:
  6.     def __init__(self, key, value):
  7.         self.key=key
  8.         self.value=value
  9.  
  10.  
  11. class Node:
  12.     def __init__(self, parent=None, nextNode=None):
  13.         self.parent   = parent
  14.         self.children = []
  15.         self.keys     = []
  16.         self.values   = []
  17.         self.nextNode = nextNode
  18.  
  19.  
  20.     def add(self,item):
  21.         if (not self.children) : # Leaf
  22.             if (len(self.keys)<3): # Hardcoded to look like the one in the example
  23.                 self.keys.append(item.key)
  24.                 self.keys.sort()
  25.                 self.values.insert(self.keys.index(item.key),item.value) #inserting the value at same index as key
  26.  
  27.             else:
  28.                 #LEAF SPLIT
  29.                 newparentnode=Node()
  30.                 if (not self.parent): #case where leaf is root
  31.                        
  32.                     if (self.keys[2]<item.key): #if our item has a smaller key than the biggest key of the node
  33.                         newparentnode.keys=[self.keys[2]] #[1,5,8] + 9 becomes [1,5] [8,9]
  34.                         print "New node created with node key :",self.keys[2]
  35.  
  36.                         newchildrennode=Node(newparentnode) # creating following children node
  37.                         newchildrennode.keys=[self.keys[2],item.key]
  38.                         newchildrennode.values=[self.values[2],item.value]
  39.  
  40.                        
  41.                     else: # if our item has a bigger key than the node's biggest key
  42.                         newparentnode.keys=[item.key]
  43.                         print "New node created with node key :",item.key
  44.  
  45.                         newchildrennode=Node(newparentnode)
  46.                         newchildrennode.keys=[item.key,self.keys[2]]
  47.                         newchildrennode.values=[item.value,self.values[2]]
  48.                     self.parent=newparentnode
  49.                     newchildrennode.parent=newparentnode
  50.                     newparentnode.children=[self,newchildrennode]
  51.                     self.values.pop() # removing last value of the node
  52.                     self.keys.pop() #removing last key of the node
  53.                     self.nextNode=newchildrennode
  54.                     t.root=newparentnode
  55.                
  56.                 else: #case where leaf is not root
  57.  
  58.                     #preparing the two new nodes
  59.                     if (self.keys[2]<item.key):
  60.                         newchildrennode=Node(newparentnode) # creating following children node
  61.                         newchildrennode.keys=[self.keys[2],item.key]
  62.                         newchildrennode.values=[self.values[2],item.value]
  63.                     else:
  64.                         newchildrennode=Node(newparentnode)
  65.                         newchildrennode.keys=[item.key,self.keys[2]]
  66.                         newchildrennode.values=[item.value,self.values[2]]
  67.                    
  68.                     #if former node parent is not full
  69.                     if (len(self.parent.keys)<3):
  70.                         newchildrennode.parent=self.parent
  71.                         self.parent.children.append(newchildrennode)
  72.                         self.parent.keys.append(newchildrennode.keys[0])
  73.                         self.values.pop() # removing last value of the node
  74.                         self.keys.pop() #removing last key of the node
  75.                         self.nextNode=newchildrennode
  76.  
  77.                     #if former node parent is full, we create a new node and attach it to the previous parent's parent
  78.                     else:
  79.                         newchildrennode.parent=Node()
  80.                         newchildrennode.parent.keys=[self.key]
  81.                         newchildrennode.parent.children=newchildrennode
  82.                         self.values.pop() # removing last value of the node
  83.                         self.keys.pop() #removing last key of the node
  84.                         self.nextNode=newchildrennode
  85.                         self.parent.add(newchildrennode.parent)#recursive split the level above
  86.  
  87.  
  88.  
  89.         else: #not a leaf
  90.  
  91.             for child in self.keys:
  92.                 if item.key<child: #if we're behind an item with a bigger key than ours
  93.                     self.children[self.keys.index(child)].add(item)
  94.                     return 0
  95.                
  96.             self.children[len(self.children)-1].add(item) # if our item has a bigger key than the biggest of the node
  97.        
  98.         return 0
  99.  
  100.  
  101.     def search(self,key):
  102.        
  103.         if (not self.children): #leaf node
  104.             for x in self.keys:
  105.                 if (key==x):
  106.                     return self.values[self.keys.index(x)]
  107.         else: #not leaf nodes
  108.             for x in self.keys:
  109.                 if (key<x):
  110.                     return self.children[self.keys.index(x)].search(key) #terminal recursivity
  111.                 return self.children[len(self.keys)].search(key)    
  112.  
  113.  
  114.     def printnode(self):
  115.         toprint=""
  116.         if (not self.children): #leaf
  117.             for key in self.keys:
  118.                 return str((key,self.values[self.keys.index(key)]))
  119.         else:
  120.             for children in self.children:
  121.                 toprint += "\n\t A child with keys :"
  122.  
  123.                 for key in children.keys:
  124.                     toprint+=str(key)+" "
  125.         return "Root has the key(s) : "+str(t.root.keys)+toprint #Not done yet
  126.  
  127.  
  128. class Tree:
  129.     def __init__(self):
  130.         self.root = None # We init the tree with an empty node
  131.         print "Empty tree created"
  132.  
  133.     def insert(self, item):
  134.         if (not self.root): # First node inserted in tree
  135.             self.root=Node()
  136.             self.root.add(item)
  137.             print "Added root node to tree"
  138.         else:
  139.             self.root.add(item)
  140.  
  141.     def search(self,key):
  142.         if (not self.root):
  143.             print "You need to add an item first."
  144.             return -1
  145.         else:
  146.             return self.root.search(key)
  147.  
  148.     def printree(self):
  149.         print "\n \nCalling print tree method "
  150.         print(self.root.printnode())
  151.  
  152.  
  153.  
  154. print("\n\n")
  155. t=Tree()
  156. i=Item(1,(1,2))
  157. i2=Item(2,(3,4))
  158. i3=Item(4,(5,6))
  159. i4=Item(5,(7,8))
  160. i5=Item(6,(9,10))
  161. i6=Item(3,(11,12))
  162. i7=Item(7,(13,14))
  163. i8=Item(8,(15,16))
  164. i9=Item(9,(17,18))
  165. i10=Item(10,(19,20))
  166. i11=Item(11,(21,22))
  167.  
  168. t.insert(i)
  169. #t.printree()
  170.  
  171. t.insert(i2)
  172. #t.printree()
  173.  
  174. t.insert(i3)
  175. #t.printree()
  176.  
  177. t.insert(i4)
  178. #t.printree()
  179.  
  180. t.insert(i5)
  181. #t.printree()
  182.  
  183. t.insert(i6)
  184. #t.printree()
  185.  
  186. t.insert(i7)
  187. #t.printree()
  188.  
  189. t.insert(i8)
  190. #t.printree()
  191.  
  192. t.insert(i9)
  193. #t.printree()
  194.  
  195. t.insert(i10)
  196. t.printree()
  197.  
  198. #Non-leaf node split has some issues
  199. #t.insert(i11)
  200. #t.printree()
  201.  
  202. print("\n")
  203. print("Item with key 1 has the values : "+str(t.search(1)))
  204. print("Item with key 8 has the values : "+str(t.search(8)))
  205. print("Item with key 10 has the values : "+str(t.search(10)))
  206.  
  207. print "\nProgram ending.\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement