TreeNode is a Python library that provides functionality to create and manipulate tree structures.
Installation
You can install TreeNode using pip:
pip install treenode-py
Usage
To use TreeNode, start by importing the TreeNode class from the treenode module:
from treenode import TreeNode
Tree
You can create a tree by initializing a TreeNode object with the root node's name:
tree = TreeNode("name of the root node")
Child Nodes
To add child nodes to the tree, use the add_child()
method:
child1 = tree.add_child("name of child node 1")
You can use the slash=False
to remove a slash after node:
child2 = tree.add_child("name of child node 2", slash=False)
You can also add subchild nodes to existing child nodes:
subchild1 = child1.add_child("Subchild 1", slash=False)
subchild2 = child1.add_child("Subchild 2", slash=False)
Printing the Tree and Nodes
To print the tree, simply use the print()
function:
print(tree)
This will be our full code:
from treenode import TreeNode
tree = TreeNode("name of tree")
child1 = tree.add_child("name of child node 1")
child2 = tree.add_child("name of child node 2", slash=False)
subchild1 = child1.add_child("Subchild 1", slash=False)
subchild2 = child1.add_child("Subchild 2", slash=False)
print(tree)
This will output(check github if you on pypi):
Root/
в”њв”Ђв”Ђ Child 1/
в”‚ в”њв”Ђв”Ђ Subchild 1
в”‚ в””в”Ђв”Ђ Subchild 2
в””в”Ђв”Ђ Child 2
You can retrieve various information about the tree, such as the depth, number of files, number of folders, list of files, and list of folders.
Documentation
Class Methods
__init__(self, name, slash=True)
Initializes a tree node.
name (str)
: The name of the node.
slash (bool, optional)
: Indicates whether the node represents a directory path with /
. Defaults to True
.
Usage:
from treenode import TreeNode
tree1 = TreeNode("name of empty tree with directory slash")
tree2 = TreeNode("name of empty tree without directory slash", slash=False)
print(tree1)
print(tree2)
add_child(self, name, slash=True)
Creates a child node with the given name and adds it to the current node.
name (str)
: The name of the child node.
slash (bool, optional)
: Indicates whether the node represents a directory path with /
. Defaults to True
.
Usage:
from treenode import TreeNode
tree = TreeNode("name of tree")
folderchild = tree.add_child("name of child node 1")
filechild = tree.add_child("name of child file", slash=False)
filechild_of_folderchild = folderchild.add_child("name of subchild file", slash=False)
print(tree)
generate_treepath(self, path)
Generates a tree structure for the given directory path.
path (str)
: The directory path.
Returns:
TreeNode
: The root node of the generated tree structure.
Usage:
from treenode import TreeNode
import os
path = input("Enter the path to the folder: ")
tree = TreeNode(os.path.basename(path)).generate_treepath(path)
print(tree)
find_node(self, name)
Finds a node with the given name.
name (str)
: The name of the node to find.
Returns:
TreeNode or None
: The node if found, otherwise None
.
Usage:
from treenode import TreeNode
tree = TreeNode("name of tree")
folder = tree.add_child("name of folder")
file = folder.add_child("name of file", slash=False)
node_name = "name of folder"
found_node = tree.find_node(node_name)
print(tree)
print(f"Node '{node_name}' found: \n{found_node}")
is_empty(self)
Checks if the node is empty (has no children).
Returns:
bool
: True
if the node is empty, otherwise False
.
Usage:
from treenode import TreeNode
tree = TreeNode("name of tree")
empty = tree.is_empty()
print(tree)
print(f"Tree is empty?: {empty}")
get_depth(self)
Calculates the depth of the tree.
Returns:
int
: The depth of the tree.
Usage:
from treenode import TreeNode
tree = TreeNode("name of tree")
folder = tree.add_child("name of folder")
file = folder.add_child("name of file", slash=False)
depth = tree.get_depth()
print(tree)
print(f"Depth: {depth}")
get_files(self)
Retrieves a list of all files in the tree.
Returns:
list
: A list of file names.
Usage:
from treenode import TreeNode
tree = TreeNode("name of tree")
file = tree.add_child("name of file", slash=False)
getted = tree.get_folders()
print(tree)
print(f"Files: {getted}")
Retrieves a list of all folders in the tree.
Returns:
list
: A list of folder names.
Usage:
from treenode import TreeNode
tree = TreeNode("name of tree")
folder = tree.add_child("name of folder")
getted = tree.get_folders()
print(tree)
print(f"Folders: {getted}")
count_files(self)
Counts the total number of files in the tree.
Returns:
int
: The total number of files.
Usage:
from treenode import TreeNode
tree = TreeNode("name of tree")
file = tree.add_child("name of file", slash=False)
counted = tree.count_files()
print(tree)
print(f"Files: {counted}")
Counts the total number of folders in the tree.
Returns:
int
: The total number of folders.
Usage:
from treenode import TreeNode
tree = TreeNode("name of tree")
folder = tree.add_child("name of folder")
counted = tree.count_folders()
print(tree)
print(f"Folders: {counted}")