Python3 解析XML 层序遍历二叉树
Python3 解析XML 层序遍历二叉树
keyword : python3, xml, xml.dom.minidom, 层序遍历, 层次遍历, 二叉树
part1 问题描述
面对如下 XML 文件,写程序按层序遍历二叉树,要求打印 text 节点中的 text 属性,并按文法的形式展示。
1.1 二叉树举例如下图
1.2 对应的文法规则如下所示
Root ->Dir +AbMissHigh
Dir ->截至
AbMissHigh ->AbMissHigh +PartOf
AbMissHigh ->Num +Tut
PartOf ->Dt +Tut
Num ->21
Tut ->日
Dt ->上
Tut ->午
part 2 程序代码
import xml.dom.minidom
def getText(node):
return node.firstChild.getAttribute('text')
def getChilds(node):
nodes = list()
for child in node.childNodes:
if (child.nodeName == "syntacticstructure"):
nodes.append(child)
return nodes
if __name__ == "__main__":
DOMTree = xml.dom.minidom.parse("kim.xml")
sentence = DOMTree.documentElement
syntacticstructure = sentence.firstChild
q = list()
q.append(syntacticstructure)
while(len(q)):
node = q.pop(0)
for child in getChilds(node):
q.append(child)
if len(getChilds(node)):
print(getText(node) + "->", end="")
for i,child in enumerate(getChilds(node)):
if i == len(getChilds(node)) - 1:
print(getText(child))
else:
print(getText(child) + "+", end="")
附录
xml文件。文件名 kim.xml:
<sentence defaultfontsize="10" file="E:\Corpus\新建文件夹\3.9.xml" lefttranslate="20" linelength="10" mintextwidth="30" name="3.9.xml" toptranslate="20" type="TreeForm">
<syntacticstructure lineblue="0" linegreen="0" linered="0" string="664305241" syntacticlevel="HEAD">
<text text="Root ">
<N0 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N1 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N2 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N3 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N4 font="Dialog" size="10" style="0" subscript="normal"/>
</text>
<syntacticfeaturesets/>
<syntacticassociations/>
<starttraces/>
<endtraces/>
<syntacticstructure lineblue="0" linegreen="0" linered="0" string="1493421489" syntacticlevel="HEAD">
<text text="Dir ">
<N0 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N1 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N2 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N3 font="Dialog" size="10" style="0" subscript="normal"/>
</text>
<syntacticfeaturesets/>
<syntacticassociations/>
<starttraces/>
<endtraces/>
<syntacticstructure lineblue="0" linegreen="0" linered="0" string="1023074303" syntacticlevel="MORPH">
<text text="截至 ">
<N0 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N1 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N2 font="Dialog" size="10" style="0" subscript="normal"/>
</text>
<syntacticfeaturesets/>
<syntacticassociations/>
<starttraces/>
<endtraces/>
</syntacticstructure>
</syntacticstructure>
<syntacticstructure lineblue="0" linegreen="0" linered="0" string="896569587" syntacticlevel="HEAD">
<text text="AbMissHigh ">
<N0 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N1 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N2 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N3 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N4 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N5 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N6 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N7 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N8 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N9 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N10 font="Dialog" size="10" style="0" subscript="normal"/>
</text>
<syntacticfeaturesets/>
<syntacticassociations/>
<starttraces/>
<endtraces/>
<syntacticstructure lineblue="0" linegreen="0" linered="0" string="310221747" syntacticlevel="HEAD">
<text text="AbMissHigh ">
<N0 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N1 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N2 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N3 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N4 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N5 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N6 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N7 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N8 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N9 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N10 font="Dialog" size="10" style="0" subscript="normal"/>
</text>
<syntacticfeaturesets/>
<syntacticassociations/>
<starttraces/>
<endtraces/>
<syntacticstructure lineblue="0" linegreen="0" linered="0" string="1733952868" syntacticlevel="HEAD">
<text text="Num ">
<N0 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N1 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N2 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N3 font="Dialog" size="10" style="0" subscript="normal"/>
</text>
<syntacticfeaturesets/>
<syntacticassociations/>
<starttraces/>
<endtraces/>
<syntacticstructure lineblue="0" linegreen="0" linered="0" string="202389819" syntacticlevel="MORPH">
<text text="21 ">
<N0 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N1 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N2 font="Dialog" size="10" style="0" subscript="normal"/>
</text>
<syntacticfeaturesets/>
<syntacticassociations/>
<starttraces/>
<endtraces/>
</syntacticstructure>
</syntacticstructure>
<syntacticstructure lineblue="0" linegreen="0" linered="0" string="893470908" syntacticlevel="HEAD">
<text text="Tut ">
<N0 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N1 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N2 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N3 font="Dialog" size="10" style="0" subscript="normal"/>
</text>
<syntacticfeaturesets/>
<syntacticassociations/>
<starttraces/>
<endtraces/>
<syntacticstructure lineblue="0" linegreen="0" linered="0" string="515093806" syntacticlevel="MORPH">
<text text="日 ">
<N0 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N1 font="Dialog" size="10" style="0" subscript="normal"/>
</text>
<syntacticfeaturesets/>
<syntacticassociations/>
<starttraces/>
<endtraces/>
</syntacticstructure>
</syntacticstructure>
</syntacticstructure>
<syntacticstructure lineblue="0" linegreen="0" linered="0" string="1588368286" syntacticlevel="HEAD">
<text text="PartOf ">
<N0 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N1 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N2 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N3 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N4 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N5 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N6 font="Dialog" size="10" style="0" subscript="normal"/>
</text>
<syntacticfeaturesets/>
<syntacticassociations/>
<starttraces/>
<endtraces/>
<syntacticstructure lineblue="0" linegreen="0" linered="0" string="670131188" syntacticlevel="HEAD">
<text text="Dt ">
<N0 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N1 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N2 font="Dialog" size="10" style="0" subscript="normal"/>
</text>
<syntacticfeaturesets/>
<syntacticassociations/>
<starttraces/>
<endtraces/>
<syntacticstructure lineblue="0" linegreen="0" linered="0" string="1617277174" syntacticlevel="MORPH">
<text text="上 ">
<N0 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N1 font="Dialog" size="10" style="0" subscript="normal"/>
</text>
<syntacticfeaturesets/>
<syntacticassociations/>
<starttraces/>
<endtraces/>
</syntacticstructure>
</syntacticstructure>
<syntacticstructure lineblue="0" linegreen="0" linered="0" string="141113787" syntacticlevel="HEAD">
<text text="Tut ">
<N0 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N1 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N2 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N3 font="Dialog" size="10" style="0" subscript="normal"/>
</text>
<syntacticfeaturesets/>
<syntacticassociations/>
<starttraces/>
<endtraces/>
<syntacticstructure lineblue="0" linegreen="0" linered="0" string="420497037" syntacticlevel="MORPH">
<text text="午 ">
<N0 backgroundblue="255" backgroundgreen="255" backgroundred="255" font="Dialog" foregroundblue="0" foregroundgreen="0" foregroundred="0" size="10" strikethrough="false" style="0" subscript="normal" underline="false"/>
<N1 font="Dialog" size="10" style="0" subscript="normal"/>
</text>
<syntacticfeaturesets/>
<syntacticassociations/>
<starttraces/>
<endtraces/>
</syntacticstructure>
</syntacticstructure>
</syntacticstructure>
</syntacticstructure>
</syntacticstructure>
</sentence>
Python3 解析XML 层序遍历二叉树的更多相关文章
- leetcode-102.层序遍历二叉树(正序)· BTree
题面 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to rig ...
- LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树
Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...
- python3解析XML文件
软硬件环境 Ubuntu 15.10 32bit Python 3.5.1 PyQt 5.5.1 前言 Python解析XML的方法挺多,本文主要是利用ElementTree来完成. 实例讲解 解析X ...
- 层序遍历二叉树 完整层序重建二叉树 python
给定一个二叉树的完整的层次遍历序列(包含所有节点,包括空节点),利用这个序列生成一颗二叉树. 我们首先来看怎样对一颗二叉树进行层序遍历,下图所示的二叉树层次遍历的结果为[a,b,c,d,e],在这个过 ...
- python3 解析xml
转载:http://www.jb51.net/article/79494.htm 这篇文章主要为大家详细介绍了深入解读Python解析XML的几种方式,以ElementTree模块为例,演示具体使用方 ...
- UVa 122 Trees on the level (动态建树 && 层序遍历二叉树)
题意 :输入一棵二叉树,你的任务是按从上到下.从左到右的顺序输出各个结点的值.每个结 点都按照从根结点到它的移动序列给出(L表示左,R表示右).在输入中,每个结点的左 括号和右括号之间没有空格,相邻 ...
- Binary Tree Level Order Traversal,层序遍历二叉树,每层作为list,最后返回List<list>
问题描述: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ...
- Binary Tree Zigzag Level Order Traversal (LeetCode) 层序遍历二叉树
题目描述: Binary Tree Zigzag Level Order Traversal AC Rate: 399/1474 My Submissions Given a binary tree, ...
- [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
随机推荐
- nodejs+mysql入门实例(链接到数据库)
//连接数据库 var mysql = require('mysql'); var connection = mysql.createConnection({ host: '******', //数据 ...
- TensorFlow读取CSV数据
代码来源于官方文档,做了一些小小的调整: # -*- coding:utf-8 -*- import tensorflow as tf filename_queue = tf.train.string ...
- MD5、SHA1加密java 16位32位
MD5.SHA1加密java 16位32位 import java.math.BigInteger; import java.security.MessageDigest; public class ...
- shell基础:通配符和其他特殊符号
这些东西可以用来批量删除:用× 通配符 其实就是基本用来匹配文件名
- python+selenium入门
from selenium import webdriver打开浏览器 driver = webdriver.Chrome() 打开网页 driver.get("http://www.bai ...
- 例子:动能并不是特别强(2-3)后,下M5的同时,也是恢复期到期的前一天
动能并不是特别强(2-3)后,下M5的同时,但是恢复期到期 EG.002195 2017/06/23-->2017/06/29
- 简易C# socket
服务器 using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Thread ...
- Javascript-全局函数和局部函数作用域的理解
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Python之装饰器的实例
1.1装饰器的应用:参数类型检查 函数参数的检查,一定是在函数外 函数应该作为参数,传入到检查函数中 检查函数拿到函数传入的实际参数,与形参声明对比 __annotations__属性是一个字典,其中 ...
- Python全栈-day10-函数2
函数高级篇 1.函数嵌套 1)嵌套定义 在函数内定义另外一个函数 def func(): print('嵌套定义') def func1(): print('这是一个嵌套函数') def func2( ...