MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / merge_trees

Method merge_trees

data_structures/heap/binomial_heap.py:23–45  ·  view source on GitHub ↗

In-place merge of two binomial trees of equal size. Returns the root of the resulting tree

(self, other)

Source from the content-addressed store, hash-verified

21 self.parent = None
22
23 def merge_trees(self, other):
24 """
25 In-place merge of two binomial trees of equal size.
26 Returns the root of the resulting tree
27 """
28 assert self.left_tree_size == other.left_tree_size, "Unequal Sizes of Blocks"
29
30 if self.val < other.val:
31 other.left = self.right
32 other.parent = None
33 if self.right:
34 self.right.parent = other
35 self.right = other
36 self.left_tree_size = self.left_tree_size * 2 + 1
37 return self
38 else:
39 self.left = other.right
40 self.parent = None
41 if other.right:
42 other.right.parent = self
43 other.right = self
44 other.left_tree_size = other.left_tree_size * 2 + 1
45 return other
46
47
48class BinomialHeap:

Callers 2

merge_heapsMethod · 0.80
insertMethod · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected