Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/treelib/__init__.py: 75%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

4 statements  

1# Copyright (C) 2011 

2# Brett Alistair Kromkamp - brettkromkamp@gmail.com 

3# Copyright (C) 2012-2025 

4# Xiaming Chen - chenxm35@gmail.com 

5# and other contributors. 

6# All rights reserved. 

7""" 

8treelib - Efficient Tree Data Structure for Python 

9 

10`treelib` is a Python library providing a comprehensive tree data structure implementation. 

11It features two primary classes: Node and Tree, designed for maximum efficiency and ease of use. 

12 

13Key Features: 

14 - O(1) node access and search operations 

15 - Multiple tree traversal algorithms (DFS, BFS, ZigZag) 

16 - Rich tree visualization with customizable display formats 

17 - Flexible tree modification operations (add, remove, move, copy) 

18 - Data export/import (JSON, dictionary, GraphViz DOT) 

19 - Subtree operations and advanced filtering 

20 - Support for custom data payloads on nodes 

21 - Memory-efficient design with shallow/deep copy options 

22 

23Architecture: 

24 - **Tree**: Self-contained hierarchical structure managing nodes and relationships 

25 - **Node**: Elementary objects storing data and maintaining parent-child links 

26 - Each tree has exactly one root node (or none if empty) 

27 - Each non-root node has exactly one parent and zero or more children 

28 

29Quick Start: 

30 .. code-block:: python 

31 

32 from treelib import Tree, Node 

33 

34 # Create and populate a tree 

35 tree = Tree() 

36 tree.create_node("Root", "root") 

37 tree.create_node("Child A", "a", parent="root") 

38 tree.create_node("Child B", "b", parent="root") 

39 tree.create_node("Grandchild", "a1", parent="a") 

40 

41 # Display the tree 

42 tree.show() 

43 

44 # Tree traversal 

45 for node_id in tree.expand_tree(mode=Tree.DEPTH): 

46 print(f"Visiting: {tree[node_id].tag}") 

47 

48 # Export to JSON 

49 json_data = tree.to_json() 

50 

51Common Use Cases: 

52 - File system representations 

53 - Organizational hierarchies 

54 - Decision trees and game trees 

55 - Category/taxonomy structures 

56 - Menu and navigation systems 

57 - Abstract syntax trees 

58 - Family trees and genealogy 

59 

60Compatibility Note: 

61 To ensure string compatibility between Python 2.x and 3.x, treelib follows 

62 Python 3.x string handling conventions. All strings are handled as unicode. 

63 

64 For Python 2.x users with non-ASCII characters, enable unicode literals: 

65 

66 .. code-block:: python 

67 

68 from __future__ import unicode_literals 

69""" 

70 

71from .node import Node # noqa: F401 

72from .tree import Tree # noqa: F401