11from __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
55import heapq
66import 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
0 commit comments