Module treeutil
[hide private]
[frames] | no frames]

Module treeutil

source code

treeutil

Contains all sort of utilities to search and manipulate nested lists as trees.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Author: by Mehdi Khoury

Version: 1.20

Copyright: (c) 2009 Mehdi Khoury under the mit license http://www.opensource.org/licenses/mit-license.html

Classes [hide private]
  TreeUtilError
  NestedHeadNode
  NotAList
  EmptyList
  EmptyDict
  CalculationError
  WrongValues
Functions [hide private]
 
listGetRootNode(myTree)
get the root node of a nested list
source code
 
BFS_Search(myTree)
Traverse the nodes of a tree in breadth-first order.
source code
 
walk(seq)
Walk over a sequence of items, printing each one in turn, and recursively walking over sub-sequences.
source code
 
getChildrenNodes2(lst, depth)
Get the children nodes in a nested list for a specific depth
source code
 
PostOrder_Search(myTree)
Traverse the nodes of a tree by getting the leafs first and the branches after.
source code
 
DFS_Search(seq)
a recursive generator that flatten a nested list gives a list of all nodes in the tree in Depth First Search order
source code
 
BranchNodes_Search(myTree)
a generator to iterate through branch nodes only in BFS order.
source code
 
LeafNodes_Search(myTree)
a generator to iterate through leaf nodes only in right-to-left BFS preorder.
source code
 
isNested(myList)
Check if a list is nested
source code
 
list_getTail(myList)
get the tail of a 'node' e.g.
source code
 
getSubLists(myList)
get a list of all sub lists in a nested list
source code
 
nestedListToDict(myList)
get a dictionnary from a nested list
source code
 
dictFindRootNode(myDict)
find the root node of a tree in a dictionary (assumption is that a root node is a key never found in any of all values)
source code
 
dictGetRootList(myDict)
get the list coresponding to the root node
source code
 
dictToSubLists(myDict)
get a list version of the dictionary
source code
 
nestedInsert(list1, list2)
nest a list inside another one if the first one has elements which are head of lists or sub lists in list 2
source code
 
dictToNestedList(myDict)
finally, transform the dictionary into a nested list
source code
Variables [hide private]
  __package__ = None
Function Details [hide private]

listGetRootNode(myTree)

source code 

Function: listGetRootNode

get the root node of a nested list

Parameters:
  • myTree - the nested list representing a tree
Returns:
the root node of a nested list

BFS_Search(myTree)

source code 

Function: BFS_Search

Traverse the nodes of a tree in breadth-first order.

Parameters:
  • myTree - the nested list representing a tree
Returns:
an generator for a list of nodes in Breath First Search order

walk(seq)

source code 

Function: walk

Walk over a sequence of items, printing each one in turn, and recursively walking over sub-sequences.

Parameters:
  • seq - the nested list representing a tree

getChildrenNodes2(lst, depth)

source code 

Function: getChildrenNodes2

Get the children nodes in a nested list for a specific depth

Parameters:
  • lst - the nested list representing a tree
  • depth - the nested list representing a tree
Returns:
a flat list of children nodes for a specific depth

PostOrder_Search(myTree)

source code 

Function: PostOrder_Search

Traverse the nodes of a tree by getting the leafs first and the branches after. Finishes with the root. e.g. [1,[2,[3,4,[5,6,7]]],[8,[9,10,11]],[12,13,14]] gives : [7, 6, 5, 4, 3, 2, 11, 10, 9, 8, 14, 13, 12, 1]

Parameters:
  • myTree - the nested list representing a tree
Returns:
a flat list of nodes in PostOrder Search

DFS_Search(seq)

source code 

Function: DFS_Search

a recursive generator that flatten a nested list gives a list of all nodes in the tree in Depth First Search order

Parameters:
  • seq - the nested list representing a tree
Returns:
a flat list of nodes in Depth First Search order

BranchNodes_Search(myTree)

source code 

Function: BranchNodes_Search

a generator to iterate through branch nodes only in BFS order.

Parameters:
  • myTree - the nested list representing a tree
Returns:
a flat list of branch nodes nodes in BFS order.

LeafNodes_Search(myTree)

source code 

Function: LeafNodes_Search

a generator to iterate through leaf nodes only in right-to-left BFS preorder.

Parameters:
  • myTree - the nested list representing a tree
Returns:
a flat list of leaf nodes nodes in BFS preorder.

isNested(myList)

source code 

Function: isNested

Check if a list is nested

Parameters:
  • myList - the nested list representing a tree
Returns:
1 if nested , 0 if not.

list_getTail(myList)

source code 

Function: list_getTail

get the tail of a 'node' e.g. [1,2]->[2] e.g. [1,[2,4],5]->[2,5]

Parameters:
  • myList - the nested list representing a tree
Returns:
the tail of the list

getSubLists(myList)

source code 

Function: getSubLists

get a list of all sub lists in a nested list

Parameters:
  • myList - the nested list representing a tree
Returns:
a list of all sub lists in a nested list

nestedListToDict(myList)

source code 

Function: nestedListToDict

get a dictionnary from a nested list

Parameters:
  • myList - the nested list representing a tree
Returns:
a dictionary

dictFindRootNode(myDict)

source code 

Function: dictFindRootNode

find the root node of a tree in a dictionary (assumption is that a root node is a key never found in any of all values)

Parameters:
  • myDict - the dictionary representing a tree
Returns:
a dictionary

dictGetRootList(myDict)

source code 

Function: dictGetRootList

get the list coresponding to the root node

Parameters:
  • myDict - the dictionary representing a tree
Returns:
the list coresponding to the root node

dictToSubLists(myDict)

source code 

Function: dictToSubLists

get a list version of the dictionary

Parameters:
  • myDict - the dictionary representing a tree
Returns:
the list corresponding to the dictionary

nestedInsert(list1, list2)

source code 

Function: nestedInsert

nest a list inside another one if the first one has elements which are head of lists or sub lists in list 2

Parameters:
  • list1 - flat list 1
  • list1 - flat list 2
Returns:
resulting nested list

dictToNestedList(myDict)

source code 

Function: dictToNestedList

finally, transform the dictionary into a nested list

Parameters:
  • myDict - the dictionary representing a tree
Returns:
the nested list corresponding to the dictionary