binarytree Documentation

repository·main·Indexed 23 days ago

https://github.com/joowani/binarytree

A Python library for studying and visualizing binary trees. It provides tools to generate random binary trees, BSTs, and heaps, as well as utilities for tree traversal (inorder, preorder, postorder, levelorder), level-order indexing, and conversion between trees and list representations. The library supports rendering trees as images using Graphviz via the Node.graphviz() method.

Tokens
1.9K
Snippets
6
Records
16
Agent score
83%

What's inside binarytree

  1. Manipulate trees using level-order (breadth-first) indexing

    main

    Binarytree supports level-order indexing, allowing you to access, replace, or delete nodes using their position in a breadth-first traversal. This is similar to how binary heaps are represented in arrays.

    • Access: Use root[index] to get the node at that index.
    • Replace: Use root[index] = new_node to replace a subtree.
    • Delete: Use del root[index] to remove a subtree.
    • Visualization: Use root.pprint(index=True) to print the tree with both node values and their level-order indices.
    from binarytree import Node
    
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    root.left.right = Node(4)
    root.left.right.left = Node(5)
    
    # View indices
    root.pprint(index=True)
    
    # Access node at index 9
    node_9 = root[9]
    
    # Replace node at index 4
    root[4] = Node(6, left=Node(7), right=Node(8))
    
    # Delete node at index 1
    del root[1]
  2. Install Graphviz for tree rendering

    main

    To use the tree rendering features in binarytree (version 6.0.0+), you must install the Graphviz software on your operating system and ensure the Graphviz executables are in your system PATH.

    # Ubuntu and Debian
    sudo apt install graphviz
    
    # Fedora and CentOS
    sudo yum install graphviz
    
    # Windows using choco (or winget)
    choco install graphviz
  3. Convert trees to and from list representations

    main

    You can convert a tree to a list representation (where all null nodes in each level are present) or a more compact representation (often used in LeetCode).

    • root.values: Returns the standard list representation.
    • root.values2: Returns the compact list representation.
    • build(list): Builds a tree from the standard list representation.
    • build2(list): Builds a tree from the compact list representation.
  4. Generate trees with binarytree.build and binarytree.build2

    main
    Use binarytree.build or binarytree.build2 to construct binary tree structures. (Note: Specific parameter details are not provided in this specification segment, but these functions are the primary entry points for manual tree construction).
  5. Generate and render trees using Node.graphviz()

    main

    You can use the Node.graphviz() method to generate a graphviz.Digraph object from a binary tree. This object can be used to inspect the DOT (graph description language) body or to render the tree as an image. Any arguments passed to graphviz() are passed directly into the Digraph.__init__ constructor.

    from binarytree import tree
    
    t = tree()
    
    # Generate a graphviz.Digraph object
    # Arguments to this method are passed into Digraph.__init__
    graph = t.graphviz()
    
    # Get DOT (graph description language) body
    print(graph.body)
    
    # Render the binary tree
    graph.render()
  6. Traverse trees using standard algorithms

    main

    The Node class provides properties that return lists of nodes following standard traversal orders:

    • inorder
    • preorder
    • postorder
    • levelorder (also accessible via list(root))
    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.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))     # [Node(1), Node(2), Node(3), Node(4), Node(5)]
  7. Build and inspect custom trees using the Node class

    main
    You can manually construct a tree by instantiating Node objects and assigning them to left and right attributes. Once built, you can inspect various structural and value-based properties directly from the root node.