Hướng dẫn python binary tree library - thư viện cây nhị phân python

Hướng dẫn python binary tree library - thư viện cây nhị phân python

Show

Chào mừng bạn đến với tài liệu cho BinaryTree.binarytree.

BinaryTree là Thư viện Python cho phép bạn tạo, trực quan hóa, kiểm tra và điều khiển cây nhị phân. Bỏ qua công việc tẻ nhạt của việc thiết lập dữ liệu thử nghiệm và đi thẳng vào các thuật toán thực hành. Hàng đống và cây tìm kiếm nhị phân cũng được hỗ trợ. Những cây tìm kiếm tự cân bằng như Red-Black hoặc AVL sẽ được thêm vào trong tương lai. is Python library which lets you generate, visualize, inspect and manipulate binary trees. Skip the tedious work of setting up test data, and dive straight into practising algorithms. Heaps and binary search trees are also supported. Self-balancing search trees like red-black or AVL will be added in the future.

Yêu cầu công

Python 3.7+

Cài đặt¶

Cài đặt qua PIP:

pip install binarytree --upgrade

Dành cho người dùng Conda:

conda install binarytree -c conda-forge

Một cây nhị phân là một cấu trúc dữ liệu trong đó mọi nút hoặc đỉnh có nhiều nhất là hai đứa trẻ. Trong Python, một cây nhị phân có thể được biểu diễn theo các cách khác nhau với các cấu trúc dữ liệu khác nhau (từ điển, danh sách) và biểu diễn lớp cho một nút. Tuy nhiên, thư viện BinaryTree giúp thực hiện trực tiếp một cây nhị phân. Nó cũng hỗ trợ HEAP và Cây tìm kiếm nhị phân (BST). Mô-đun này không được cài đặt sẵn với mô-đun tiện ích tiêu chuẩn Python. Để cài đặt nó nhập lệnh dưới đây trong thiết bị đầu cuối. & NBSP;
 

pip install binarytree 

Tạo nút

Lớp nút đại diện cho cấu trúc của một nút cụ thể trong cây nhị phân. Các thuộc tính của lớp này là các giá trị, trái, phải. & Nbsp;
 

Cú pháp: BinaryTree.Node (giá trị, trái = Không, phải = Không) Tham số: & NBSP; Giá trị: Chứa dữ liệu cho một nút. Giá trị này phải là số. & Nbsp; trái: Chứa các chi tiết của nút bên trái. binarytree.Node(value, left=None, right=None)
Parameters: 
value: Contains the data for a node. This value must be number. 
left: Contains the details of left node child. 
right: Contains details of the right node child. 
 

Lưu ý: Nếu nút con trái hoặc phải không phải là một thể hiện của lớp binytree.node thì BinaryTree.Exception.NodetypeError được nâng lên và nếu giá trị nút không phải là số thì BinaryTree.Exceptions.NodeValueError được nâng lên.
Example:

Python3

from binarytree import

conda install binarytree -c conda-forge
0

conda install binarytree -c conda-forge
1
conda install binarytree -c conda-forge
2
conda install binarytree -c conda-forge
3
conda install binarytree -c conda-forge
4
conda install binarytree -c conda-forge
5

conda install binarytree -c conda-forge
6
conda install binarytree -c conda-forge
2
conda install binarytree -c conda-forge
3
conda install binarytree -c conda-forge
9
conda install binarytree -c conda-forge
5

pip install binarytree 
1
conda install binarytree -c conda-forge
2
conda install binarytree -c conda-forge
3
pip install binarytree 
4
conda install binarytree -c conda-forge
5

pip install binarytree 
6
pip install binarytree 
7
pip install binarytree 
8
pip install binarytree 
9

pip install binarytree 
6
pip install binarytree 
7
Binary tree from list :
 
    ___3
   /    \
  6      8
 / \      \
2   11     13


List from binary tree : [3, 6, 8, 2, 11, None, 13]
2
Binary tree from list :
 
    ___3
   /    \
  6      8
 / \      \
2   11     13


