-
Notifications
You must be signed in to change notification settings - Fork 312
Added implementation for M-ary tree #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d77922a
Added implementation for M-ary tree
vibhu18116 74dab8f
Removing trailing white spaces
vibhu18116 13bbd98
Fixed _ in names and MAryTree to global namespace
vibhu18116 6b2cb62
Merge branch 'master' into m-ary_tree
vibhu18116 d9bcef7
Updated the ordering of parameters.
vibhu18116 ac26f9a
Removed tab
czgdp1807 ebe7541
Update pydatastructs/utils/misc_util.py
czgdp1807 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,162 @@ | ||
from pydatastructs.utils import MAryTreeNode | ||
from pydatastructs.linear_data_structures.arrays import ArrayForTrees | ||
|
||
__all__ = [ | ||
'MAryTree' | ||
] | ||
|
||
class MAryTree(object): | ||
""" | ||
Abstract m-ary tree. | ||
|
||
Parameters | ||
========== | ||
|
||
root_data | ||
Optional, the root node of the binary tree. | ||
If not of type MAryTreeNode, it will consider | ||
root as data and a new root node will | ||
be created. | ||
key | ||
Required if tree is to be instantiated with | ||
root otherwise not needed. | ||
comp: lambda | ||
Optional, A lambda function which will be used | ||
for comparison of keys. Should return a | ||
bool value. By default it implements less | ||
than operator. | ||
is_order_statistic: bool | ||
Set it to True, if you want to use the | ||
order statistic features of the tree. | ||
max_children | ||
Optional, specifies the maximum number of children | ||
a node can have. Defaults to 2 in case nothing is | ||
specified. | ||
|
||
References | ||
========== | ||
|
||
.. [1] https://en.wikipedia.org/wiki/M-ary_tree | ||
""" | ||
|
||
__slots__ = ['root_idx', 'max_children', 'comparator', 'tree', 'size', | ||
'is_order_statistic'] | ||
|
||
|
||
def __new__(cls, key=None, root_data=None, comp=None, | ||
is_order_statistic=False, max_children=2): | ||
obj = object.__new__(cls) | ||
if key is None and root_data is not None: | ||
raise ValueError('Key required.') | ||
key = None if root_data is None else key | ||
root = MAryTreeNode(key, root_data) | ||
root.is_root = True | ||
obj.root_idx = 0 | ||
obj.max_children = max_children | ||
obj.tree, obj.size = ArrayForTrees(MAryTreeNode, [root]), 1 | ||
obj.comparator = lambda key1, key2: key1 < key2 \ | ||
if comp is None else comp | ||
obj.is_order_statistic = is_order_statistic | ||
return obj | ||
|
||
def insert(self, key, data): | ||
""" | ||
Inserts data by the passed key using iterative | ||
algorithm. | ||
|
||
Parameters | ||
========== | ||
|
||
key | ||
The key for comparison. | ||
data | ||
The data to be inserted. | ||
|
||
Returns | ||
======= | ||
|
||
None | ||
""" | ||
raise NotImplementedError("This is an abstract method.") | ||
|
||
def delete(self, key, **kwargs): | ||
""" | ||
Deletes the data with the passed key | ||
using iterative algorithm. | ||
|
||
Parameters | ||
========== | ||
|
||
key | ||
The key of the node which is | ||
to be deleted. | ||
|
||
Returns | ||
======= | ||
|
||
True | ||
If the node is deleted successfully. | ||
None | ||
If the node to be deleted doesn't exists. | ||
|
||
Note | ||
==== | ||
|
||
The node is deleted means that the connection to that | ||
node are removed but the it is still in tree. | ||
""" | ||
raise NotImplementedError("This is an abstract method.") | ||
|
||
def search(self, key, **kwargs): | ||
""" | ||
Searches for the data in the binary search tree | ||
using iterative algorithm. | ||
|
||
Parameters | ||
========== | ||
|
||
key | ||
The key for searching. | ||
parent: bool | ||
If true then returns index of the | ||
parent of the node with the passed | ||
key. | ||
By default, False | ||
|
||
Returns | ||
======= | ||
|
||
int | ||
If the node with the passed key is | ||
in the tree. | ||
tuple | ||
The index of the searched node and | ||
the index of the parent of that node. | ||
None | ||
In all other cases. | ||
""" | ||
raise NotImplementedError("This is an abstract method.") | ||
|
||
def to_binary_tree(self): | ||
""" | ||
Converts an m-ary tree to a binary tree. | ||
|
||
Returns | ||
======= | ||
|
||
TreeNode | ||
The root of the newly created binary tree. | ||
""" | ||
raise NotImplementedError("This is an abstract method.") | ||
|
||
|
||
def __str__(self): | ||
to_be_printed = ['' for i in range(self.tree._last_pos_filled + 1)] | ||
for i in range(self.tree._last_pos_filled + 1): | ||
if self.tree[i] is not None: | ||
node = self.tree[i] | ||
to_be_printed[i] = (node.key, node.data) | ||
for j in node.children: | ||
if j is not None: | ||
to_be_printed[i].append(j) | ||
return str(to_be_printed) |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.