Tree 使用方式
Traditional Ways of Tree Traversal
This page contains examples of some “standard” traversal algorithms (ones that can be found in most textbooks). All examples perform pre-order tree traversal on a general rooted tree. “Algorithms, Data Structures and Problem Solving with C++” by Mark Allen Weiss (Addison-Wesley, 1995) gives the following definition of the general rooted tree:
- One node is distinguished as a root.
- Every node c, except the root, is connected by an edge from exactly one other node p. p is the parent and c is one of p‘s children.
- There is a unique path from the root to each node. The number of edges that must follow is the path length (sometimes it is called “depth”).
Binary trees and their variations (AVL, red-black and so forth) are not considered here.
Each example performs full traversal of a DOM tree and prints name and value of each node. An XML parser, for example Apache’s Xerces, is needed in order to run the code.
Example 1. Traversal using recursion
Recursive traversal is the best known and most frequently used. Recursive algorithm uses method call stack in order to keep the state of the traversal for every level of a tree. There is a common misconception that recursive algorithms are slow because of the call stack copying overhead. I have not found it to be the case in Java, at least it is not the case for methods with small number of local variables.
import org.w3c.dom.*;
public class RecursiveTraversal implements ITraversal {
/**
* Performs full tree traversal using recursion.
*/
public void traverse( Node parentNode ) {
// traverse all nodes that belong to the parent
for(Node node=parentNode.getFirstChild(); node!=null; node=node.getNextSibling()
) {
// print node information
System.out.println( node.getNodeName()+"="+node.getNodeValue());
// traverse children
traverse(node);
}
}
}
Example 2. Traversal using stack
A stack object is used to store tree level’s state thus eliminating the need for recursion.
Note that in reality you don’t want to use java.util.Stack because its methods are synchronized. It also inherits from Vector and its methods are synchronized as well. So some sort of custom stack class (for example, based on java.util.ArrayList) should be used instead.
import org.w3c.dom.*;
import java.util.*;
public class StackTraversal implements ITraversal {
/**
* Performs full tree traversal using stack.
*/
public void traverse( Node rootNode ) {
Stack stack = new Stack();
// ignore root -- root acts as a container
Node node=rootNode.getFirstChild();
while (node!=null) {
// print node information
System.out.println( node.getNodeName()+"="+node.getNodeValue());
if ( node.hasChildNodes()) {
// store next sibling in the stack. We return to it after all children are
processed.
if (node.getNextSibling()!=null)
stack.push( node.getNextSibling() );
node = node.getFirstChild();
}
else {
node = node.getNextSibling();
if (node==null && !stack.isEmpty())
// return to the parent's level.
// note that some levels can be skipped if the parent's node was the last one.
node=(Node) stack.pop();
}
}
}
}
Example 3. Traversal using child-parent link
It is possible to avoid using stack for treelike structures that provide support for child-parent link. Link from child to parent can be used to return back to the parent level once the child level is processed. This link effectively simulates stack, so there is no need for a separate stack object. Most of the tree types (including DOM) do support child-parent link. This is probably the most elegant way of traversing a tree — no recursion or stack is involved.
import org.w3c.dom.*;
public class LinkTraversal implements ITraversal {
/**
* Performs full tree traversal using child-parent link.
*/
public void traverse( Node rootNode ) {
// ignore root -- root acts as a container
Node node=rootNode.getFirstChild();
while (node!=null) {
// print node information
System.out.println( node.getNodeName()+"="+node.getNodeValue());
if ( node.hasChildNodes()) {
node = node.getFirstChild();
}
else { // leaf
// find the parent level
while (node.getNextSibling()==null && node != rootNode)
// use child-parent link to get to the parent level
node=node.getParentNode();
node = node.getNextSibling();
}
}
}
}
Tree 使用方式的更多相关文章
- EasyUI Tree递归方式获取JSON
最近需要用到EASYUI中的TREE功能,以前我是直接拼接成<UL><LI>发现这样拼完之后在更改树后对树的刷新不是很理想,现改用JSON格式,首先分析TREE中JOSN格式如 ...
- 数据结构 - Codeforces Round #353 (Div. 2) D. Tree Construction
Tree Construction Problem's Link ------------------------------------------------------------------- ...
- Device Tree(三):代码分析【转】
转自:http://www.wowotech.net/linux_kenrel/dt-code-analysis.html Device Tree(三):代码分析 作者:linuxer 发布于:201 ...
- Device Tree(三):代码分析
一.前言 Device Tree总共有三篇,分别是: 1.为何要引入Device Tree,这个机制是用来解决什么问题的?(请参考引入Device Tree的原因) 2.Device Tree的基础概 ...
- 【转】Device Tree(三):代码分析
原文网址:http://www.wowotech.net/linux_kenrel/dt-code-analysis.html 一.前言 Device Tree总共有三篇,分别是: 1.为何要引入De ...
- XGBoost 与 Boosted Tree
http://www.52cs.org/?p=429 作者:陈天奇,毕业于上海交通大学ACM班,现就读于华盛顿大学,从事大规模机器学习研究. 注解:truth4sex 编者按:本文是对开源xgboo ...
- 转 Velocity中加载vm文件的三种方式
Velocity中加载vm文件的三种方式 velocitypropertiespath Velocity中加载vm文件的三种方式: 方式一:加载classpath目录下的vm文件 Prope ...
- orocos_kdl学习(二):KDL Tree与机器人运动学
KDL(Kinematics and Dynamics Library)中定义了一个树来代表机器人的运动学和动力学参数,ROS中的kdl_parser提供了工具能将机器人描述文件URDF转换为KDL ...
- Boosted Tree
原文:http://www.52cs.org/?p=429 作者:陈天奇,毕业于上海交通大学ACM班,现就读于华盛顿大学,从事大规模机器学习研究. 注解:truth4sex 编者按:本文是对开源xg ...
随机推荐
- 菜鸟做HTML5小游戏 - 刮刮乐
继上篇翻翻乐之后,又来刮刮乐.还是先上效果图: 开始demo的世界: 1.css去绘制界面效果.(源码提供) 2.原理:要实现刮刮卡内容的出现,我用div做了背景层去显示刮出的内容结果[重点].中间C ...
- HTML&CSS基础学习笔记1.18-表格的边框
今天的内容比较简单~来看看HTML里表格的边框是怎么设置的吧 表格的边框 前面为了方便观察表格单元格的情况,我们给<table>加了border属性.<table>的borde ...
- 深入浅出scanf、getcha、gets、cin函数
转:问题描述一:(分析scanf()和getchar()读取字符) scanf(), getchar()等都是标准输入函数,一般人都会觉得这几个函数非常简单,没什么特殊的.但是有时候却就是因为使用这些 ...
- WebVR
WebVR 主要面向Web前端工程师,需要一定Javascript及three.js基础:本文主要分享内容为基于three.js开发WebVR思路及碰到的问题:有兴趣的同学,欢迎跟帖讨论. 目录:一. ...
- 更新项目经常使用的Linux命令
在公司经常在服务器上更新项目,总结了自己经常使用的命令: 1.删除:rm -rf 文件名2.复制:copy 文件 目标地址3.压缩:zip -r 压缩后文件名 被压缩目录4.移动:move 文件 目标 ...
- 较优H圈matlab实现
大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang %解决完备图中的较优H圈 clc clear w = [ inf 6 1 8 3 1 ;... ...
- Xcode本地文件 提交svn 的明细
XXXXX.xcscmblueprint 这个文件不用提交svn
- UART与USART的区别
UART与USART都是单片机上的串口通信,他们之间的区别如下: 首先从名字上看: UART:universal asynchronous receiver and transmitter通用异步收/ ...
- QT树莓派交叉编译环开发环境搭建(附多个exe工具下载链接)
前两天入手了一块2.8’的tft液晶显示屏,于是和树莓派连了一发,成功将命令行显示在了这块小的可怜的屏幕上之后,觉得这屏幕就显示个黑白内容太浪费了,于是考虑开发一个”脸”(图形用户界面,GUI).首先 ...
- PowerShell 简单模式识别 1
PowerShell 简单模式识别 1 10 6月, 2013 在 Powershell tagged 字符串 / 文本 / 通配符 by Mooser Lee 在验证用户的条目时,模式识别是必要并 ...