Prototyping 21 May 2013: Difference between revisions
Line 13: | Line 13: | ||
http://en.wikipedia.org/wiki/Tree_traversal | http://en.wikipedia.org/wiki/Tree_traversal | ||
[[File:Sorted_binary_tree_preorder.svg|500px]][[Sorted_binary_tree_breadth-first_traversal.svg|500px]] | [[File:Sorted_binary_tree_preorder.svg|500px]][[File:Sorted_binary_tree_breadth-first_traversal.svg|500px]] | ||
http://localhost/doc/python2.7/html/library/xml.etree.elementtree.html?highlight=element#xml.etree.ElementTree | http://localhost/doc/python2.7/html/library/xml.etree.elementtree.html?highlight=element#xml.etree.ElementTree |
Revision as of 08:26, 21 May 2013
- Tree
- Walking a tree
- Recursive traversal
- Depth-first traversal
- Breadth-first traversal
ElementTree
http://docs.python.org/2/library/xml.etree.elementtree.html
Tree Traversal
http://en.wikipedia.org/wiki/Tree_traversal
File:Sorted binary tree preorder.svgFile:Sorted binary tree breadth-first traversal.svg
Walking the tree
ElementTree the fundamental unit is the Element
And Element has:
- .tag (a string representing the name of the tag, like "p" or "script")
- .attrib (a "dictionary" with name=value pairs of the tag attributes, like id="foo", or style="color: blue")
- .text (String of text contents of the node)
- .tail (if there's text after child tags, it'd be here)
In addition (and this is why it's a tree), each element can be iterated / treated like a list of all sub-elements.
- Iteration to access contained "child" elements
Walk
def walk (node):
print node.tag
for child in node:
walk(child)
Extracting the text of a node
(function exists?)
How to show the structure in a more tree way?
center... breadth first traversal!
Cherry picking
(collecting things while walking the tree)