Skip to content

Commit

Permalink
Merge pull request #24 from DechinPhy/master
Browse files Browse the repository at this point in the history
Add get_parent_node function
  • Loading branch information
joowani authored May 22, 2020
2 parents 1d384dd + 7c1e41a commit 821f433
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
51 changes: 51 additions & 0 deletions binarytree/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,57 @@ def _get_tree_properties(root):
}


def get_parent_node(root, node):
"""Search from the binary tree and return the parent node for require node.
:param root: Root node of the binary tree.
:rtype: binarytree.Node
:param node: Require node you want to get its parent node.
:rtype: binarytree.Node
:return: The parent node of require node.
:rtype: binarytree.Node
**Example**:
.. doctest::
>>> from binarytree import Node, get_parent_node
>>> root = Node(0)
>>> root.left = Node(1)
>>> root.right = Node(2)
>>> root.left.left = Node(3)
>>> print (root)
>>> 0
/ \
1 2
/
3
>>> print (get_parent_node(root, root.left.left))
>>> 1
/
3
"""
if node is None:
return None
node_stack = []
while True:
if root is not None:
node_stack.append(root)
if root.left is node:
return root
else:
root = root.left
elif len(node_stack) > 0:
root = node_stack.pop()
if root.right is node:
return root
else:
root = root.right
else:
break
return None


class Node(object):
"""Represents a binary tree node.
Expand Down
18 changes: 18 additions & 0 deletions tests/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pytest

from binarytree import Node, build, tree, bst, heap
from binarytree import get_parent_node
from binarytree.exceptions import (
NodeValueError,
NodeIndexError,
Expand Down Expand Up @@ -884,3 +885,20 @@ def test_heap_float_values():
assert root.min_leaf_depth == root_copy.min_leaf_depth
assert root.min_node_value == root_copy.min_node_value + 0.1
assert root.size == root_copy.size


@pytest.mark.order14
def test_get_parent_node():
root = Node(0)
root.left = Node(1)
root.right = Node(2)
root.left.left = Node(3)
root.right.right = Node(4)
assert get_parent_node(root, root.left.left) == root.left
assert get_parent_node(root, root.left) == root
assert get_parent_node(root, root) is None
assert get_parent_node(root, root.right.right) == root.right
assert get_parent_node(root, root.right) == root
assert get_parent_node(root, Node(5)) is None
assert get_parent_node(None, root.left) is None
assert get_parent_node(root, None) is None

0 comments on commit 821f433

Please sign in to comment.