diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | python-binarytree.spec | 1211 | ||||
| -rw-r--r-- | sources | 1 |
3 files changed, 1213 insertions, 0 deletions
@@ -0,0 +1 @@ +/binarytree-6.5.1.tar.gz diff --git a/python-binarytree.spec b/python-binarytree.spec new file mode 100644 index 0000000..d20e9a2 --- /dev/null +++ b/python-binarytree.spec @@ -0,0 +1,1211 @@ +%global _empty_manifest_terminate_build 0 +Name: python-binarytree +Version: 6.5.1 +Release: 1 +Summary: Python Library for Studying Binary Trees +License: MIT +URL: https://github.com/joowani/binarytree +Source0: https://mirrors.nju.edu.cn/pypi/web/packages/cd/c8/3928e18bccba6c5625547273073ff722458405e1d1bc9ceb25ba5ec12325/binarytree-6.5.1.tar.gz +BuildArch: noarch + +Requires: python3-graphviz +Requires: python3-setuptools +Requires: python3-setuptools-scm[toml] +Requires: python3-black +Requires: python3-flake8 +Requires: python3-isort +Requires: python3-mypy +Requires: python3-pre-commit +Requires: python3-pytest +Requires: python3-pytest-cov +Requires: python3-sphinx +Requires: python3-sphinx-rtd-theme +Requires: python3-types-setuptools +Requires: python3-types-dataclasses + +%description +# Binarytree: Python Library for Studying Binary Trees + + + +[](https://codecov.io/gh/joowani/binarytree) +[](https://badge.fury.io/py/binarytree) +[](https://github.com/joowani/binarytree/blob/main/LICENSE) + + +Are you studying binary trees for your next exam, assignment or technical interview? + +**Binarytree** is a Python library which lets you generate, visualize, inspect and +manipulate [binary trees](https://en.wikipedia.org/wiki/Binary_tree). Skip the tedious +work of setting up test data, and dive straight into practising your algorithms. +[Heaps](https://en.wikipedia.org/wiki/Heap_(data_structure)) and +[binary search trees](https://en.wikipedia.org/wiki/Binary_search_tree) are also supported. +Self-balancing search trees like [red-black](https://en.wikipedia.org/wiki/Red%E2%80%93black_tree) +or [AVL](https://en.wikipedia.org/wiki/AVL_tree) will be added in the future. + +Check out the [documentation](http://binarytree.readthedocs.io) for more details. + + + +Binarytree can be used with [Graphviz](https://graphviz.org) and +[Jupyter Notebooks](https://jupyter.org) as well: + + + +## Requirements + +Python 3.7+ + +## Installation + +Install via [pip](https://pip.pypa.io): + +```shell +pip install binarytree --upgrade +``` + +For [conda](https://docs.conda.io) users: + +```shell +conda install binarytree -c conda-forge +``` + +## Getting Started + +Binarytree uses the following class to represent a node: + +```python +class Node: + + def __init__(self, value, left=None, right=None): + self.value = value # The node value (float/int/str) + self.left = left # Left child + self.right = right # Right child +``` + +Generate and pretty-print various types of binary trees: + +```python +from binarytree import tree, bst, heap + +# Generate a random binary tree and return its root node. +my_tree = tree(height=3, is_perfect=False) + +# Generate a random BST and return its root node. +my_bst = bst(height=3, is_perfect=True) + +# Generate a random max heap and return its root node. +my_heap = heap(height=3, is_max=True, is_perfect=False) + +# Pretty-print the trees in stdout. +print(my_tree) +# +# _______1_____ +# / \ +# 4__ ___3 +# / \ / \ +# 0 9 13 14 +# / \ \ +# 7 10 2 +# +print(my_bst) +# +# ______7_______ +# / \ +# __3__ ___11___ +# / \ / \ +# 1 5 9 _13 +# / \ / \ / \ / \ +# 0 2 4 6 8 10 12 14 +# +print(my_heap) +# +# _____14__ +# / \ +# ____13__ 9 +# / \ / \ +# 12 7 3 8 +# / \ / +# 0 10 6 +# +``` +Generate trees with letter values instead of numbers: + +```python +from binarytree import tree + +my_tree = tree(height=3, is_perfect=False, letters=True) + +print(my_tree) +# +# ____H____ +# / \ +# __E__ F__ +# / \ / \ +# M G J B +# \ / / / \ +# O L D I A +# +``` + + +Build your own trees: + +```python +from binarytree import Node + +root = Node(1) +root.left = Node(2) +root.right = Node(3) +root.left.right = Node(4) + +print(root) +# +# __1 +# / \ +# 2 3 +# \ +# 4 +# +``` + +Inspect tree properties: + +```python +from binarytree import Node + +root = Node(1) +root.left = Node(2) +root.right = Node(3) +root.left.left = Node(4) +root.left.right = Node(5) + +print(root) +# +# __1 +# / \ +# 2 3 +# / \ +# 4 5 +# +assert root.height == 2 +assert root.is_balanced is True +assert root.is_bst is False +assert root.is_complete is True +assert root.is_max_heap is False +assert root.is_min_heap is True +assert root.is_perfect is False +assert root.is_strict is True +assert root.leaf_count == 3 +assert root.max_leaf_depth == 2 +assert root.max_node_value == 5 +assert root.min_leaf_depth == 1 +assert root.min_node_value == 1 +assert root.size == 5 + +# See all properties at once. +assert root.properties == { + 'height': 2, + 'is_balanced': True, + 'is_bst': False, + 'is_complete': True, + 'is_max_heap': False, + 'is_min_heap': True, + 'is_perfect': False, + 'is_strict': True, + 'leaf_count': 3, + 'max_leaf_depth': 2, + 'max_node_value': 5, + 'min_leaf_depth': 1, + 'min_node_value': 1, + 'size': 5 +} + +print(root.leaves) +# [Node(3), Node(4), Node(5)] + +print(root.levels) +# [[Node(1)], [Node(2), Node(3)], [Node(4), Node(5)]] +``` + +Compare and clone trees: +```python +from binarytree import tree + +original = tree() + +# Clone the binary tree. +clone = original.clone() + +# Check if the trees are equal. +original.equals(clone) +``` + + +Use [level-order (breadth-first)](https://en.wikipedia.org/wiki/Tree_traversal#Breadth-first_search) +indexes to manipulate nodes: + +```python +from binarytree import Node + +root = Node(1) # index: 0, value: 1 +root.left = Node(2) # index: 1, value: 2 +root.right = Node(3) # index: 2, value: 3 +root.left.right = Node(4) # index: 4, value: 4 +root.left.right.left = Node(5) # index: 9, value: 5 + +print(root) +# +# ____1 +# / \ +# 2__ 3 +# \ +# 4 +# / +# 5 +# +root.pprint(index=True) +# +# _________0-1_ +# / \ +# 1-2_____ 2-3 +# \ +# _4-4 +# / +# 9-5 +# +print(root[9]) +# Node(5) + +# Replace the node/subtree at index 4. +root[4] = Node(6, left=Node(7), right=Node(8)) +root.pprint(index=True) +# +# ______________0-1_ +# / \ +# 1-2_____ 2-3 +# \ +# _4-6_ +# / \ +# 9-7 10-8 +# + +# Delete the node/subtree at index 1. +del root[1] +root.pprint(index=True) +# +# 0-1_ +# \ +# 2-3 +``` + +Traverse trees using different algorithms: + +```python +from binarytree import Node + +root = Node(1) +root.left = Node(2) +root.right = Node(3) +root.left.left = Node(4) +root.left.right = Node(5) + +print(root) +# +# __1 +# / \ +# 2 3 +# / \ +# 4 5 +# +print(root.inorder) +# [Node(4), Node(2), Node(5), Node(1), Node(3)] + +print(root.preorder) +# [Node(1), Node(2), Node(4), Node(5), Node(3)] + +print(root.postorder) +# [Node(4), Node(5), Node(2), Node(3), Node(1)] + +print(root.levelorder) +# [Node(1), Node(2), Node(3), Node(4), Node(5)] + +print(list(root)) # Equivalent to root.levelorder +# [Node(1), Node(2), Node(3), Node(4), Node(5)] +``` + +Convert to [list representations](https://en.wikipedia.org/wiki/Binary_tree#Arrays): + +```python +from binarytree import build + +# Build a tree from list representation +values = [7, 3, 2, 6, 9, None, 1, 5, 8] +root = build(values) +print(root) +# +# __7 +# / \ +# __3 2 +# / \ \ +# 6 9 1 +# / \ +# 5 8 +# + +# Go back to list representation +print(root.values) +# [7, 3, 2, 6, 9, None, 1, 5, 8] +``` + +Binarytree supports another representation which is more compact but without +the [indexing properties](https://en.wikipedia.org/wiki/Binary_tree#Arrays) +(this method is often used in [Leetcode](https://leetcode.com/)): + +```python +from binarytree import build, build2, Node + +# First let's create an example tree. +root = Node(1) +root.left = Node(2) +root.left.left = Node(3) +root.left.left.left = Node(4) +root.left.left.right = Node(5) +print(root) +# +# 1 +# / +# __2 +# / +# 3 +# / \ +# 4 5 + +# First representation is already shown above. +# All "null" nodes in each level are present. +print(root.values) +# [1, 2, None, 3, None, None, None, 4, 5] + +# Second representation is more compact but without the indexing properties. +print(root.values2) +# [1, 2, None, 3, None, 4, 5] + +# Build trees from the list representations +tree1 = build(root.values) +tree2 = build2(root.values2) +assert tree1.equals(tree2) is True +``` + +Check out the [documentation](http://binarytree.readthedocs.io) for more details. + + + + +%package -n python3-binarytree +Summary: Python Library for Studying Binary Trees +Provides: python-binarytree +BuildRequires: python3-devel +BuildRequires: python3-setuptools +BuildRequires: python3-pip +%description -n python3-binarytree +# Binarytree: Python Library for Studying Binary Trees + + + +[](https://codecov.io/gh/joowani/binarytree) +[](https://badge.fury.io/py/binarytree) +[](https://github.com/joowani/binarytree/blob/main/LICENSE) + + +Are you studying binary trees for your next exam, assignment or technical interview? + +**Binarytree** is a Python library which lets you generate, visualize, inspect and +manipulate [binary trees](https://en.wikipedia.org/wiki/Binary_tree). Skip the tedious +work of setting up test data, and dive straight into practising your algorithms. +[Heaps](https://en.wikipedia.org/wiki/Heap_(data_structure)) and +[binary search trees](https://en.wikipedia.org/wiki/Binary_search_tree) are also supported. +Self-balancing search trees like [red-black](https://en.wikipedia.org/wiki/Red%E2%80%93black_tree) +or [AVL](https://en.wikipedia.org/wiki/AVL_tree) will be added in the future. + +Check out the [documentation](http://binarytree.readthedocs.io) for more details. + + + +Binarytree can be used with [Graphviz](https://graphviz.org) and +[Jupyter Notebooks](https://jupyter.org) as well: + + + +## Requirements + +Python 3.7+ + +## Installation + +Install via [pip](https://pip.pypa.io): + +```shell +pip install binarytree --upgrade +``` + +For [conda](https://docs.conda.io) users: + +```shell +conda install binarytree -c conda-forge +``` + +## Getting Started + +Binarytree uses the following class to represent a node: + +```python +class Node: + + def __init__(self, value, left=None, right=None): + self.value = value # The node value (float/int/str) + self.left = left # Left child + self.right = right # Right child +``` + +Generate and pretty-print various types of binary trees: + +```python +from binarytree import tree, bst, heap + +# Generate a random binary tree and return its root node. +my_tree = tree(height=3, is_perfect=False) + +# Generate a random BST and return its root node. +my_bst = bst(height=3, is_perfect=True) + +# Generate a random max heap and return its root node. +my_heap = heap(height=3, is_max=True, is_perfect=False) + +# Pretty-print the trees in stdout. +print(my_tree) +# +# _______1_____ +# / \ +# 4__ ___3 +# / \ / \ +# 0 9 13 14 +# / \ \ +# 7 10 2 +# +print(my_bst) +# +# ______7_______ +# / \ +# __3__ ___11___ +# / \ / \ +# 1 5 9 _13 +# / \ / \ / \ / \ +# 0 2 4 6 8 10 12 14 +# +print(my_heap) +# +# _____14__ +# / \ +# ____13__ 9 +# / \ / \ +# 12 7 3 8 +# / \ / +# 0 10 6 +# +``` +Generate trees with letter values instead of numbers: + +```python +from binarytree import tree + +my_tree = tree(height=3, is_perfect=False, letters=True) + +print(my_tree) +# +# ____H____ +# / \ +# __E__ F__ +# / \ / \ +# M G J B +# \ / / / \ +# O L D I A +# +``` + + +Build your own trees: + +```python +from binarytree import Node + +root = Node(1) +root.left = Node(2) +root.right = Node(3) +root.left.right = Node(4) + +print(root) +# +# __1 +# / \ +# 2 3 +# \ +# 4 +# +``` + +Inspect tree properties: + +```python +from binarytree import Node + +root = Node(1) +root.left = Node(2) +root.right = Node(3) +root.left.left = Node(4) +root.left.right = Node(5) + +print(root) +# +# __1 +# / \ +# 2 3 +# / \ +# 4 5 +# +assert root.height == 2 +assert root.is_balanced is True +assert root.is_bst is False +assert root.is_complete is True +assert root.is_max_heap is False +assert root.is_min_heap is True +assert root.is_perfect is False +assert root.is_strict is True +assert root.leaf_count == 3 +assert root.max_leaf_depth == 2 +assert root.max_node_value == 5 +assert root.min_leaf_depth == 1 +assert root.min_node_value == 1 +assert root.size == 5 + +# See all properties at once. +assert root.properties == { + 'height': 2, + 'is_balanced': True, + 'is_bst': False, + 'is_complete': True, + 'is_max_heap': False, + 'is_min_heap': True, + 'is_perfect': False, + 'is_strict': True, + 'leaf_count': 3, + 'max_leaf_depth': 2, + 'max_node_value': 5, + 'min_leaf_depth': 1, + 'min_node_value': 1, + 'size': 5 +} + +print(root.leaves) +# [Node(3), Node(4), Node(5)] + +print(root.levels) +# [[Node(1)], [Node(2), Node(3)], [Node(4), Node(5)]] +``` + +Compare and clone trees: +```python +from binarytree import tree + +original = tree() + +# Clone the binary tree. +clone = original.clone() + +# Check if the trees are equal. +original.equals(clone) +``` + + +Use [level-order (breadth-first)](https://en.wikipedia.org/wiki/Tree_traversal#Breadth-first_search) +indexes to manipulate nodes: + +```python +from binarytree import Node + +root = Node(1) # index: 0, value: 1 +root.left = Node(2) # index: 1, value: 2 +root.right = Node(3) # index: 2, value: 3 +root.left.right = Node(4) # index: 4, value: 4 +root.left.right.left = Node(5) # index: 9, value: 5 + +print(root) +# +# ____1 +# / \ +# 2__ 3 +# \ +# 4 +# / +# 5 +# +root.pprint(index=True) +# +# _________0-1_ +# / \ +# 1-2_____ 2-3 +# \ +# _4-4 +# / +# 9-5 +# +print(root[9]) +# Node(5) + +# Replace the node/subtree at index 4. +root[4] = Node(6, left=Node(7), right=Node(8)) +root.pprint(index=True) +# +# ______________0-1_ +# / \ +# 1-2_____ 2-3 +# \ +# _4-6_ +# / \ +# 9-7 10-8 +# + +# Delete the node/subtree at index 1. +del root[1] +root.pprint(index=True) +# +# 0-1_ +# \ +# 2-3 +``` + +Traverse trees using different algorithms: + +```python +from binarytree import Node + +root = Node(1) +root.left = Node(2) +root.right = Node(3) +root.left.left = Node(4) +root.left.right = Node(5) + +print(root) +# +# __1 +# / \ +# 2 3 +# / \ +# 4 5 +# +print(root.inorder) +# [Node(4), Node(2), Node(5), Node(1), Node(3)] + +print(root.preorder) +# [Node(1), Node(2), Node(4), Node(5), Node(3)] + +print(root.postorder) +# [Node(4), Node(5), Node(2), Node(3), Node(1)] + +print(root.levelorder) +# [Node(1), Node(2), Node(3), Node(4), Node(5)] + +print(list(root)) # Equivalent to root.levelorder +# [Node(1), Node(2), Node(3), Node(4), Node(5)] +``` + +Convert to [list representations](https://en.wikipedia.org/wiki/Binary_tree#Arrays): + +```python +from binarytree import build + +# Build a tree from list representation +values = [7, 3, 2, 6, 9, None, 1, 5, 8] +root = build(values) +print(root) +# +# __7 +# / \ +# __3 2 +# / \ \ +# 6 9 1 +# / \ +# 5 8 +# + +# Go back to list representation +print(root.values) +# [7, 3, 2, 6, 9, None, 1, 5, 8] +``` + +Binarytree supports another representation which is more compact but without +the [indexing properties](https://en.wikipedia.org/wiki/Binary_tree#Arrays) +(this method is often used in [Leetcode](https://leetcode.com/)): + +```python +from binarytree import build, build2, Node + +# First let's create an example tree. +root = Node(1) +root.left = Node(2) +root.left.left = Node(3) +root.left.left.left = Node(4) +root.left.left.right = Node(5) +print(root) +# +# 1 +# / +# __2 +# / +# 3 +# / \ +# 4 5 + +# First representation is already shown above. +# All "null" nodes in each level are present. +print(root.values) +# [1, 2, None, 3, None, None, None, 4, 5] + +# Second representation is more compact but without the indexing properties. +print(root.values2) +# [1, 2, None, 3, None, 4, 5] + +# Build trees from the list representations +tree1 = build(root.values) +tree2 = build2(root.values2) +assert tree1.equals(tree2) is True +``` + +Check out the [documentation](http://binarytree.readthedocs.io) for more details. + + + + +%package help +Summary: Development documents and examples for binarytree +Provides: python3-binarytree-doc +%description help +# Binarytree: Python Library for Studying Binary Trees + + + +[](https://codecov.io/gh/joowani/binarytree) +[](https://badge.fury.io/py/binarytree) +[](https://github.com/joowani/binarytree/blob/main/LICENSE) + + +Are you studying binary trees for your next exam, assignment or technical interview? + +**Binarytree** is a Python library which lets you generate, visualize, inspect and +manipulate [binary trees](https://en.wikipedia.org/wiki/Binary_tree). Skip the tedious +work of setting up test data, and dive straight into practising your algorithms. +[Heaps](https://en.wikipedia.org/wiki/Heap_(data_structure)) and +[binary search trees](https://en.wikipedia.org/wiki/Binary_search_tree) are also supported. +Self-balancing search trees like [red-black](https://en.wikipedia.org/wiki/Red%E2%80%93black_tree) +or [AVL](https://en.wikipedia.org/wiki/AVL_tree) will be added in the future. + +Check out the [documentation](http://binarytree.readthedocs.io) for more details. + + + +Binarytree can be used with [Graphviz](https://graphviz.org) and +[Jupyter Notebooks](https://jupyter.org) as well: + + + +## Requirements + +Python 3.7+ + +## Installation + +Install via [pip](https://pip.pypa.io): + +```shell +pip install binarytree --upgrade +``` + +For [conda](https://docs.conda.io) users: + +```shell +conda install binarytree -c conda-forge +``` + +## Getting Started + +Binarytree uses the following class to represent a node: + +```python +class Node: + + def __init__(self, value, left=None, right=None): + self.value = value # The node value (float/int/str) + self.left = left # Left child + self.right = right # Right child +``` + +Generate and pretty-print various types of binary trees: + +```python +from binarytree import tree, bst, heap + +# Generate a random binary tree and return its root node. +my_tree = tree(height=3, is_perfect=False) + +# Generate a random BST and return its root node. +my_bst = bst(height=3, is_perfect=True) + +# Generate a random max heap and return its root node. +my_heap = heap(height=3, is_max=True, is_perfect=False) + +# Pretty-print the trees in stdout. +print(my_tree) +# +# _______1_____ +# / \ +# 4__ ___3 +# / \ / \ +# 0 9 13 14 +# / \ \ +# 7 10 2 +# +print(my_bst) +# +# ______7_______ +# / \ +# __3__ ___11___ +# / \ / \ +# 1 5 9 _13 +# / \ / \ / \ / \ +# 0 2 4 6 8 10 12 14 +# +print(my_heap) +# +# _____14__ +# / \ +# ____13__ 9 +# / \ / \ +# 12 7 3 8 +# / \ / +# 0 10 6 +# +``` +Generate trees with letter values instead of numbers: + +```python +from binarytree import tree + +my_tree = tree(height=3, is_perfect=False, letters=True) + +print(my_tree) +# +# ____H____ +# / \ +# __E__ F__ +# / \ / \ +# M G J B +# \ / / / \ +# O L D I A +# +``` + + +Build your own trees: + +```python +from binarytree import Node + +root = Node(1) +root.left = Node(2) +root.right = Node(3) +root.left.right = Node(4) + +print(root) +# +# __1 +# / \ +# 2 3 +# \ +# 4 +# +``` + +Inspect tree properties: + +```python +from binarytree import Node + +root = Node(1) +root.left = Node(2) +root.right = Node(3) +root.left.left = Node(4) +root.left.right = Node(5) + +print(root) +# +# __1 +# / \ +# 2 3 +# / \ +# 4 5 +# +assert root.height == 2 +assert root.is_balanced is True +assert root.is_bst is False +assert root.is_complete is True +assert root.is_max_heap is False +assert root.is_min_heap is True +assert root.is_perfect is False +assert root.is_strict is True +assert root.leaf_count == 3 +assert root.max_leaf_depth == 2 +assert root.max_node_value == 5 +assert root.min_leaf_depth == 1 +assert root.min_node_value == 1 +assert root.size == 5 + +# See all properties at once. +assert root.properties == { + 'height': 2, + 'is_balanced': True, + 'is_bst': False, + 'is_complete': True, + 'is_max_heap': False, + 'is_min_heap': True, + 'is_perfect': False, + 'is_strict': True, + 'leaf_count': 3, + 'max_leaf_depth': 2, + 'max_node_value': 5, + 'min_leaf_depth': 1, + 'min_node_value': 1, + 'size': 5 +} + +print(root.leaves) +# [Node(3), Node(4), Node(5)] + +print(root.levels) +# [[Node(1)], [Node(2), Node(3)], [Node(4), Node(5)]] +``` + +Compare and clone trees: +```python +from binarytree import tree + +original = tree() + +# Clone the binary tree. +clone = original.clone() + +# Check if the trees are equal. +original.equals(clone) +``` + + +Use [level-order (breadth-first)](https://en.wikipedia.org/wiki/Tree_traversal#Breadth-first_search) +indexes to manipulate nodes: + +```python +from binarytree import Node + +root = Node(1) # index: 0, value: 1 +root.left = Node(2) # index: 1, value: 2 +root.right = Node(3) # index: 2, value: 3 +root.left.right = Node(4) # index: 4, value: 4 +root.left.right.left = Node(5) # index: 9, value: 5 + +print(root) +# +# ____1 +# / \ +# 2__ 3 +# \ +# 4 +# / +# 5 +# +root.pprint(index=True) +# +# _________0-1_ +# / \ +# 1-2_____ 2-3 +# \ +# _4-4 +# / +# 9-5 +# +print(root[9]) +# Node(5) + +# Replace the node/subtree at index 4. +root[4] = Node(6, left=Node(7), right=Node(8)) +root.pprint(index=True) +# +# ______________0-1_ +# / \ +# 1-2_____ 2-3 +# \ +# _4-6_ +# / \ +# 9-7 10-8 +# + +# Delete the node/subtree at index 1. +del root[1] +root.pprint(index=True) +# +# 0-1_ +# \ +# 2-3 +``` + +Traverse trees using different algorithms: + +```python +from binarytree import Node + +root = Node(1) +root.left = Node(2) +root.right = Node(3) +root.left.left = Node(4) +root.left.right = Node(5) + +print(root) +# +# __1 +# / \ +# 2 3 +# / \ +# 4 5 +# +print(root.inorder) +# [Node(4), Node(2), Node(5), Node(1), Node(3)] + +print(root.preorder) +# [Node(1), Node(2), Node(4), Node(5), Node(3)] + +print(root.postorder) +# [Node(4), Node(5), Node(2), Node(3), Node(1)] + +print(root.levelorder) +# [Node(1), Node(2), Node(3), Node(4), Node(5)] + +print(list(root)) # Equivalent to root.levelorder +# [Node(1), Node(2), Node(3), Node(4), Node(5)] +``` + +Convert to [list representations](https://en.wikipedia.org/wiki/Binary_tree#Arrays): + +```python +from binarytree import build + +# Build a tree from list representation +values = [7, 3, 2, 6, 9, None, 1, 5, 8] +root = build(values) +print(root) +# +# __7 +# / \ +# __3 2 +# / \ \ +# 6 9 1 +# / \ +# 5 8 +# + +# Go back to list representation +print(root.values) +# [7, 3, 2, 6, 9, None, 1, 5, 8] +``` + +Binarytree supports another representation which is more compact but without +the [indexing properties](https://en.wikipedia.org/wiki/Binary_tree#Arrays) +(this method is often used in [Leetcode](https://leetcode.com/)): + +```python +from binarytree import build, build2, Node + +# First let's create an example tree. +root = Node(1) +root.left = Node(2) +root.left.left = Node(3) +root.left.left.left = Node(4) +root.left.left.right = Node(5) +print(root) +# +# 1 +# / +# __2 +# / +# 3 +# / \ +# 4 5 + +# First representation is already shown above. +# All "null" nodes in each level are present. +print(root.values) +# [1, 2, None, 3, None, None, None, 4, 5] + +# Second representation is more compact but without the indexing properties. +print(root.values2) +# [1, 2, None, 3, None, 4, 5] + +# Build trees from the list representations +tree1 = build(root.values) +tree2 = build2(root.values2) +assert tree1.equals(tree2) is True +``` + +Check out the [documentation](http://binarytree.readthedocs.io) for more details. + + + + +%prep +%autosetup -n binarytree-6.5.1 + +%build +%py3_build + +%install +%py3_install +install -d -m755 %{buildroot}/%{_pkgdocdir} +if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi +if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi +if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi +if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi +pushd %{buildroot} +if [ -d usr/lib ]; then + find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst +fi +if [ -d usr/lib64 ]; then + find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst +fi +if [ -d usr/bin ]; then + find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst +fi +if [ -d usr/sbin ]; then + find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst +fi +touch doclist.lst +if [ -d usr/share/man ]; then + find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst +fi +popd +mv %{buildroot}/filelist.lst . +mv %{buildroot}/doclist.lst . + +%files -n python3-binarytree -f filelist.lst +%dir %{python3_sitelib}/* + +%files help -f doclist.lst +%{_docdir}/* + +%changelog +* Fri May 05 2023 Python_Bot <Python_Bot@openeuler.org> - 6.5.1-1 +- Package Spec generated @@ -0,0 +1 @@ +bc2e647a29a127b1fa83ef24c86a632b binarytree-6.5.1.tar.gz |
