Skip to content

Commit f8f8b48

Browse files
committed
Refactor get_parent_node to get_parent and add pytest.ini
1 parent 821f433 commit f8f8b48

File tree

9 files changed

+72
-61
lines changed

9 files changed

+72
-61
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,4 @@ ENV/
8787

8888
# Rope project settings
8989
.ropeproject
90-
.idea*
90+
.idea/

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ matrix:
99
dist: xenial
1010
sudo: true
1111
install:
12-
- pip install flake8 mock
12+
- pip install flake8
13+
- pip install mock
1314
- pip install pytest==3.5.1
1415
- pip install pytest-cov==2.5.1
1516
- pip install python-coveralls==2.9.1

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Announcements
5050
Requirements
5151
============
5252

53-
- Python 2.7, 3.4, 3.5, 3.6, 3.7, 3.8
53+
- Python 2.7+ or 3.4+
5454

5555
Installation
5656
============

binarytree/__init__.py

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import absolute_import, unicode_literals, division
22

3-
__all__ = ['Node', 'tree', 'bst', 'heap', 'build']
3+
__all__ = ['Node', 'tree', 'bst', 'heap', 'build', 'get_parent']
44

55
import heapq
66
import random
@@ -26,7 +26,7 @@ def _is_balanced(root):
2626
"""Return the tree height + 1 if balanced, -1 otherwise.
2727
2828
:param root: Root node of the binary tree.
29-
:type root: binarytree.Node | None
29+
:type root: binarytree.Node
3030
:return: Height if the binary tree is balanced, -1 otherwise.
3131
:rtype: int
3232
"""
@@ -45,7 +45,7 @@ def _is_bst(root, min_value=float('-inf'), max_value=float('inf')):
4545
"""Check if the binary tree is a BST (binary search tree).
4646
4747
:param root: Root node of the binary tree.
48-
:type root: binarytree.Node | None
48+
:type root: binarytree.Node
4949
:param min_value: Minimum node value seen.
5050
:type min_value: int | float
5151
:param max_value: Maximum node value seen.
@@ -66,7 +66,7 @@ def _is_symmetric(root):
6666
"""Check if the binary tree is symmetric.
6767
6868
:param root: Root node of the binary tree.
69-
:type root: binarytree.Node | None
69+
:type root: binarytree.Node
7070
:return: True if the binary tree is symmetric, False otherwise.
7171
:rtype: bool
7272
"""
@@ -168,7 +168,7 @@ def _build_tree_string(root, curr_index, index=False, delimiter='-'):
168168
call then combines its left and right sub-boxes to build a larger box etc.
169169
170170
:param root: Root node of the binary tree.
171-
:type root: binarytree.Node | None
171+
:type root: binarytree.Node
172172
:param curr_index: Level-order_ index of the current node (root node is 0).
173173
:type curr_index: int
174174
:param index: If set to True, include the level-order_ node indexes using
@@ -247,7 +247,7 @@ def _get_tree_properties(root):
247247
"""Inspect the binary tree and return its properties (e.g. height).
248248
249249
:param root: Root node of the binary tree.
250-
:rtype: binarytree.Node
250+
:type root: binarytree.Node
251251
:return: Binary tree properties.
252252
:rtype: dict
253253
"""
@@ -321,54 +321,54 @@ def _get_tree_properties(root):
321321
}
322322

323323

324-
def get_parent_node(root, node):
325-
"""Search from the binary tree and return the parent node for require node.
324+
def get_parent(root, child):
325+
"""Search the binary tree and return the parent of given child.
326326
327327
:param root: Root node of the binary tree.
328+
:type: binarytree.Node
329+
:param child: Child node.
328330
:rtype: binarytree.Node
329-
:param node: Require node you want to get its parent node.
330-
:rtype: binarytree.Node
331-
:return: The parent node of require node.
331+
:return: Parent node, or None if missing.
332332
:rtype: binarytree.Node
333333
334334
**Example**:
335335
336-
.. doctest::
336+
.. doctest::
337337
338-
>>> from binarytree import Node, get_parent_node
339-
>>> root = Node(0)
340-
>>> root.left = Node(1)
341-
>>> root.right = Node(2)
342-
>>> root.left.left = Node(3)
343-
>>> print (root)
344-
>>> 0
345-
/ \
346-
1 2
347-
/
348-
3
349-
>>> print (get_parent_node(root, root.left.left))
350-
>>> 1
351-
/
352-
3
338+
>>> from binarytree import Node, get_parent
339+
>>>
340+
>>> root = Node(1)
341+
>>> root.left = Node(2)
342+
>>> root.right = Node(3)
343+
>>> root.left.right = Node(4)
344+
>>>
345+
>>> print(root)
346+
<BLANKLINE>
347+
__1
348+
/ \\
349+
2 3
350+
\\
351+
4
352+
<BLANKLINE>
353+
>>> print(get_parent(root, root.left.right))
354+
<BLANKLINE>
355+
2
356+
\\
357+
4
358+
<BLANKLINE>
353359
"""
354-
if node is None:
360+
if child is None:
355361
return None
356-
node_stack = []
357-
while True:
358-
if root is not None:
359-
node_stack.append(root)
360-
if root.left is node:
361-
return root
362-
else:
363-
root = root.left
364-
elif len(node_stack) > 0:
365-
root = node_stack.pop()
366-
if root.right is node:
367-
return root
362+
363+
stack = [root]
364+
while stack:
365+
node = stack.pop()
366+
if node:
367+
if node.left is child or node.right is child:
368+
return node
368369
else:
369-
root = root.right
370-
else:
371-
break
370+
stack.append(node.left)
371+
stack.append(node.right)
372372
return None
373373

