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, ...
随机推荐
- Go win32
先说的让人兴奋的吧,“GO的库开发在windwos上尽然是一摸一样的啊” C:\go-pro\go-self-package>go build gin-main.go 2017/09/20 14 ...
- django项目mysql中文编码问题
在做django+mysql项目的时候,遇到中文报错问题. 问题分析:是由于mysql数据库,字符集的问题 在cmd命令行模式进入mysql mysql -uroot -p以root身份进入mysql ...
- 从0开始搭建vue+webpack脚手架(二)
接着从0开始搭建vue+webpack脚手架(一) 三.配置webpack-dev-server 1. webpack-dev-server自带一个node的服务器, 项目在服务端运行的同时可以实现热 ...
- 软工网络15团队作业4——Alpha阶段敏捷冲刺7.0
1.每天举行站立式会议,提供当天站立式会议照片一张. 2.项目每个成员的昨天进展.存在问题.今天安排. 成员 昨天已完成 今天计划完成 郭炜埕 实现前端各界面的跳转连接 学习后端相关知识 郑晓丽 完善 ...
- PKCS#1
ASN.1 syntax,octet string是一个8 bytes sequence string. RSA中涉及到的Data conversion: 1)I2OSP,Integer to Oct ...
- FileInputstream,FileOutputstream 和 byteArrayInputStream,byteArrayOutputStream
你知道FileInputstream和FileOutputstream吗?FileInputstream,FileOutputstream分别是由抽象类Inputstream和Outputstream ...
- win10 在任务栏添加 desktop 快捷图标
http://www.xitongcheng.com/jiaocheng/win10_article_11980.html [Shell] Command= IconFile=explorer.exe ...
- kali 开启ssh服务
1. 一.配置SSH参数 修改sshd_config文件,命令为: vi /etc/ssh/sshd_config 将#PasswordAuthentication no的注释去掉,并且将NO修 ...
- JSVC技术
如果我们的某个项目时web项目,我们很容易就可以放置在Tomcat中进行启动. 可是如果我们的项目不是web项目,我们又需要在单独启动时,我们又应该怎么办呢? 引出了我们今天的主人公:JSVC ...
- 计算概论(A)/基础编程练习2(8题)/6:数组逆序重放
#include<stdio.h> int main() { // 输入n个整数 ; scanf("%d", &n); // 循环读入元素 while(scan ...