-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-3-4 Tree .python
More file actions
46 lines (39 loc) · 1.12 KB
/
2-3-4 Tree .python
File metadata and controls
46 lines (39 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class BTreeNode:
def __init__(self, leaf=True):
self.leaf = leaf
self.keys = []
self.children = []
def display(self, level=0):
print(f"Level {level}: {self.keys}")
if not self.leaf:
for child in self.children:
child.display(level + 1)
class BTree:
def __init__(self, t):
self.root = BTreeNode(True)
self.t = t
def insert(self, k):
root = self.root
if len(root.keys) == (2 * self.t) - 1:
temp = BTreeNode()
self.root = temp
temp.children.append(root)
self.split_child(temp, 0)
self.insert_non_full(temp, k)
else:
self.insert_non_full(root, k)
def insert_non_full(self, x, k):
# Implementation of insertion in a non-full node
# ...
def split_child(self, x, i):
# Implementation of splitting a child node
# ...
def main():
B = BTree(3)
keys = [10, 20, 5, 6, 12, 30, 7, 17]
for key in keys:
B.insert(key)
print("B-tree structure:")
B.display()
if __name__ == '__main__':
main()