374374

@@ -383,9 +383,9 @@ class Node(object):
383383
:param value: Node value (must be a number).
384384
:type value: int | float | numbers.Number
385385
:param left: Left child node (default: None).
386-
:type left: binarytree.Node | None
386+
:type left: binarytree.Node
387387
:param right: Right child node (default: None).
388-
:type right: binarytree.Node | None
388+
:type right: binarytree.Node
389389
:raise binarytree.exceptions.NodeTypeError: If left or right child node is
390390
not an instance of :class:`binarytree.Node`.
391391
:raise binarytree.exceptions.NodeValueError: If node value is not a number

binarytree/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '5.0.0' # pragma: no cover
1+
__version__ = '5.1.0' # pragma: no cover

docs/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ algorithms. Heaps and BSTs (binary search trees) are also supported.
1111
Requirements
1212
============
1313

14-
- Python 2.7, 3.5, 3.6, 3.7, 3.8
14+
- Python 2.7+ or 3.4+
1515

1616
Installation
1717
============

docs/specs.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ functions:
99
* :func:`binarytree.tree`
1010
* :func:`binarytree.bst`
1111
* :func:`binarytree.heap`
12+
* :func:`binarytree.get_parent`
1213

1314
Class: binarytree.Node
1415
======================
@@ -38,3 +39,8 @@ Function: binarytree.heap
3839
=========================
3940

4041
.. autofunction:: binarytree.heap
42+
43+
Function: binarytree.get_parent
44+
===============================
45+
46+
.. autofunction:: binarytree.get_parent

pytest.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[pytest]
2+
python_files = tests.py test_*.py *_tests.py
3+
addopts = -vv -p no:warnings --cov=binarytree
4+
norecursedirs = venv htmlcov build dist .idea .git

tests/test_tree.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pytest
77

88
from binarytree import Node, build, tree, bst, heap
9-
from binarytree import get_parent_node
9+
from binarytree import get_parent
1010
from binarytree.exceptions import (
1111
NodeValueError,
1212
NodeIndexError,
@@ -887,18 +887,18 @@ def test_heap_float_values():
887887
assert root.size == root_copy.size
888888

889889

890-
@pytest.mark.order14
891-
def test_get_parent_node():
890+
def test_get_parent():
892891
root = Node(0)
893892
root.left = Node(1)
894893
root.right = Node(2)
895894
root.left.left = Node(3)
896895
root.right.right = Node(4)
897-
assert get_parent_node(root, root.left.left) == root.left
898-
assert get_parent_node(root, root.left) == root
899-
assert get_parent_node(root, root) is None
900-
assert get_parent_node(root, root.right.right) == root.right
901-
assert get_parent_node(root, root.right) == root
902-
assert get_parent_node(root, Node(5)) is None
903-
assert get_parent_node(None, root.left) is None
904-
assert get_parent_node(root, None) is None
896+
897+
assert get_parent(root, root.left.left) == root.left
898+
assert get_parent(root, root.left) == root
899+
assert get_parent(root, root) is None
900+
assert get_parent(root, root.right.right) == root.right
901+
assert get_parent(root, root.right) == root
902+
assert get_parent(root, Node(5)) is None
903+
assert get_parent(None, root.left) is None
904+
assert get_parent(root, None) is None

0 commit comments

Comments
 (0)