diff --git a/pydatastructs/trees/__init__.py b/pydatastructs/trees/__init__.py index f849cc055..df0fc9b0c 100644 --- a/pydatastructs/trees/__init__.py +++ b/pydatastructs/trees/__init__.py @@ -6,7 +6,7 @@ ) from .binary_trees import ( - Node, BinaryTree, BinarySearchTree, BinaryTreeTraversal, AVLTree + TreeNode, BinaryTree, BinarySearchTree, BinaryTreeTraversal, AVLTree ) __all__.extend(binary_trees.__all__) diff --git a/pydatastructs/trees/binary_trees.py b/pydatastructs/trees/binary_trees.py index e680c263e..b5fc123bc 100644 --- a/pydatastructs/trees/binary_trees.py +++ b/pydatastructs/trees/binary_trees.py @@ -1,5 +1,5 @@ from __future__ import print_function, division -from pydatastructs.utils import Node +from pydatastructs.utils import TreeNode from pydatastructs.miscellaneous_data_structures import Stack from pydatastructs.linear_data_structures import ( OneDimensionalArray, DynamicOneDimensionalArray) @@ -23,7 +23,7 @@ class BinaryTree(object): root_data Optional, the root node of the binary tree. - If not of type Node, it will consider + If not of type TreeNode, it will consider root as data and a new root node will be created. key @@ -53,10 +53,10 @@ def __new__(cls, key=None, root_data=None, comp=None, if key == None and root_data != None: raise ValueError('Key required.') key = None if root_data == None else key - root = Node(key, root_data) + root = TreeNode(key, root_data) root.is_root = True obj.root_idx = 0 - obj.tree, obj.size = ArrayForTrees(Node, [root]), 1 + obj.tree, obj.size = ArrayForTrees(TreeNode, [root]), 1 obj.comparator = lambda key1, key2: key1 < key2 \ if comp == None else comp obj.is_order_statistic = is_order_statistic @@ -217,7 +217,7 @@ def insert(self, key, data): self.tree[walk].key = key self.tree[walk].data = data return None - new_node, prev_node, flag = Node(key, data), 0, True + new_node, prev_node, flag = TreeNode(key, data), 0, True while flag: if not self.comparator(key, self.tree[walk].key): if self.tree[walk].right == None: @@ -353,7 +353,7 @@ def select(self, i): Returns ======= - n: Node + n: TreeNode The node with the i-th smallest key References @@ -736,7 +736,7 @@ def depth_first_search(self, order='in_order', node=None): ======= list - Each element is of type 'Node'. + Each element is of type 'TreeNode'. """ if node == None: node = self.tree.root_idx @@ -766,7 +766,7 @@ def breadth_first_search(self, node=None, strategy='queue'): ======= list - Each element of the list is of type `Node`. + Each element of the list is of type `TreeNode`. """ strategies = ('queue',) if strategy not in strategies: diff --git a/pydatastructs/trees/space_partitioning_trees.py b/pydatastructs/trees/space_partitioning_trees.py index a502e0217..e99403f0a 100644 --- a/pydatastructs/trees/space_partitioning_trees.py +++ b/pydatastructs/trees/space_partitioning_trees.py @@ -1,4 +1,4 @@ -from pydatastructs.utils import Node +from pydatastructs.utils import TreeNode # TODO: REPLACE COLLECTIONS QUEUE WITH PYDATASTRUCTS QUEUE from collections import deque as Queue from pydatastructs.linear_data_structures.arrays import _check_type @@ -62,7 +62,7 @@ def _union(self, i1, i2): Helper function for taking union of two intervals. """ - return Node([i1.key[0], i1.key[1], i2.key[2], i2.key[3]], None) + return TreeNode([i1.key[0], i1.key[1], i2.key[2], i2.key[3]], None) def _intersect(self, i1, i2): """ @@ -132,14 +132,14 @@ def build(self): endpoints.sort() elem_int = Queue() - elem_int.append(Node([False, endpoints[0] - 1, endpoints[0], False], None)) + elem_int.append(TreeNode([False, endpoints[0] - 1, endpoints[0], False], None)) i = 0 while i < len(endpoints) - 1: - elem_int.append(Node([True, endpoints[i], endpoints[i], True], None)) - elem_int.append(Node([False, endpoints[i], endpoints[i+1], False], None)) + elem_int.append(TreeNode([True, endpoints[i], endpoints[i], True], None)) + elem_int.append(TreeNode([False, endpoints[i], endpoints[i+1], False], None)) i += 1 - elem_int.append(Node([True, endpoints[i], endpoints[i], True], None)) - elem_int.append(Node([False, endpoints[i], endpoints[i] + 1, False], None)) + elem_int.append(TreeNode([True, endpoints[i], endpoints[i], True], None)) + elem_int.append(TreeNode([False, endpoints[i], endpoints[i] + 1, False], None)) self.tree = [] while len(elem_int) > 1: @@ -163,7 +163,7 @@ def build(self): self.root_idx = -1 for segment in self.segments: - I = Node([True, segment[0], segment[1], True], None) + I = TreeNode([True, segment[0], segment[1], True], None) calls = [self.root_idx] while calls: idx = calls.pop() @@ -204,7 +204,7 @@ def query(self, qx, init_node=None): self.build() if init_node == None: init_node = self.root_idx - qn = Node([True, qx, qx, True], None) + qn = TreeNode([True, qx, qx, True], None) intervals = [] calls = [init_node] while calls: diff --git a/pydatastructs/trees/tests/test_binary_trees.py b/pydatastructs/trees/tests/test_binary_trees.py index c3713196b..4ff0c215b 100644 --- a/pydatastructs/trees/tests/test_binary_trees.py +++ b/pydatastructs/trees/tests/test_binary_trees.py @@ -2,7 +2,7 @@ BinarySearchTree, BinaryTreeTraversal, AVLTree, ArrayForTrees) from pydatastructs.utils.raises_util import raises -from pydatastructs.utils.misc_util import Node +from pydatastructs.utils.misc_util import TreeNode from copy import deepcopy def test_BinarySearchTree(): @@ -138,9 +138,9 @@ def test_AVLTree(): a2.insert(1, 1) assert str(a2) == "[(None, 1, 1, None)]" a3 = AVLTree() - a3.tree = ArrayForTrees(Node, 0) + a3.tree = ArrayForTrees(TreeNode, 0) for i in range(7): - a3.tree.append(Node(i, i)) + a3.tree.append(TreeNode(i, i)) a3.tree[0].left = 1 a3.tree[0].right = 6 a3.tree[1].left = 5 @@ -152,9 +152,9 @@ def test_AVLTree(): "(None, 3, 3, None), (None, 4, 4, None), " "(None, 5, 5, None), (None, 6, 6, None)]") a4 = AVLTree() - a4.tree = ArrayForTrees(Node, 0) + a4.tree = ArrayForTrees(TreeNode, 0) for i in range(7): - a4.tree.append(Node(i, i)) + a4.tree.append(TreeNode(i, i)) a4.tree[0].left = 1 a4.tree[0].right = 2 a4.tree[2].left = 3 @@ -167,21 +167,21 @@ def test_AVLTree(): "(None, 6, 6, None)]") a5 = AVLTree(is_order_statistic=True) - a5.tree = ArrayForTrees(Node, [ - Node(10, 10), - Node(5, 5), - Node(17, 17), - Node(2, 2), - Node(9, 9), - Node(12, 12), - Node(20, 20), - Node(3, 3), - Node(11, 11), - Node(15, 15), - Node(18, 18), - Node(30, 30), - Node(13, 13), - Node(33, 33) + a5.tree = ArrayForTrees(TreeNode, [ + TreeNode(10, 10), + TreeNode(5, 5), + TreeNode(17, 17), + TreeNode(2, 2), + TreeNode(9, 9), + TreeNode(12, 12), + TreeNode(20, 20), + TreeNode(3, 3), + TreeNode(11, 11), + TreeNode(15, 15), + TreeNode(18, 18), + TreeNode(30, 30), + TreeNode(13, 13), + TreeNode(33, 33) ]) a5.tree[0].left, a5.tree[0].right, a5.tree[0].parent, a5.tree[0].height = \ diff --git a/pydatastructs/utils/__init__.py b/pydatastructs/utils/__init__.py index 2a74fd3a7..64f0e982e 100644 --- a/pydatastructs/utils/__init__.py +++ b/pydatastructs/utils/__init__.py @@ -2,6 +2,6 @@ from . import misc_util from .misc_util import ( - Node + TreeNode ) __all__.extend(misc_util.__all__) diff --git a/pydatastructs/utils/misc_util.py b/pydatastructs/utils/misc_util.py index 35a56c8d8..9e7e7bf7d 100644 --- a/pydatastructs/utils/misc_util.py +++ b/pydatastructs/utils/misc_util.py @@ -1,13 +1,13 @@ from __future__ import print_function, division __all__ = [ - 'Node' + 'TreeNode' ] _check_type = lambda a, t: isinstance(a, t) NoneType = type(None) -class Node(object): +class TreeNode(object): """ Represents node in trees.