日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Tree 使用方式

發(fā)布時間:2023/11/27 生活经验 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Tree 使用方式 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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 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();}}}
}

轉(zhuǎn)載于:https://www.cnblogs.com/pyfreshman/p/4935122.html

總結(jié)

以上是生活随笔為你收集整理的Tree 使用方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。