2019独角兽企业重金招聘Python工程师标准>>>

XML文件为嵌套结构,分析XML文件并提取关键字和数据的过程分为两个部分:
1. 字符串处理,这一部分为编译原理的一些基础知识的实现,状态机什么的,就是把字符流处理为带关键字的多个节点
2. 将节点建立成树,将树用深度或者广度遍历显示,其中深度遍历将为树形结构,广度遍历成层次结构 
    如数据为int source[] = {4,3,5,6,-6,-5,-3,2,-2,-4};,对应的XML结构应该 为 <4><3><5><6></6></5>< /3><2></2></4>,深度遍历后应该为
4
  3
    5
      6
  2
广度遍历后应该为
4
3 2
5 6

用C++实现上面的第二部分的源码如下:
数据 节点结构包括双亲指针,第一个孩子的指针和右兄弟的指针

#include <stdio.h>
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <assert.h>
#include <ctype.h>
#include <malloc.h>
#include <math.h>
#include <string.h>
//#include <stdlib.h>
//#include <cstring>

const int MAXLENGTH = 100;

typedef struct tagNode
{
 int data;
 struct tagNode *pLeftChild;
 struct tagNode *pRightBrother;
 struct tagNode *pParent;
}*pNode,Node;

typedef struct Queue
{
 pNode baseData[MAXLENGTH];
 int front;
 int rear; 
}Queue;

bool InitQueue(Queue &queueNodes);//初始化队列
bool InQueue(Queue &queueNodes,pNode data);//入队列
bool OutQueque(Queue &queueNodes,pNode &data);//出队列
int PrintLength(Queue queueNodes);//返回队列长度
pNode initNodes(pNode pInsertNode,int data);//返回下一个要插入的位置
void printNodesByDepth(pNode root,int height);//深度遍历
void printNodesByWidth(pNode root);//广度遍历

int PrintLength(Queue queueNodes)
{
 if (queueNodes.rear < queueNodes.front)
  return queueNodes.rear + MAXLENGTH - queueNodes.front;
 else
  return queueNodes.rear - queueNodes.front;
}

bool InitQueue(Queue &queueNodes)//初始化队列
{
 queueNodes.front = 0;
 queueNodes.rear = 0;
 return true;
}
bool InQueue(Queue &queueNodes,pNode data)//入队列
{
 if ((queueNodes.rear+1) % MAXLENGTH == queueNodes.front)
 {
  cout<<"Queue is full!";
  return false;
 }
 else
 {
  queueNodes.baseData[queueNodes.rear] = data;
  queueNodes.rear = (queueNodes.rear+1) % MAXLENGTH;
  return true;
 }

}
bool OutQueque(Queue &queueNodes,pNode &data)//出队列
{
 if (queueNodes.front == queueNodes.rear)
 {
  cout<<"Queue is empty!";
  return false;
 }
 else
 {
  data = queueNodes.baseData[queueNodes.front];
  queueNodes.front = (queueNodes.front + 1) % MAXLENGTH;
  return true;
 }
}
队列操作完成//
pNode initNodes(pNode pInsertNode,int data)//返回下一个要插入的位置
{
 if (data>0)
 {
  pNode pNewNode = new Node;
  pNewNode->data = data;
  pNewNode->pParent = pInsertNode;
  pNewNode->pLeftChild = NULL;
  pNewNode->pRightBrother = NULL;

pNode p = pInsertNode;//游标

if (p->pLeftChild == NULL)//插入点没有左孩子,新节点作为左孩子
  {
   p->pLeftChild = pNewNode;
  }
  else//有左孩子,插入到最后一个孩子节点的右边
  { 
   p = pInsertNode->pLeftChild;
   while (p->pRightBrother!=NULL)
   {
    p = p->pRightBrother;
   }
   p->pRightBrother = pNewNode;
  }
  return pNewNode;//返回下一个插入点指针
 }
 else//小于零返回该节点的母节点
 {
  return pInsertNode->pParent;
 } 
}
void printNodesByDepth(pNode root,int height)//深度遍历
{
 if (root != NULL)
 {
  if (0 == height)
  {
   cout<<root->data<<endl;
  }
  else
  {
   cout<<setw(height)<<"+"<<root->data<<endl;
  }
  
  printNodesByDepth(root->pLeftChild,height+1);
  printNodesByDepth(root->pRightBrother,height);
 } 
}
void printNodesByWidth(pNode root)//广度遍历
{
 Queue myQueue;
 pNode pTemp = NULL;
 InitQueue(myQueue);//初始化队列
 InQueue(myQueue,root);// 根节点入队列

while (myQueue.front != myQueue.rear)//将队首出队列,并将其孩子链插入队尾
 {
  OutQueque(myQueue,pTemp);
  cout<<"current length:"<<PrintLength(myQueue)<<endl;
  cout<<pTemp->data<<" ";
  if (pTemp->pLeftChild != NULL)
  {
   pTemp = pTemp->pLeftChild;

InQueue(myQueue,pTemp);
  
   pTemp = pTemp->pRightBrother;

while(pTemp != NULL)
   {
    InQueue(myQueue,pTemp);
    pTemp = pTemp->pRightBrother;
   }
  }
 }

}
void main()
{
 int source[] = {4,11,-11,3,2,-2,8,-8,19,-19,-3,5,6,-6,7,-7,10,-10,-5,-4};
// int source[] = {4,3,5,6,-6,-5,-3,2,-2,-4};
 int lenth = sizeof(source)/sizeof(int);
 pNode root = new Node;
 root->pLeftChild = NULL;
 root->pRightBrother = NULL;
 root->pParent = NULL;
 root->data = source[0];
 pNode InsertNode = root;

for (int i = 1; i<lenth; i++)
 {
  InsertNode = initNodes(InsertNode,source[i]);
 }
// printNodesByDepth(root,0); //深度遍历
 printNodesByWidth(root);//广度遍历

}

