Skip to content

Commit

Permalink
Replaced class Node with TreeNode (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
raajtilaksarma authored and czgdp1807 committed Dec 2, 2019
1 parent dc308d8 commit ad9d297
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 41 deletions.
2 changes: 1 addition & 1 deletion pydatastructs/trees/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
)

from .binary_trees import (
Node, BinaryTree, BinarySearchTree, BinaryTreeTraversal, AVLTree
TreeNode, BinaryTree, BinarySearchTree, BinaryTreeTraversal, AVLTree
)
__all__.extend(binary_trees.__all__)

Expand Down
16 changes: 8 additions & 8 deletions pydatastructs/trees/binary_trees.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -353,7 +353,7 @@ def select(self, i):
Returns
=======
n: Node
n: TreeNode
The node with the i-th smallest key
References
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
18 changes: 9 additions & 9 deletions pydatastructs/trees/space_partitioning_trees.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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:
Expand All @@ -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()
Expand Down Expand Up @@ -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:
Expand Down
40 changes: 20 additions & 20 deletions pydatastructs/trees/tests/test_binary_trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 = \
Expand Down
2 changes: 1 addition & 1 deletion pydatastructs/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

from . import misc_util
from .misc_util import (
Node
TreeNode
)
__all__.extend(misc_util.__all__)
4 changes: 2 additions & 2 deletions pydatastructs/utils/misc_util.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit ad9d297

Please sign in to comment.