List from binary tree : [3, 6, 8, 2, 11, None, 13]
3
Binary tree from list :
 
    ___3
   /    \
  6      8
 / \      \
2   11     13


List from binary tree : [3, 6, 8, 2, 11, None, 13]
4
Binary tree from list :
 
    ___3
   /    \
  6      8
 / \      \
2   11     13


List from binary tree : [3, 6, 8, 2, 11, None, 13]
5

pip install binarytree 
6
pip install binarytree 
7
Binary tree from list :
 
    ___3
   /    \
  6      8
 / \      \
2   11     13


List from binary tree : [3, 6, 8, 2, 11, None, 13]
8
Binary tree from list :
 
    ___3
   /    \
  6      8
 / \      \
2   11     13


List from binary tree : [3, 6, 8, 2, 11, None, 13]
9

pip install binarytree 
6
pip install binarytree 
7
Binary tree of any height :

      14____
     /      \
    2        5__
   /        /   \
  6        1     13
 /        /     /  \
7        9     4    8

Binary tree of given height :

  1__
 /   \
5     2
     / \
    4   3

Perfect binary tree of given height :

    __3__
   /     \
  2       4
 / \     / \
6   0   1   5
2
Binary tree of any height :

      14____
     /      \
    2        5__
   /        /   \
  6        1     13
 /        /     /  \
7        9     4    8

Binary tree of given height :

  1__
 /   \
5     2
     / \
    4   3

Perfect binary tree of given height :

    __3__
   /     \
  2       4
 / \     / \
6   0   1   5
3

pip install binarytree 
6
pip install binarytree 
7
Binary tree of any height :

      14____
     /      \
    2        5__
   /        /   \
  6        1     13
 /        /     /  \
7        9     4    8

Binary tree of given height :

  1__
 /   \
5     2
     / \
    4   3

Perfect binary tree of given height :

    __3__
   /     \
  2       4
 / \     / \
6   0   1   5
6
Binary tree of any height :

      14____
     /      \
    2        5__
   /        /   \
  6        1     13
 /        /     /  \
7        9     4    8

Binary tree of given height :

  1__
 /   \
5     2
     / \
    4   3

Perfect binary tree of given height :

    __3__
   /     \
  2       4
 / \     / \
6   0   1   5
7

pip install binarytree 
6
pip install binarytree 
7
BST of any height : 
 
        ____9______
       /           \
    __5__       ____12___
   /     \     /         \
  2       8   10         _14
 / \     /      \       /
1   4   7        11    13

BST of given height : 
 
    5
   / \
  4   6
 /
3

Perfect BST of given height : 
 
    __3__
   /     \
  1       5
 / \     / \
0   2   4   6
0
BST of any height : 
 
        ____9______
       /           \
    __5__       ____12___
   /     \     /         \
  2       8   10         _14
 / \     /      \       /
1   4   7        11    13

BST of given height : 
 
    5
   / \
  4   6
 /
3

Perfect BST of given height : 
 
    __3__
   /     \
  1       5
 / \     / \
0   2   4   6
1

Output:

