GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - brettbates/p4Tree: pyTree with added access level. Based completely off of: https://github.com/caesar0301/pyTree, all credit goes to the creator

BranchesTags
Open more actions menu

Latest commit

 

History

89 Commits
89 Commits

Folders and files

NameName
Last commit message
Last commit date
examples
examples
 
 
p4treelib.egg-info
p4treelib.egg-info
 
 
p4treelib
p4treelib
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
HISTORY
HISTORY
 
 
INSTALL
INSTALL
 
 
LICENSE
LICENSE
 
 
MANIFEST.in
MANIFEST.in
 
 
README.md
README.md
 
 
requirements-testing.txt
requirements-testing.txt
 
 
setup.cfg
setup.cfg
 
 
setup.py
setup.py
 
 
test.sh
test.sh
 
 

Repository files navigation

pyTree (treelib on PyPI)

Tree Implementation in python: simple to use for you.

How to install

sudo pip install -U treelib

or

sudo easy_install -U treelib

Basic Usage

  • Example 1: Create a tree

    tree = Tree() tree.create_node("Harry", "harry") # root node tree.create_node("Jane", "jane", parent = "harry") tree.create_node("Bill", "bill", parent = "harry") tree.create_node("Diane", "diane", parent = "jane") tree.create_node("George", "george", parent = "diane") tree.create_node("Mary", "mary", parent = "diane") tree.create_node("Jill", "jill", parent = "george") tree.create_node("Mark", "mark", parent = "jane") tree.show()

Result:

Harry[harry]
|___ Jane[jane]
|    |___ Diane[diane]
|         |___ George[george]
|              |___ Jill[jill]
|         |___ Mary[mary]
|    |___ Mark[mark]
|___ Bill[bill]
  • Example 2: expand a tree with mode being Tree.DEPTH or Tree.WIDTH

    for node in tree.expand_tree(mode=Tree.DEPTH): print tree[node].name

Result:

Harry
Jane
Diane
George
Jill
Mary
Mark
Bill
  • Example 2a: expand a tree with mode being Tree.ZIGZAG (WIDTH-first, level by level, first level left-to-right, second - right-to-left and so on)

    for node in tree.expand_tree(mode=Tree.ZIGZAG): print tree[node].identifier

Result:

harry
bill
jane
diane
mark
mary
george
jill
  • Example 3: expand tree with filter

    for node in tree.expand_tree(filter = lambda x: x.identifier != 'george'): print tree[node].name

Result:

Harry
Jane
Mark
Bill
  • Example 4: get a subtree

    sub_t = tree.subtree('diane') sub_t.show()

Result:

Diane[diane]
|___ George[george]
|    |___ Jill[jill]
|___ Mary[mary]
  • Example 5: paste a new tree to original one

    new_tree = Tree() new_tree.create_node("n1", "1") # root node new_tree.create_node("n2", "2", parent = "1") new_tree.create_node("n3", "3", parent = "1") tree.paste('jill', new_tree) tree.show()

Result:

Harry[harry]
|___ Jane[jane]
|    |___ Diane[diane]
|         |___ George[george]
|              |___ Jill[jill]
|                   |___ n1[1]
|                        |___ n2[2]
|                        |___ n3[3]
|         |___ Mary[mary]
|    |___ Mark[mark]
|___ Bill[bill]
  • Example 6: remove the existing node from the tree

    tree.remove_node('1') tree.show()

Result:

As the result of example 1
  • Example 7: Move a node

    tree.move_node('jill', 'harry') tree.show()

Result:

Harry[harry]
|___ Jane[jane]
|    |___ Diane[diane]
|         |___ George[george]
|         |___ Mary[mary]
|    |___ Mark[mark]
|___ Bill[bill]
|___ Jill[jill]

Advanced Usage

You can also inherit and modify the behaviors of the tree structure to meet your need easily and conveniently. For example, to define a tree structure with data payload for each node, you can program like the way below:

import treelib

class myNode(treelib.node):
    def __init__(self, payload):
        self.data = payload
