当前位置: 首页 > 编程日记 > 正文

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 pp 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 parentfor(Node node=parentNode.getFirstChild(); node!=null; node=node.getNextSibling()) {// print node informationSystem.out.println( node.getNodeName()+"="+node.getNodeValue());// traverse childrentraverse(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 containerNode node=rootNode.getFirstChild();while (node!=null) {// print node informationSystem.out.println( node.getNodeName()+"="+node.getNodeValue());if ( node.hasChildNodes()) {// store next sibling in the stack. We return to it after all children areprocessed.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 containerNode node=rootNode.getFirstChild();while (node!=null) {// print node informationSystem.out.println( node.getNodeName()+"="+node.getNodeValue());if ( node.hasChildNodes()) {node = node.getFirstChild();}else {    // leaf// find the parent levelwhile (node.getNextSibling()==null && node != rootNode)// use child-parent link to get to the parent levelnode=node.getParentNode();node = node.getNextSibling();}}}
}

转载于:https://www.cnblogs.com/pyfreshman/p/4935122.html

相关文章:

网站更换服务器ip地教程,由于服务器更换IP地址,服务器不更换。需要如何操作使网站正常运行呢?,POSCMS,CodeIgniter技术文档,PHP开发文档,迅睿CMS框架官方教程...

多文件Files内容详情中(show.html) 模板中调用方法是:{loop $字段名 $i $c} 序号: {$i} 标题:{$c.title} 描述:{$c.description} 文件原始地址:{dr_get_file($c.file)} 文件的下载地址:{dr_down_file($c.file)} 图片缩…

3ds Max V-Ray5 完整指南大师班视频教程

3ds Max V-Ray5 完整指南大师班视频教程 时长15小时 包括项目文件 1920X1080 MP4 语言:英语中文字幕(机译) 标题:Gumroad–V-Ray 5 Masterclass:您的3ds Max V-Ray完整指南 大小:41.8G 共八大章 88小节课程 信息&am…

2022-2028年中国氢化丁晴橡胶行业市场研究及前瞻分析报告

【报告类型】产业研究 【报告价格】4500起 【出版时间】即时更新(交付时间约3个工作日) 【发布机构】智研瞻产业研究院 【报告格式】PDF版 本报告介绍了中国氢化丁晴橡胶电商行业市场行业相关概述、中国氢化丁晴橡胶电商行业市场行业运行环境、分析…

配置Exchange OWA和Sharepoint网站单点登录

配置Exchange OWA和Sharepoint网站单点登录如果我们在组织中已经部署完成了Lync、Exchange以及Sharepoint,那么我们会发现这三套系统在通过域账户登录计算机时,如果本机有安装Outlook和Lync,那么在登录Lync或启动Outlook的时候就会自动使用当…

BigTable

转载于:https://www.cnblogs.com/fanweisheng/p/11250529.html

Blender2.9全流程创建逼真未来科幻蝙蝠汽车视频教程