Cây nhị phân: & nbsp; 3 & nbsp;/ \ & nbsp; 6 8list của các nút: [Node (3), Node (6), Node (8)] Inorder của các nút: [Node (6), Node (3), Node (8 . : True, 'is_complete': true, 'leaf_count': 2, 'min_node_value': 3, 'max_node_value': 8, 'min_leaf_depth': 1, 'max_leaf_depth': 1, 'is_bst' , 'is_symmetric': false}

/ \ 
6 8
List of nodes : [Node(3), Node(6), Node(8)]
Inorder of nodes : [Node(6), Node(3), Node(8)]
Size of tree : 3
Height of tree : 1
Properties of tree : 
{‘height’: 1, ‘size’: 3, ‘is_max_heap’: False, ‘is_min_heap’: True, ‘is_perfect’: True, ‘is_strict’: True, ‘is_complete’: True, ‘leaf_count’: 2, ‘min_node_value’: 3, ‘max_node_value’: 8, ‘min_leaf_depth’: 1, ‘max_leaf_depth’: 1, ‘is_bst’: False, ‘is_balanced’: True, ‘is_symmetric’: False}

Xây dựng một cây nhị phân từ danh sách:

Thay vì sử dụng phương thức nút nhiều lần, chúng ta có thể sử dụng phương thức build () để chuyển đổi danh sách các giá trị thành cây nhị phân. Chỉ mục 2*i+1, đứa trẻ bên phải tại INDEX 2*I+2 và cha mẹ tại (i - 1) // 2. Các yếu tố tại Index J cho j> len (danh sách) // 2 là các nút lá. Không cho biết sự vắng mặt của một nút tại chỉ mục đó. Chúng ta cũng có thể lấy lại danh sách các nút sau khi xây dựng một cây nhị phân bằng thuộc tính giá trị. & NBSP;
Here, a given list contains the nodes of tree such that the element at index i has its left child at index 2*i+1, the right child at index 2*i+2 and parent at (i – 1)//2. The elements at index j for j>len(list)//2 are leaf nodes. None indicates the absence of a node at that index. We can also get the list of nodes back after building a binary tree using values attribute.
 

Cú pháp: BinaryTree.Build (Giá trị) Tham số: & nbsp; Giá trị: Danh sách biểu diễn của cây nhị phân. Returns: gốc của cây nhị phân. & Nbsp; & nbsp;
Parameters: 
values: List representation of the binary tree.
Returns: root of the binary tree. 
 

Example: 

Python3

from binarytree import

BST of any height : 
 
        ____9______
       /           \
    __5__       ____12___
   /     \     /         \
  2       8   10         _14
 / \     /      \       /
1   4   7        11    13

BST of given height : 
 
    5
   / \
  4   6
 /
3

Perfect BST of given height : 
 
    __3__
   /     \
  1       5
 / \     / \
0   2   4   6
5

BST of any height : 
 
        ____9______
       /           \
    __5__       ____12___
   /     \     /         \
  2       8   10         _14
 / \     /      \       /
1   4   7        11    13

BST of given height : 
 
    5
   / \
  4   6
 /
3

Perfect BST of given height : 
 
    __3__
   /     \
  1       5
 / \     / \
0   2   4   6
6
conda install binarytree -c conda-forge
2
BST of any height : 
 
        ____9______
       /           \
    __5__       ____12___
   /     \     /         \
  2       8   10         _14
 / \     /      \       /
1   4   7        11    13

BST of given height : 
 
    5
   / \
  4   6
 /
3

Perfect BST of given height : 
 
    __3__
   /     \
  1       5
 / \     / \
0   2   4   6
8
conda install binarytree -c conda-forge
4
Binary tree from list :
 
    ___3
   /    \
  6      8
 / \      \
2   11     13


List from binary tree : [3, 6, 8, 2, 11, None, 13]
3
conda install binarytree -c conda-forge
9
Binary tree from list :
 
    ___3
   /    \
  6      8
 / \      \
2   11     13


List from binary tree : [3, 6, 8, 2, 11, None, 13]
3
pip install binarytree 
4
Binary tree from list :
 
    ___3
   /    \
  6      8
 / \      \
2   11     13


List from binary tree : [3, 6, 8, 2, 11, None, 13]
3
Max-heap of any height : 
 
         _______14______
        /               \
    ___12__            __13__
   /       \          /      \
  10        8        3        9
 /  \      / \      / \      /
1    5    4   6    0   2    7

Max-heap of given height : 
 
    __6__
   /     \
  4       5
 / \     / \
2   0   1   3

Perfect min-heap of given height : 
 
    __0__
   /     \
  1       3
 / \     / \
2   6   4   5
5
Binary tree from list :
 
    ___3
   /    \
  6      8
 / \      \
2   11     13


List from binary tree : [3, 6, 8, 2, 11, None, 13]
3
Max-heap of any height : 
 
         _______14______
        /               \
    ___12__            __13__
   /       \          /      \
  10        8        3        9
 /  \      / \      / \      /
1    5    4   6    0   2    7

Max-heap of given height : 
 
    __6__
   /     \
  4       5
 / \     / \
2   0   1   3

Perfect min-heap of given height : 
 
    __0__
   /     \
  1       3
 / \     / \
2   6   4   5
7
Binary tree from list :
 
    ___3
   /    \
  6      8
 / \      \
2   11     13


List from binary tree : [3, 6, 8, 2, 11, None, 13]
3
Max-heap of any height : 
 
         _______14______
        /               \
    ___12__            __13__
   /       \          /      \
  10        8        3        9
 /  \      / \      / \      /
1    5    4   6    0   2    7

Max-heap of given height : 
 
    __6__
   /     \
  4       5
 / \     / \
2   0   1   3

Perfect min-heap of given height : 
 
    __0__
   /     \
  1       3
 / \     / \
2   6   4   5
9
Binary tree from list :
 
    ___3
   /    \
  6      8
 / \      \
2   11     13


List from binary tree : [3, 6, 8, 2, 11, None, 13]
3from1from2

from3

conda install binarytree -c conda-forge
2 from5

pip install binarytree 
6
pip install binarytree 
7from8from9

binarytree 0binarytree 1

pip install binarytree 
6
pip install binarytree 
7binarytree 4binarytree 5

binarytree 0binarytree 7

Output:

Binary tree from list :
 
    ___3
   /    \
  6      8
 / \      \
2   11     13


List from binary tree : [3, 6, 8, 2, 11, None, 13]

Xây dựng một cây nhị phân ngẫu nhiên:

cây () tạo một cây nhị phân ngẫu nhiên và trả về nút gốc của nó. & nbsp;
 

Cú pháp: BinaryTree.Tree (height = 3, is_perfect = false) tham số: & nbsp; chiều cao: đó là chiều cao của cây và giá trị của nó có thể nằm giữa phạm vi 0-9 (bao gồm) & nbsp; nhị phân được tạo ra. returns: nút gốc của cây nhị phân. & nbsp; & nbsp;
Parameters: 
height: It is the height of the tree and its value can be between the range 0-9 (inclusive) 
is_perfect: If set True a perfect binary is created.
Returns: Root node of the binary tree. 
 

Example:

Python3

from binarytree import import1

conda install binarytree -c conda-forge
1
conda install binarytree -c conda-forge
2 import4

pip install binarytree 
6
pip install binarytree 
7import7
conda install binarytree -c conda-forge
5

pip install binarytree 
6
conda install binarytree -c conda-forge
00

conda install binarytree -c conda-forge
01
conda install binarytree -c conda-forge
2
conda install binarytree -c conda-forge
03
conda install binarytree -c conda-forge
2
Max-heap of any height : 
 
         _______14______
        /               \
    ___12__            __13__
   /       \          /      \
  10        8        3        9
 /  \      / \      / \      /
1    5    4   6    0   2    7

Max-heap of given height : 
 
    __6__
   /     \
  4       5
 / \     / \
2   0   1   3

Perfect min-heap of given height : 
 
    __0__
   /     \
  1       3
 / \     / \
2   6   4   5
5
conda install binarytree -c conda-forge
5

pip install binarytree 
6
pip install binarytree 
7
conda install binarytree -c conda-forge
09
conda install binarytree -c conda-forge
5

pip install binarytree 
6
conda install binarytree -c conda-forge
12

conda install binarytree -c conda-forge
13
conda install binarytree -c conda-forge
2
conda install binarytree -c conda-forge
03
conda install binarytree -c conda-forge
2
Max-heap of any height : 
 
         _______14______
        /               \
    ___12__            __13__
   /       \          /      \
  10        8        3        9
 /  \      / \      / \      /
1    5    4   6    0   2    7

Max-heap of given height : 
 
    __6__
   /     \
  4       5
 / \     / \
2   0   1   3

Perfect min-heap of given height : 
 
    __0__
   /     \
  1       3
 / \     / \
2   6   4   5
5from9

conda install binarytree -c conda-forge
19
conda install binarytree -c conda-forge
20
conda install binarytree -c conda-forge
2
conda install binarytree -c conda-forge
22
conda install binarytree -c conda-forge
5

pip install binarytree 
6
pip install binarytree 
7
conda install binarytree -c conda-forge
26
conda install binarytree -c conda-forge
5

pip install binarytree 
6
conda install binarytree -c conda-forge
29

Hướng dẫn python binary tree library - thư viện cây nhị phân python

Output: 

Binary tree of any height :

      14____
     /      \
    2        5__
   /        /   \
  6        1     13
 /        /     /  \
7        9     4    8

Binary tree of given height :

  1__
 /   \
5     2
     / \
    4   3

Perfect binary tree of given height :

    __3__
   /     \
  2       4
 / \     / \
6   0   1   5

Xây dựng một BST:

Cây tìm kiếm nhị phân là một loại cấu trúc dữ liệu cây đặc biệt có thứ tự cung cấp một danh sách các nút hoặc đỉnh được sắp xếp. Trong Python, chúng ta có thể trực tiếp tạo một đối tượng BST bằng mô -đun BinaryTree. BST () tạo một cây tìm kiếm nhị phân ngẫu nhiên và trả về nút gốc của nó. & nbsp;
 

Cú pháp: BinaryTree.bst (chiều cao = 3, is_perfect = false) tham số: & nbsp; chiều cao: đó là chiều cao của cây và giá trị của nó có thể nằm giữa phạm vi 0-9 (bao gồm) & nbsp; nhị phân được tạo ra. returns: nút gốc của bst. & nbsp; & nbsp;
Parameters: 
height: It is the height of the tree and its value can be between the range 0-9 (inclusive) 
is_perfect: If set True a perfect binary is created.
Returns: Root node of the BST. 
 

Example:

Python3

from binarytree import

conda install binarytree -c conda-forge
33

conda install binarytree -c conda-forge
1
conda install binarytree -c conda-forge
2
conda install binarytree -c conda-forge
36

pip install binarytree 
6
pip install binarytree 
7
conda install binarytree -c conda-forge
39from9

binarytree 0

conda install binarytree -c conda-forge
42

conda install binarytree -c conda-forge
01
conda install binarytree -c conda-forge
2
conda install binarytree -c conda-forge
45
conda install binarytree -c conda-forge
2
Max-heap of any height : 
 
         _______14______
        /               \
    ___12__            __13__
   /       \          /      \
  10        8        3        9
 /  \      / \      / \      /
1    5    4   6    0   2    7

Max-heap of given height : 
 
    __6__
   /     \
  4       5
 / \     / \
2   0   1   3

Perfect min-heap of given height : 
 
    __0__
   /     \
  1       3
 / \     / \
2   6   4   5
5
conda install binarytree -c conda-forge
5

pip install binarytree 
6
pip install binarytree 
7
conda install binarytree -c conda-forge
51from9

binarytree 0

conda install binarytree -c conda-forge
54

conda install binarytree -c conda-forge
13
conda install binarytree -c conda-forge
2
conda install binarytree -c conda-forge
45
conda install binarytree -c conda-forge
2
Max-heap of any height : 
 
         _______14______
        /               \
    ___12__            __13__
   /       \          /      \
  10        8        3        9
 /  \      / \      / \      /
1    5    4   6    0   2    7

Max-heap of given height : 
 
    __6__
   /     \
  4       5
 / \     / \
2   0   1   3

Perfect min-heap of given height : 
 
    __0__
   /     \
  1       3
 / \     / \
2   6   4   5
5binarytree 5

conda install binarytree -c conda-forge
61
conda install binarytree -c conda-forge
20
conda install binarytree -c conda-forge
2
conda install binarytree -c conda-forge
222

pip install binarytree 
6
pip install binarytree 
7
conda install binarytree -c conda-forge
68from9

binarytree 0

conda install binarytree -c conda-forge
71

Output:

BST of any height : 
 
        ____9______
       /           \
    __5__       ____12___
   /     \     /         \
  2       8   10         _14
 / \     /      \       /
1   4   7        11    13

BST of given height : 
 
    5
   / \
  4   6
 /
3

Perfect BST of given height : 
 
    __3__
   /     \
  1       5
 / \     / \
0   2   4   6

Nhập đống:

HEAP là cấu trúc dữ liệu cây có thể có hai loại - & nbsp;

  • Tối đa HEAP & NBSP;
  • Min Heap & nbsp; & nbsp;
     

Sử dụng phương thức HEAP () của thư viện binytree, chúng ta có thể tạo ra một maxHeap ngẫu nhiên và trả về nút gốc của nó. Để tạo Minheap, chúng ta cần đặt thuộc tính IS_MAX là sai. & NBSP;
 

Cú pháp: BinaryTree.Heap (chiều cao = 3, is_max = true, is_perfect = false) tham số: & nbsp; chiều cao: đó là chiều cao của cây và giá trị của nó có thể nằm giữa phạm vi 0-9 (bao gồm) & nbsp; is_max: nếu Đặt True tạo ra một heap tối đa khác Min Heap. & NBSP; IS_PERFECT: Nếu đặt đúng một nhị phân hoàn hảo được tạo ra. returns: nút gốc của heap. & nbsp; & nbsp; & nbsp;
Parameters: 
height: It is the height of the tree and its value can be between the range 0-9 (inclusive) 
is_max: If set True generates a max heap else min heap. 
is_perfect: If set True a perfect binary is created.
Returns: Root node of the heap. 
  

Python3

from binarytree import

conda install binarytree -c conda-forge
75

conda install binarytree -c conda-forge
1
conda install binarytree -c conda-forge
2
conda install binarytree -c conda-forge
78

pip install binarytree 
6
pip install binarytree 
7
conda install binarytree -c conda-forge
81from9

binarytree 0

conda install binarytree -c conda-forge
42

conda install binarytree -c conda-forge
01
conda install binarytree -c conda-forge
2
conda install binarytree -c conda-forge
87
conda install binarytree -c conda-forge
2
Max-heap of any height : 
 
         _______14______
        /               \
    ___12__            __13__
   /       \          /      \
  10        8        3        9
 /  \      / \      / \      /
1    5    4   6    0   2    7

Max-heap of given height : 
 
    __6__
   /     \
  4       5
 / \     / \
2   0   1   3

Perfect min-heap of given height : 
 
    __0__
   /     \
  1       3
 / \     / \
2   6   4   5
5
conda install binarytree -c conda-forge
5

pip install binarytree 
6
pip install binarytree 
7
conda install binarytree -c conda-forge
93from9

binarytree 0

conda install binarytree -c conda-forge
54

conda install binarytree -c conda-forge
13
conda install binarytree -c conda-forge
2
conda install binarytree -c conda-forge
87
conda install binarytree -c conda-forge
2
Max-heap of any height : 
 
         _______14______
        /               \
    ___12__            __13__
   /       \          /      \
  10        8        3        9
 /  \      / \      / \      /
1    5    4   6    0   2    7

Max-heap of given height : 
 
    __6__
   /     \
  4       5
 / \     / \
2   0   1   3

Perfect min-heap of given height : 
 
    __0__
   /     \
  1       3
 / \     / \
2   6   4   5
5binarytree 5

conda install binarytree -c conda-forge
19
pip install binarytree 
04
conda install binarytree -c conda-forge
2
pip install binarytree 
06binarytree 5

conda install binarytree -c conda-forge
19
conda install binarytree -c conda-forge
20
conda install binarytree -c conda-forge
2
conda install binarytree -c conda-forge
22
conda install binarytree -c conda-forge
5

pip install binarytree 
6
pip install binarytree 
7
pip install binarytree 
15from9

binarytree 0

conda install binarytree -c conda-forge
71

Output:

Max-heap of any height : 
 
         _______14______
        /               \
    ___12__            __13__
   /       \          /      \
  10        8        3        9
 /  \      / \      / \      /
1    5    4   6    0   2    7

Max-heap of given height : 
 
    __6__
   /     \
  4       5
 / \     / \
2   0   1   3

Perfect min-heap of given height : 
 
    __0__
   /     \
  1       3
 / \     / \
2   6   4   5