ashwin-op / b_and_bplus_tree Goto Github PK
View Code? Open in Web Editor NEWImplementation of B and B+ Trees in python
License: MIT License
Implementation of B and B+ Trees in python
License: MIT License
In the file 'B-Tree.py'.
There are some code problem.
Details are as follows.
In function '_splitChild(self, x, i)',
t = self.t
y = x.children[i]
z = Node(y.leaf)
x.children.insert(i + 1, z)
x.keys.insert(i, y.keys[t - 1])
z.keys = y.keys[t: (2 * t) - 1]
y.keys = y.keys[0: t - 1]
if not y.leaf:
z.children = y.children[t: 2 * t]
y.children = y.children[0: t - 1]
In my view, the correct version is:
z.keys = y.keys[t:(2t)-1]; y.keys = y.keys[0:t-2]
z.children=y.children[t:2t];y.children=y.children[0:t-1]
`def main():
B = BTree(2) ------ i changed
# Insert
customNo = 20 ------ i changed
for i in range(1,customNo+1):
B.insert((i, randint(i, 5 * i)))
B.printTree(B.root)
print()
`
when i run,i get the following error:
`E:\workspace\PycharmProjects\B_and_Bplus_Tree\venv\Scripts\python.exe E:/workspace/PycharmProjects/B_and_Bplus_Tree/B-Tree.py
Level 0 --> 1: (8, 31)
Level 1 --> 1: (4, 10)
Level 2 --> 1: (2, 6)
Level 3 --> 1: (1, 4)
Level 1 --> 1: (12, 30)
Level 2 --> 1: (10, 19)
Level 3 --> 1: (9, 22)
Level 2 --> 3: (14, 32) (16, 42) (18, 25)
Level 3 --> 1: (13, 14)
Level 3 --> 1: (15, 27)
Level 3 --> 1: (17, 58)
Level 3 --> 2: (19, 76) (20, 71)
Key 12 deleted!
Traceback (most recent call last):
File "E:/workspace/PycharmProjects/B_and_Bplus_Tree/B-Tree.py", line 313, in
main()
File "E:/workspace/PycharmProjects/B_and_Bplus_Tree/B-Tree.py", line 298, in main
B.delete(B.root, (toDelete,))
File "E:/workspace/PycharmProjects/B_and_Bplus_Tree/B-Tree.py", line 156, in delete
self.delete(x.children[i], k)
IndexError: list index out of range
Process finished with exit code 1
`
There is a typo in the realization of B-tree.
It's necessary to change in file B-Tree.py
in function _deletePredecessor
line #193 from
return x.pop()
to
return x.keys.pop()
Since argument x
is not a list of keys, it is an object-node that contains a list of keys.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.