Skip to content
Snippets Groups Projects
Commit 695fef09 authored by Mamadu-lamarana Bah's avatar Mamadu-lamarana Bah :speech_balloon:
Browse files

code review

parent 74f40c10
Branches
No related tags found
No related merge requests found
#import Node as Bnode
from Node import Node
#from Visualization import Visualization
class Btree() :
......@@ -36,20 +34,20 @@ class Btree() :
# >>> Btree(2, Node([4, 10], [Node([1, 3]), Node([25]), Node([50])])).insertion(4)
# True
>>> c = Btree(2,Node([1, 10]))
>>> print(c.insertion(15))
>>> c.insertion(15)
True
"""
print(self.root)
fini, milieu, g, d = self.root.insert(value, self.k)
if (not fini):
new_root = Node([milieu], [g, d])
self.root = new_root
print(self.root)
return True
def linearisation(self):
"""
Exemple(s):
>>> a =Btree(2, Node([12, 42], [Node([2, 3]), Node([25]), Node([50])]))
>>> a.linearisation()
......@@ -59,20 +57,36 @@ class Btree() :
def isBalance(self):
"""
Verification de l'équilibrage de l'arbre
• Toutes les feuilles ont la même profondeur, à savoir la hauteur h de l’arbre.
• k/2 ≤ n ≤ k. Taux de remplissage min = 50%, et moyen 75%.
n = nombre de clés contenus dans le nœud x
• Si x n’est pas une feuille :
• pour 2<=i<=n, pour toute clef x du filsi : clesi <= x <=clesi+1
• Pour toute clef x du fils1 : x <= cles1
• Si x n’est pas la racine, n est compris entre k/2 et k.
Return :
bool : true si l'arbre est bien équilibré et false sinon.
Exemple(s) :
>>> b= Btree(3, Node([12,25,50], [Node([1,11]), Node([20]), Node([30]), Node([100])]))
>>> b = Btree(3, Node([12,25,50], [Node([1,11]), Node([20]), Node([30]), Node([100])]))
>>> b.isBalance()
True
>>> Btree(3, Node([12,25,50,62], [Node([1,11]), Node([20]), Node([30]), Node([100])])).isBalance()
False
>>> Btree(3, Node([12,25,50], [Node([15]), Node([30]), Node([100])])).isBalance()
False
>>> Btree(3, Node([12,25,50])).isBalance()
True
"""
(ok, _, _, _) = self.root.is_ArbreB(self.k, True)
return ok
def equals(self, otherObject):
if not isinstance(otherObject, Btree):
return False
return (self.root.keys == otherObject.root.keys and self.k == otherObject.k)
def __repr__(self) :
"""
representataion d'un btree
"""
return f"Btree({self.root})"
if __name__ == '__main__':
......
......@@ -43,6 +43,12 @@ class Node() :
def is_ArbreB(self, k, is_root = False):
"""
Verification de l'équilibrage de l'arbre
Params :
k => number of keys in node
Return :
(ok, height, mini, maxi)
Exemple(s) :
>>> Node([5]).is_ArbreB(2)
(True, 0, 5, 5)
......@@ -57,6 +63,8 @@ class Node() :
(False, 1, 0, 5)
>>> Node([5], [Node([0]), Node([7], [Node([6]), Node([8])])]).is_ArbreB(2)
(False, 2, 0, 8)
>>> Node([12,25,50], [Node([1,11]), Node([20]), Node([30]), Node([100])]).is_ArbreB(3)
(True, 1, 1, 100)
"""
ok = (is_root or (k//2) <= len(self.keys)) and len(self.keys) <= k
ok = ok and all(self.keys[i] < self.keys[i+1] for i in range(len(self.keys) - 1))
......
......@@ -20,7 +20,7 @@ class experimentation:
for n in [2, 4, 5] + list(range(6, 37, 2)) + [7, 9, 11, 13]:
self.btree.insertion(n)
# TODO Visualization(self.btree).render()
#Visualization(self.btree).render()
print(self.btree)
......
import unittest
from Node import *
class experimentation:
def setUp(self):
self.node = Node([12, 42], )
if __name__ == "__main__":
import sys
sys.path.insert(1, './src')
sys.path.insert(2, '../src')
from Node import *
unittest.main()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment