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ôngPython 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útLớ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;
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. Python3
conda install binarytree -c conda-forge0 conda install binarytree -c conda-forge1 conda install binarytree -c conda-forge2 conda install binarytree -c conda-forge3 conda install binarytree -c conda-forge4 conda install binarytree -c conda-forge5 conda install binarytree -c conda-forge6 conda install binarytree -c conda-forge2 conda install binarytree -c conda-forge3 conda install binarytree -c conda-forge9 conda install binarytree -c conda-forge5 pip install binarytree1 conda install binarytree -c conda-forge2 conda install binarytree -c conda-forge3 pip install binarytree4 conda install binarytree -c conda-forge5 pip install binarytree6 pip install binarytree7 pip install binarytree8 pip install binarytree9 pip install binarytree6 pip install binarytree7 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 binarytree6 pip install binarytree7 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 binarytree6 pip install binarytree7 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 52 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 53 pip install binarytree6 pip install binarytree7 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 56 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 57 pip install binarytree6 pip install binarytree7 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 60 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 61 Output:
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;
Example: Python3
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 65 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 66 conda install binarytree -c conda-forge2 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 68 conda install binarytree -c conda-forge4 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-forge9 Binary tree from list : ___3 / \ 6 8 / \ \ 2 11 13 List from binary tree : [3, 6, 8, 2, 11, None, 13]3 pip install binarytree4 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 55 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 57 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 59 Binary tree from list : ___3 / \ 6 8 / \ \ 2 11 13 List from binary tree : [3, 6, 8, 2, 11, None, 13]3 from 1from 2
conda install binarytree -c conda-forge2 from 5pip install binarytree6 pip install binarytree7 from 8from 9
pip install binarytree6 pip install binarytree7 binarytree 4binarytree 5
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;
Example: Python3
conda install binarytree -c conda-forge1 conda install binarytree -c conda-forge2 import 4pip install binarytree6 pip install binarytree7 import 7conda install binarytree -c conda-forge5 pip install binarytree6 conda install binarytree -c conda-forge00 conda install binarytree -c conda-forge01 conda install binarytree -c conda-forge2 conda install binarytree -c conda-forge03 conda install binarytree -c conda-forge2 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 55 conda install binarytree -c conda-forge5 pip install binarytree6 pip install binarytree7 conda install binarytree -c conda-forge09 conda install binarytree -c conda-forge5 pip install binarytree6 conda install binarytree -c conda-forge12 conda install binarytree -c conda-forge13 conda install binarytree -c conda-forge2 conda install binarytree -c conda-forge03 conda install binarytree -c conda-forge2 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 55 from 9conda install binarytree -c conda-forge19 conda install binarytree -c conda-forge20 conda install binarytree -c conda-forge2 conda install binarytree -c conda-forge22 conda install binarytree -c conda-forge5 pip install binarytree6 pip install binarytree7 conda install binarytree -c conda-forge26 conda install binarytree -c conda-forge5 pip install binarytree6 conda install binarytree -c conda-forge29 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;
Example: Python3
conda install binarytree -c conda-forge33 conda install binarytree -c conda-forge1 conda install binarytree -c conda-forge2 conda install binarytree -c conda-forge36 pip install binarytree6 pip install binarytree7 conda install binarytree -c conda-forge39 from 9
conda install binarytree -c conda-forge42 conda install binarytree -c conda-forge01 conda install binarytree -c conda-forge2 conda install binarytree -c conda-forge45 conda install binarytree -c conda-forge2 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 55 conda install binarytree -c conda-forge5 pip install binarytree6 pip install binarytree7 conda install binarytree -c conda-forge51 from 9
conda install binarytree -c conda-forge54 conda install binarytree -c conda-forge13 conda install binarytree -c conda-forge2 conda install binarytree -c conda-forge45 conda install binarytree -c conda-forge2 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 55 binarytree 5conda install binarytree -c conda-forge61 conda install binarytree -c conda-forge20 conda install binarytree -c conda-forge2 conda install binarytree -c conda-forge222 pip install binarytree6 pip install binarytree7 conda install binarytree -c conda-forge68 from 9
conda install binarytree -c conda-forge71 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;
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;
Python3
conda install binarytree -c conda-forge75 conda install binarytree -c conda-forge1 conda install binarytree -c conda-forge2 conda install binarytree -c conda-forge78 pip install binarytree6 pip install binarytree7 conda install binarytree -c conda-forge81 from 9
conda install binarytree -c conda-forge42 conda install binarytree -c conda-forge01 conda install binarytree -c conda-forge2 conda install binarytree -c conda-forge87 conda install binarytree -c conda-forge2 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 55 conda install binarytree -c conda-forge5 pip install binarytree6 pip install binarytree7 conda install binarytree -c conda-forge93 from 9
conda install binarytree -c conda-forge54 conda install binarytree -c conda-forge13 conda install binarytree -c conda-forge2 conda install binarytree -c conda-forge87 conda install binarytree -c conda-forge2 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 55 binarytree 5conda install binarytree -c conda-forge19 pip install binarytree04 conda install binarytree -c conda-forge2 pip install binarytree06 binarytree 5conda install binarytree -c conda-forge19 conda install binarytree -c conda-forge20 conda install binarytree -c conda-forge2 conda install binarytree -c conda-forge22 conda install binarytree -c conda-forge5 pip install binarytree6 pip install binarytree7 pip install binarytree15 from 9
conda install binarytree -c conda-forge71 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 |