-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added BinomialTreeNode * added BinomialTree * added BinomialHeap
- Loading branch information
Showing
8 changed files
with
485 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
pydatastructs/miscellaneous_data_structures/binomial_trees.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from pydatastructs.utils.misc_util import BinomialTreeNode, _check_type | ||
|
||
__all__ = [ | ||
'BinomialTree' | ||
] | ||
|
||
class BinomialTree(object): | ||
""" | ||
Represents binomial trees | ||
Parameters | ||
========== | ||
root: BinomialTreeNode | ||
The root of the binomial tree. | ||
By default, None | ||
order: int | ||
The order of the binomial tree. | ||
By default, None | ||
Examples | ||
======== | ||
>>> from pydatastructs import BinomialTree, BinomialTreeNode | ||
>>> root = BinomialTreeNode(1, 1) | ||
>>> tree = BinomialTree(root, 0) | ||
>>> tree.is_empty | ||
False | ||
References | ||
========== | ||
.. [1] https://en.wikipedia.org/wiki/Binomial_heap | ||
""" | ||
__slots__ = ['root', 'order'] | ||
|
||
def __new__(cls, root=None, order=None): | ||
if root is not None and \ | ||
not _check_type(root, BinomialTreeNode): | ||
raise TypeError("%s i.e., root should be of " | ||
"type BinomialTreeNode."%(root)) | ||
if order is not None and not _check_type(order, int): | ||
raise TypeError("%s i.e., order should be of " | ||
"type int."%(order)) | ||
obj = object.__new__(cls) | ||
if root is not None: | ||
root.is_root = True | ||
obj.root = root | ||
obj.order = order | ||
return obj | ||
|
||
def add_sub_tree(self, other_tree): | ||
""" | ||
Adds a sub tree to current tree. | ||
Parameters | ||
========== | ||
other_tree: BinomialTree | ||
Raises | ||
====== | ||
ValueError: If order of the two trees | ||
are different. | ||
""" | ||
if not _check_type(other_tree, BinomialTree): | ||
raise TypeError("%s i.e., other_tree should be of " | ||
"type BinomialTree"%(other_tree)) | ||
if self.order != other_tree.order: | ||
raise ValueError("Orders of both the trees should be same.") | ||
self.root.children.append(other_tree.root) | ||
other_tree.root.parent = self.root | ||
other_tree.root.is_root = False | ||
self.order += 1 | ||
|
||
@property | ||
def is_empty(self): | ||
return self.root is None |
17 changes: 17 additions & 0 deletions
17
pydatastructs/miscellaneous_data_structures/tests/test_binomial_trees.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from pydatastructs.miscellaneous_data_structures.binomial_trees import BinomialTree | ||
from pydatastructs.utils.raises_util import raises | ||
from pydatastructs.utils.misc_util import BinomialTreeNode | ||
|
||
# only tests the corner cases | ||
def test_BinomialTree(): | ||
assert raises(TypeError, lambda: BinomialTree(1, 1)) | ||
assert raises(TypeError, lambda: BinomialTree(None, 1.5)) | ||
|
||
bt = BinomialTree() | ||
assert raises(TypeError, lambda: bt.add_sub_tree(None)) | ||
bt1 = BinomialTree(BinomialTreeNode(1, 1), 0) | ||
node = BinomialTreeNode(2, 2) | ||
node.add_children(BinomialTreeNode(3, 3)) | ||
bt2 = BinomialTree(node, 1) | ||
assert raises(ValueError, lambda: bt1.add_sub_tree(bt2)) | ||
assert bt1.is_empty is False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.