Generate a random heap and return its root node. :param height: Height of the heap (default: 3, range: 0 - 9 inclusive). :type height: int :param is_max: If set to True (default: True), generate a max heap. If set to False, generate a min heap. A binary tree with only the root n
(
height: int = 3,
is_max: bool = True,
is_perfect: bool = False,
letters: bool = False,
)
| 2466 | |
| 2467 | |
| 2468 | def heap( |
| 2469 | height: int = 3, |
| 2470 | is_max: bool = True, |
| 2471 | is_perfect: bool = False, |
| 2472 | letters: bool = False, |
| 2473 | ) -> Optional[Node]: |
| 2474 | """Generate a random heap and return its root node. |
| 2475 | |
| 2476 | :param height: Height of the heap (default: 3, range: 0 - 9 inclusive). |
| 2477 | :type height: int |
| 2478 | :param is_max: If set to True (default: True), generate a max heap. If set |
| 2479 | to False, generate a min heap. A binary tree with only the root node is |
| 2480 | considered both a min and max heap. |
| 2481 | :type is_max: bool |
| 2482 | :param is_perfect: If set to True (default: False), a perfect heap with all |
| 2483 | levels filled is returned. If set to False, a perfect heap may still be |
| 2484 | generated by chance. |
| 2485 | :type is_perfect: bool |
| 2486 | :param letters: If set to True (default: False), uppercase alphabet letters are |
| 2487 | used for node values instead of numbers. |
| 2488 | :type letters: bool |
| 2489 | :return: Root node of the heap. |
| 2490 | :rtype: binarytree.Node |
| 2491 | :raise binarytree.exceptions.TreeHeightError: If height is invalid. |
| 2492 | |
| 2493 | **Example**: |
| 2494 | |
| 2495 | .. doctest:: |
| 2496 | |
| 2497 | >>> from binarytree import heap |
| 2498 | >>> |
| 2499 | >>> root = heap() |
| 2500 | >>> |
| 2501 | >>> root.height |
| 2502 | 3 |
| 2503 | >>> root.is_max_heap |
| 2504 | True |
| 2505 | |
| 2506 | .. doctest:: |
| 2507 | |
| 2508 | >>> from binarytree import heap |
| 2509 | >>> |
| 2510 | >>> root = heap(4, is_max=False) |
| 2511 | >>> |
| 2512 | >>> root.height |
| 2513 | 4 |
| 2514 | >>> root.is_min_heap |
| 2515 | True |
| 2516 | |
| 2517 | .. doctest:: |
| 2518 | |
| 2519 | >>> from binarytree import heap |
| 2520 | >>> |
| 2521 | >>> root = heap(5, is_max=False, is_perfect=True) |
| 2522 | >>> |
| 2523 | >>> root.height |
| 2524 | 5 |
| 2525 | >>> root.is_min_heap |