转载于:https://my.oschina.net/duluo180/blog/7655

C++读取XML树的建立和遍历相关推荐

  1. C++实现树的建立,查找,遍历输出

    C++实现树的建立,查找,遍历输出: #include<iostream> #include<stack> using namespace std; template<c ...

  2. jaxp与dom4j遍历xml树

    1.jaxp遍历xml树 import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFacto ...

  3. 树和森林的遍历 (树的建立和输出)

    树和森林的遍历 对于树的遍历从其结构出发有三种搜索路径:先根(次序)遍历树,先访问根结点,然后依次先根遍历根的各棵子树:后根(次序)遍历树,先依次后根遍历根的各棵子树,然后访问根结点:按层(次序)遍历 ...

  4. 【Qt】DOM读取XML文档

    00. 目录 文章目录 00. 目录 01. 概述 02. 开发环境 03. XML文档示例 04. DOM读取XML文档内容 05. 预留 06. 附录 01. 概述 DOM(Document Ob ...

  5. C#中常用的几种读取XML文件的方法

    XML文件是一种常用的文件格式,例如WinForm里面的app.config以及Web程序中的web.config文件,还有许多重要的场所都有它的身影.Xml是Internet环境中跨平台的,依赖于内 ...

  6. Qt4_使用QXmlStreamReader读取XML

    使用QXmlStreamReader读取XML 使用 QXmlStreamReader,是在 Qt 中读取 XML 文档的最快且最简单的方式.因为解析器的工作能力是逐渐递增的,所以它尤其适用于诸如查找 ...

  7. 赫夫曼树建立c语言源程序编译结果详细解释,哈夫曼树的建立与实现最终版(备份存档)...

    <哈夫曼树的建立与实现.doc>由会员分享,可免费在线阅读全文,更多与<哈夫曼树的建立与实现(最终版)>相关文档资源请在帮帮文库(www.woc88.com)数亿文档库存里搜索 ...

  8. [Qt教程] 第27篇 XML(一)使用DOM读取XML文档

    [Qt教程] 第27篇 XML(一)使用DOM读取XML文档 楼主  发表于 2013-5-21 21:14:28 | 查看: 1001| 回复: 14 使用DOM读取XML文档 版权声明 该文章原创 ...

  9. 使用.NET读取XML文件

    介绍 本文中我将介绍在ASP.NET应用程序中如何读取XML文件,这是一个十分有用的技巧.使用这个技巧,我们能够定制我们的应用程序的配置文件,也可以读取那些保存在XML文件中的数据. 概论 下面的代码 ...

最新文章

  1. DDoS***、CC***的***方式和防御方法
  2. Raspberry 4B安装wiringPi库 和 python-dev包
  3. querySelector
  4. 重装系统后sqlserver安装失败_Windows 10八月更新再遇尴尬:安装失败 或安装后随机重启...
  5. JSR 310新日期/时间API的自定义JSR 303 Bean验证约束
  6. 8. Action过滤
  7. 在网络上提供资源的计算机,在计算机网络中通常把提供并管理共享资源的计算机称为...
  8. 开发者如何在一周从入门级到专家级别的修炼
  9. 企业权限管理系统之AdminLTE的基本介绍(一)
  10. 大数据处理的五大关键技术及其应用
  11. Diss 暴雪爸爸,炉石是否还是“良心”游戏?
  12. python爬取微信运动_如何利用Python爬取微信运动中各个好友的运动信息
  13. 解决 Error creating bean with name ‘dataSource‘ defined in class path resource 问题
  14. WebRTC 教程四: WebRTC聊天室设计和搭建
  15. 网易邮箱支持手机收发邮件的服务器,手机如何使用IMAP服务收发网易的邮件
  16. java的幂运算_java中幂指数值的运算
  17. Hadoop:HDFS读写流程
  18. 罗克韦尔CompactLogix 控制系统PLC如何借助工业网关实现远程编程维护?
  19. 中国黑客群体的真实收入
  20. react native 实战系列教程之热更新原理分析与实现

热门文章

  1. 赛前集训前的总结(警醒)
  2. 赢得 Docker 挑战最佳实践
  3. iOS项目开发实战——使用Xcode6设计自己定义控件与图形
  4. 《嵌入式系统开发之道——菜鸟成长日志与项目经理的私房菜》——01-05 基本职能:老鸟也曾是菜鸟...
  5. Redis内存缓存系统入门
  6. 100%有用的Photoshop实用快捷健介绍
  7. CSS3 之 flex
  8. 考虑用静态工厂方法代替构造器
  9. c# lock的使用及注意事项
  10. 零售分析用vba还是python_数据分析?Excel、VBA和Python?营销套路还是大势所趋!...