Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get all leaf nodes in radix tree #46

Open
RSwarnkar opened this issue Nov 13, 2019 · 1 comment
Open

How to get all leaf nodes in radix tree #46

RSwarnkar opened this issue Nov 13, 2019 · 1 comment

Comments

@RSwarnkar
Copy link

How to get all leaf nodes from radix tree?

@ljluestc
Copy link

ljluestc commented Jun 9, 2024

import radix

# Function to check if a node is a leaf node
def is_leaf(node):
    return not node.prefixes()

# Function to collect all leaf nodes
def get_all_leaf_nodes(rt):
    leaf_nodes = []
    for node in rt:
        if is_leaf(node):
            leaf_nodes.append(node)
    return leaf_nodes

# Example usage
def main():
    rt = radix.Radix()

    # Adding some prefixes
    rt.add("1.2.2.0/23")
    rt.add("1.2.2.0/24")
    rt.add("1.2.3.0/24")
    rt.add("1.2.4.0/24")

    # Getting all leaf nodes
    leaf_nodes = get_all_leaf_nodes(rt)
    
    # Printing all leaf nodes
    for leaf in leaf_nodes:
        print(leaf.prefix)

if __name__ == "__main__":
    main()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants