1
1
from __future__ import absolute_import , unicode_literals , division
2
2
3
- __all__ = ['Node' , 'tree' , 'bst' , 'heap' , 'build' ]
3
+ __all__ = ['Node' , 'tree' , 'bst' , 'heap' , 'build' , 'get_parent' ]
4
4
5
5
import heapq
6
6
import random
@@ -26,7 +26,7 @@ def _is_balanced(root):
26
26
"""Return the tree height + 1 if balanced, -1 otherwise.
27
27
28
28
:param root: Root node of the binary tree.
29
- :type root: binarytree.Node | None
29
+ :type root: binarytree.Node
30
30
:return: Height if the binary tree is balanced, -1 otherwise.
31
31
:rtype: int
32
32
"""
@@ -45,7 +45,7 @@ def _is_bst(root, min_value=float('-inf'), max_value=float('inf')):
45
45
"""Check if the binary tree is a BST (binary search tree).
46
46
47
47
:param root: Root node of the binary tree.
48
- :type root: binarytree.Node | None
48
+ :type root: binarytree.Node
49
49
:param min_value: Minimum node value seen.
50
50
:type min_value: int | float
51
51
:param max_value: Maximum node value seen.
@@ -66,7 +66,7 @@ def _is_symmetric(root):
66
66
"""Check if the binary tree is symmetric.
67
67
68
68
:param root: Root node of the binary tree.
69
- :type root: binarytree.Node | None
69
+ :type root: binarytree.Node
70
70
:return: True if the binary tree is symmetric, False otherwise.
71
71
:rtype: bool
72
72
"""
@@ -168,7 +168,7 @@ def _build_tree_string(root, curr_index, index=False, delimiter='-'):
168
168
call then combines its left and right sub-boxes to build a larger box etc.
169
169
170
170
:param root: Root node of the binary tree.
171
- :type root: binarytree.Node | None
171
+ :type root: binarytree.Node
172
172
:param curr_index: Level-order_ index of the current node (root node is 0).
173
173
:type curr_index: int
174
174
:param index: If set to True, include the level-order_ node indexes using
@@ -247,7 +247,7 @@ def _get_tree_properties(root):
247
247
"""Inspect the binary tree and return its properties (e.g. height).
248
248
249
249
:param root: Root node of the binary tree.
250
- :rtype : binarytree.Node
250
+ :type root : binarytree.Node
251
251
:return: Binary tree properties.
252
252
:rtype: dict
253
253
"""
@@ -321,54 +321,54 @@ def _get_tree_properties(root):
321
321
}
322
322
323
323
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 .
326
326
327
327
:param root: Root node of the binary tree.
328
+ :type: binarytree.Node
329
+ :param child: Child node.
328
330
: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.
332
332
:rtype: binarytree.Node
333
333
334
334
**Example**:
335
335
336
- .. doctest::
336
+ .. doctest::
337
337
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>
353
359
"""
354
- if node is None :
360
+ if child is None :
355
361
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
368
369
else :
369
- root = root .right
370
- else :
371
- break
370
+ stack .append (node .left )
371
+ stack .append (node .right )
372
372
return None
373
373
374
374
@@ -383,9 +383,9 @@ class Node(object):
383
383
:param value: Node value (must be a number).
384
384
:type value: int | float | numbers.Number
385
385
:param left: Left child node (default: None).
386
- :type left: binarytree.Node | None
386
+ :type left: binarytree.Node
387
387
:param right: Right child node (default: None).
388
- :type right: binarytree.Node | None
388
+ :type right: binarytree.Node
389
389
:raise binarytree.exceptions.NodeTypeError: If left or right child node is
390
390
not an instance of :class:`binarytree.Node`.
391
391
:raise binarytree.exceptions.NodeValueError: If node value is not a number
0 commit comments