Blender2.9全流程创建逼真未来科幻蝙蝠汽车视频教程 MP4 |视频:h264,1280720 |音频:AAC,44.1 KHz,2通道 含课程工程素材 体裁:在线学习|语言:英语中文字幕(根据原英文字幕机译更准…

微服务项目用了几台服务器,微服务部署运维

docker介绍,及作用就是类似VM虚拟机一样的虚拟容器技术。docker 可以帮我们把所需要的应用打包容器, 每一个容器都相互独立的,而且容器占用内存小,启动和管理的速度非常快。比如 之前我们使用linux 虚拟机,如果要用mys…

JAVA用最简单的方法来构建一个高可用的服务端,提升系统可用性

一、什么是提升系统的高可用性 JAVA服务端,顾名思义就是23体验网为用户提供服务的。停工时间,就是不能向用户提供服务的时间。高可用,就是系统具有高度可用性,尽量减少停工时间。如何用最简单的方法来搭建一个高效率可用的服务端…

jQuery学习笔记一

2019独角兽企业重金招聘Python工程师标准>>> 1、使用attr()方法控制元素的属性 attr()方法的作用是设置或者返回元素的属性,其中attr(属性名)格式是获取元素属性名的值,attr(属性名,属性值)格式则是设置元素属性名的值。 2、操作元…

关于60枚一分两分五分硬币凑成一块钱的解决方法

关于60枚一分两分五分硬币凑成一块钱的解决方法 一.强行三重for循环 #include<stdio.h> int main() {int a, b, c;for (a 1; a < 60; a){for (b 1; b < 60; b){for (c 1; c < 60; c){if (100 5 * c 2 * b a && 60 a b c){printf("%d %d…

hbase RPCServer源码分析

前置知识&#xff1a; java&#xff0c;nio&#xff0c;多线程 看了几天的源码&#xff0c;写一些自己心得&#xff0c;若有错误请指出。 RPCServer的作用&#xff1a;负责创建listener&#xff0c;reader&#xff0c;responser&#xff0c;handler来处理client端的请求。 RPCS…

Maya创建科幻3D动画循环场景视频教程

Maya创建科幻3D动画循环场景视频教程 Skillshare – Create a Sci-Fi 3D Animation Loop in Autodesk Maya 持续时间3h 27m 包括项目文件 1280X720 Mkv 语言&#xff1a;英语中文字幕&#xff08;根据原英文字幕机译更准确&#xff09;原英文字幕 大小解压后 3.05G 共20小节课…

2022-2028年中国氢化丁腈橡胶行业市场深度分析及投资规模预测报告

【报告类型】产业研究 【报告价格】4500起 【出版时间】即时更新&#xff08;交付时间约3个工作日&#xff09; 【发布机构】智研瞻产业研究院 【报告格式】PDF版 本报告介绍了中国氢化丁腈橡胶行业市场行业相关概述、中国氢化丁腈橡胶行业市场行业运行环境、分析了中国氢…

js中的json对象和字符串之间的转化

字符串转对象(strJSON代表json字符串) var obj eval(strJSON); var obj strJSON.parseJSON(); var obj JSON.parse(strJSON)&#xff1b; json对象转字符串(obj代表json对象) var str obj.toJSONString(); var str JSON.stringify(obj) 运用时候需要除了eval()以外需要jso…

三种方式实现分布式锁

通过以上过程你可以发现锁的获取是按照创建时间来的,谁先来争取锁谁就先获得锁,因此它实现的是公平锁。答案是不能,以Synchronized关键字为例,Synchronized关键字无论是在偏向锁、轻量级锁还是重量级锁状态都不能实现这点,如重量级锁,重量级锁是靠系统底层的互斥量Mutex实现的,也就是说每个节点(服务器)所使用的互斥量是分开的,节点A的互斥量是无法锁住节点B的线程访问临界区,因此Synchronized关键字只能保证单服务器内的JVM进程的不同线程同步,是不能用做分布式环境中来保证线程同步。

超级挂载 实现过程-代码

公司产品是给机顶盒或者电视做的&#xff0c;玩的就是大型游戏&#xff0c;一个游戏常常能有G级别的数据包&#xff0c;于是产生一个需求&#xff0c;将游戏的数据包存放在外置sdcard上&#xff0c;用户差一个32G的卡能随便玩游戏&#xff0c;不占用设备自身的存储容量&#xf…

synchronized对象锁与类锁用法&如何用synchronized锁字符串对象,这里面的坑要注意

我们使用synchronized通常都有这样一个误区:synchronized锁住的代码块一定是所有线程都互斥的。其实不然!首先我们明确一点,synchronized锁住的是一个对象!如果锁住的这个对象,在多个线程中相同,那么这些线程访问synchronized修饰的代码块时,总是互斥的。但是!如果锁住的这个对象,在多个线程中是不同的,那么这些线程访问synchronized修饰的代码块,是不会互斥的!

浅谈Java分布式与集群

在日常操作中,相信很多人在怎么理解Java分布式与集群问题上存在疑惑,今天就大概说说,不注意听,觉得两个可能是同一个东西,其实这个是两个概念。一句话概括:分布式是以缩短单个任务的执行时间来提升效率的,而集群则是通过提高单位时间内执行的任务数来提升效率。

550种Blender风格化笔刷素材

550种Blender风格化笔刷素材 550Blender刷风格化版(包括4K阿尔法) 大小解压后&#xff1a;3G 信息: 一个伟大的自定义风格化的刷子使用Blender收集。Alphas包含在其他软件中使用。 ArtStation – [MEGAPACK] 550 Blender Brushes Stylized Edition (4K Alphas Included) Bl…

synchronized用法详解

synchronized 是 Java 中的关键字,是一种同步锁。主要应用于多线程环境下保证线程的安全性。A. 无论synchronized关键字加在方法上还是对象上,如果它作用的对象是非静态的,则它取得的锁是对象;如果synchronized作用的对象是一个静态方法或一个类,则它取得的锁是对类,该类所有的对象同一把锁。B. 每个对象只有一个锁(lock)与之相关联,谁拿到这个锁谁就可以运行它所控制的那段代码。C. 实现同步是要很大的系统开销作为代价的,甚至可能造成死锁,所以尽量避免无谓的同步控制。

POJ 3723

最大生成树 #include<iostream> #include<cstdio> #include<cstring> #include<set> #include<algorithm> #include<stack> #include<map> #include<queue> #include<list> #include<vector> using namespace std…

有关Run-Time Check Failure #2 - Stack around the variable 'XXX' was corrupted.错误的解决方法

有关Run-Time Check Failure #2 - Stack around the variable ‘XXX’ was corrupted.错误的解决方法 今天我在敲完一段代码运行的时候出现一个错误&#xff0c;如下&#xff1a; Run-Time Check Failure #2 - Stack around the variable ‘filename’ was corrupted. 大概意…

Nginx搭建负载均衡集群

(1).实验环境 youxi1 192.168.5.101 负载均衡器 youxi2 192.168.5.102 主机1 youxi3 192.168.5.103 主机2 (2).Nginx负载均衡策略 nginx的负载均衡用于upstream模板定义的后端服务器列表中选取一台服务器接收用户的请求。一个基本的upstream模块如下&…

如何卸载iPhone模拟器中的自己创建的程序

当你使用iPhone模拟器测试过很多程序以后&#xff0c;模拟器中放置了大量无用的程序。 一直在找如何清除这些程序&#xff0c;其实后来发现很简单。 模拟器本身就带将这些程序清除到垃圾箱的功能。如下图所示&#xff1a;点击上图所示的命令“还原内容和设置...”后出现如下图所…

MySQL存储引擎的介绍

数据库存储引擎是数据库底层软件组件&#xff0c;数据库管理系统使用数据引擎进行创建、查询、更新和删除数据操作。不同的存储引擎提供不同的存储机制、索引技巧、锁定水平等功能&#xff0c;使用不同的存储引擎还可以获得特定的功能。 现在许多数据库管理系统都支持多种不同的…

Maya和Arnold的高级照明实践

Maya和Arnold的高级照明实践 时长6小时20分钟 包括项目文件 1920X1080 MP4 语言&#xff1a;英语机译中文字幕 大小&#xff1a;14.8G 题目&#xff1a;FXPHD - MYA312 - Maya & Arnold的高级照明实践 FXPHD - MYA312 - 玛雅和阿诺德的高级照明实践。 信息&#xff1a;本…

读《大道至简》第六章感想

语言确实是种工具&#xff0c;但我们不应该忽略工具的作用。我们想什么&#xff0c;去做什么事会决定使用什么工具&#xff0c;但反过来我们有什么工具也会决定我们怎么想&#xff0c;怎么做事。如果工具没有提供这个功能&#xff0c;你就不会向这方面想&#xff0c;也就不会这…

Java学习总结:1

Java的特性 1.简洁有效 2.可移植性 3.面向对象 4.解释型 5.适合分布式计算 6.拥有较好的性能 7.健壮、防患于未然 8.具有多线程处理能力 9.具有较高的安全性 10.是一种动态语言 11.是一种中性结构 Java在开发上有三个分支 一.Java EE(Java企业级开发) 二.Java SE(Java标准版…

2022-2028年中国轻型输送带行业市场发展规模及市场分析预测报告

【报告类型】产业研究 【报告价格】4500起 【出版时间】即时更新&#xff08;交付时间约3个工作日&#xff09; 【发布机构】智研瞻产业研究院 【报告格式】PDF版 本报告介绍了中国轻型输送带行业市场行业相关概述、中国轻型输送带行业市场行业运行环境、分析了中国轻型输…

从零开始写个编译器吧 - 单词化简述(Tokenization)

2019独角兽企业重金招聘Python工程师标准>>> 实际上&#xff0c;所谓的源代码&#xff0c;我们可以将其视为一段长长的字符串。所谓字符串&#xff0c;即是字符的有序集。但是&#xff0c;字符本身作为编译器的输入单位&#xff0c;粒度实在太小了&#xff0c;因此&…