博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaScript Lab - Articles - Non-recursive Preorder Traversal - Part 1
阅读量:6256 次
发布时间:2019-06-22

本文共 2514 字,大约阅读时间需要 8 分钟。

Non-recursive Preorder Traversal - Part 1

Published on 1st of November 2008. Copyright Tavs Dokkedahl. Displayed 4534 time(s)

A brief introduction to trees

If you have never heard of trees keep reading otherwise you can safely skip to the next section.

A tree is a datastructure which organizes data as shown on the picture below.

Tree datastructure

  • The root is the top most element. A root element has no parent element nor any siblings.
  • A branch has exactly 1 parent element and at least 1 child element. It may have 0 or more siblings.
  • A leaf has exactly 1 parent and no child elements. If may have 0 or more siblings.

In general all elements in the tree are called nodes and we differentiate between root, branches and leafs by checking their parent/child properties.

The DOM of a HTML document is such a tree. Below is a picture of how some of the elements of this page are ordered.

Tree datastructure

This tree corresponds to the following HTML code

1  2    3      4       JavaScript Lab - Articles - Non-recursive Preorder Traversal 5      6     
7 8 9
10
11
12
    13 ...14
15
16
17
18 ...19 20 JavaScript Lab - Articles - Non-recursive Preorder Traversal
    ...
...

The root node is the html element. It has 2 children - the head and the body elements.

The head element has 2 children - the title and the meta elements. These two elements are siblings and share the same parent node.

The meta element is a leaf as it has no child nodes whereas the title is not as it has a string value.

The title string itself is a text node and a leaf.

When we are doing a preorder traversal we are visiting each node in the tree in the following order

Tree datastructure

So a preorder traversal is a ordering of the elements as they are ordered in the source code.

Traverse/walk DOM tree recursively

Task definition:
You have a DOM tree (startNode which can be the whole document),
and need to find first specific tag in this tree.
Here is the recursion function to do this:
1.
function findNodeByTag(startNode, tagName) {
2.
    if (startNode.nodeName.toLowerCase() == tagName.toLowerCase()) return startNode;
3.
    var childList = startNode.childNodes;
4.
    for (var i=0; i<childList.length; i++) {
5.
        return findNodeByTag(childList[i], tagName);
6.
    }
7.
    return null;
8.
}
And you call it:
1.
findNodeByTag(myDOMobj, "img");

转载地址:http://antsa.baihongyu.com/

你可能感兴趣的文章
Yii用ajax实现无刷新检索更新CListView数据
查看>>
App 卸载记录
查看>>
南京大学周志华教授当选欧洲科学院外籍院士
查看>>
计算机网络与Internet应用
查看>>
Django 文件下载功能
查看>>
走红日本 阿里云如何能够赢得海外荣耀
查看>>
qt 学习之路2
查看>>
线上应用故障排查之二:高内存占用
查看>>
异常处理汇总 ~ 修正果带着你的Code飞奔吧!
查看>>
PCIE_DMA:xapp1052学习笔记
查看>>
python ----字符串基础练习题30道
查看>>
uva-10879-因数分解
查看>>
python 调用aiohttp
查看>>
linux下文件的一些文件颜色的含义
查看>>
跨域iframe高度自适应(兼容IE/FF/OP/Chrome)
查看>>
学习鸟哥的Linux私房菜笔记(8)——文件查找与文件管理2
查看>>
升级fedora 18到fedora 19
查看>>
11月20日学习内容整理:jquery插件
查看>>
SVN与TortoiseSVN实战:补丁详解
查看>>
获取页面中所有dropdownlist类型控件
查看>>