...
new_node = myNode("1234567890")

Useful APIs

Add the import declaration to use treelib in your project:

from treelib import Node, Tree

This module treelib mainly contains two data structures: Node and Tree.

The structure Node defines basic properties such as node identifier (unique ID in the environment of a tree), node name (readable for human), parent node, children nodes etc., and some public operations on a node. (e.g., a in the description below):

# To create a new node object.
a = Node([tag[, identifier[, expanded]]])

# To get or set the ID of the node
a.identifier [=nid]

# To get or set the ID of a's parent node
a.bpointer [=value]
a.update_bpointer(nid) # for set only

# As a getting operator, ID list of a's SON nodes is obtained.
# As a setting operator, the value can be list, set, or dict.
# For list or set, it is converted to a list type by the packeage;
# For dict, the keys are treated as the node IDs
a.fpointer [=value]

# Update the children list with different modes
a.update_fpointer(identifier, mode=[Node.ADD, Node.DELETE, Node.INSERT])

The class Tree defines the tree-like structure based on the node structure. Public methods are also available to make operations on the tree, e.g. a Tree object t:

# To create a new object of tree structure
t = Tree()

# To get or set the ID of the root
t.root [=nid]

# To get the list of all the nodes randomly belonging to this tree
t.all_nodes()

# Add a new node object to the tree and make the parent as the root by default
t.add_node(node[, parent])

# To create a new node and add it to this tree
t.create_node(name[,identifier[,parent]])

# To traverse the tree nodes with different modes; NOTE:
# `nid` refers to the expanding point to start;
# `mode` refers to the search mode (Tree.DEPTH, Tree.WIDTH);
# `filter` refers to the function of one varible to act on the **node object**;
# `cmp`, `key`, `reverse` are present to sort **node objects** in the same level.
t.expand_tree([nid[,mode[,filter[,cmp[,key[,reverse]]]]]]) 

# To get the object of the node with ID of nid
# An alternative way is using '[]' operation on the tree.
# But small difference exists between them:
# the get_node() will return None if nid is absent, whereas '[]' will raise KeyError.
t.get_node(nid)

# To get the children (only sons) list of the node with ID == nid.
t.is_branch(nid)

# To move node (source) from its parent to another parent (destination).
t.move_node(source, destination)

# To paste a new tree to an existing tree, with `nid` becoming the parent of the root of this new tree.
t.paste(nid, new_tree) 

# To remove the node and free the memory along with its successors.
t.remove_node(identifier)

# To search the tree from `nid` to the root along links reversedly
# Note: `filter` refers to the function of one varible to act on the **node object**.
t.rsearch(nid[,filter]) 

# To print the tree structure in hierarchy style;
# Note:
# `nid` refers to the expanding point to start;
# `level` refers to the node level in the tree (root as level 0);
# `idhidden` refers to hiding the node ID when priting;
# `filter` refers to the function of one varible to act on the **node object**;
# `cmp`, `key`, `reverse` are present to sort **node objects** in the same level.
t.show([nid[,level[,idhidden[,filter[,cmp[,key[,reverse]]]]]]])

# To return a soft copy of the subtree with `nid` being the root; The softness 
# means all the nodes are shared between subtree and the original.
t.subtree(nid)

# Save the tree into file for offline analysis.
t.save2file(filename[,nid[,level[,idhidden[,filter[,cmp[,key[,reverse]]]]]]])

Contributors

  • Brett Alistair Kromkamp ([email protected]): Basic framework finished.

  • Xiaming Chen ([email protected]): For reasearch utility, I finish main parts and make the library freely public.

  • Holger Bast ([email protected]): Replaces list with dict for nodes indexing and improves the performance to a large extend.

  • Ilya Kuprik ([email protected]): Added ZIGZAG tree-walk mode to function tree_expand()

About

pyTree with added access level. Based completely off of: https://github.com/caesar0301/pyTree, all credit goes to the creator

Resources

Readme
License
Activity

Stars

1 star

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.