MCPcopy
hub / github.com/keon/algorithms / _split_child

Method _split_child

algorithms/data_structures/b_tree.py:67–86  ·  view source on GitHub ↗

Split a full child node into two nodes. Args: parent: The parent node whose child is being split. child_index: The index of the child to split.

(self, parent: Node, child_index: int)

Source from the content-addressed store, hash-verified

65 self.root = Node()
66
67 def _split_child(self, parent: Node, child_index: int) -> None:
68 """Split a full child node into two nodes.
69
70 Args:
71 parent: The parent node whose child is being split.
72 child_index: The index of the child to split.
73 """
74 new_right_child = Node()
75 half_max = self.max_number_of_keys // 2
76 child = parent.children[child_index]
77 middle_key = child.keys[half_max]
78 new_right_child.keys = child.keys[half_max + 1 :]
79 child.keys = child.keys[:half_max]
80
81 if not child.is_leaf:
82 new_right_child.children = child.children[half_max + 1 :]
83 child.children = child.children[: half_max + 1]
84
85 parent.keys.insert(child_index, middle_key)
86 parent.children.insert(child_index + 1, new_right_child)
87
88 def insert_key(self, key: int) -> None:
89 """Insert a key into the B-tree.

Callers 2

insert_keyMethod · 0.95

Calls 2

NodeClass · 0.70
insertMethod · 0.45

Tested by

no test coverage detected