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, ...
随机推荐
- php二维数组搜索
$ar = array( 2 => array( 'catid' => 2, 'catdir' => 'notice', ), ...
- js 检查文件格式和文件大小
之前有个工作需要用到js检查文件大小和文件格式,网上查了下有个兄弟写的不错,拿过来就能直接用,感谢他顺便记录下.原始文章地址http://www.jb51.net/article/43498.htm ...
- gitlab RPM卸载 & 安装 && 升级(9.0.13-》9.5.9-》10.0->10.3.9->10.6.6-》10.8-》11.0)
版本:9.0.3 升级版本:9.0.13 一,停止服务 gitlab-ctl stop unicorn gitlab-ctl stop sidekiq gitlab-ctl stop nginx 二, ...
- Linux(Centos)服务器配置node项目
以阿里云服务器,CentOS系统为例 上一节已经提到怎么安装nodejs,以下是以vue项目为例 步骤: (1)首先安装vue脚手架@vue/cli, 官网参考 vue-cli3.x [root@lu ...
- AI案列 三条路
在生成点生成带有颜色的小球,小球走对应颜色的路 先做好三个小球,做成预制体 对应颜色的小球,除了自身颜色,其他颜色不要选 新建脚本: using System.Collections; using S ...
- linux整理
文件查看命令 cat [OPTION]... [FILE]... - E: 显示行结束符$ -n: 对显示出的每一行进行编号 -A:显示所有控制符 -b:非空行编号 -s:压缩连续的空行成一行 he ...
- Yii2 数据缓存/片段缓存/页面缓存/Http缓存
- Windows 下VC++6.0制作、使用动态库和静态库
Windows 下VC++6.0制作.使用动态库和静态库 一.VC++6.0制作.使用静态库 静态库制作 1.如图一在VC++6.0中new一个的为win32 static library工程并新建一 ...
- 以太坊客户端Ethereum Wallet与Geth区别简介
以太坊客户端Ethereum Wallet与Geth区别简介 最近有不少朋友在搭建交易平台,在咨询和技术交流的过程中发现很多朋友不太清楚Ethereum Wallet和Geth区别.甚至有朋友使用Ge ...
- windows.onload和body的onload属性的区别
关于windows.onload和body的onload属性的区别网上有些说法说的也不太统一,现在系统说下: 先看共同点: 都是body内容体加载结束执行: window.onload 内部方式可以 ...