javascript红黑树_通过JavaScript探索树

javascript红黑树

Trees are hands-down the most common data structure that you already interact with. From your file system to the DOM, many of the things your computer does is entirely dependent on generating and manipulating trees.

树是您已经与之交互的最常见的数据结构。 从文件系统到DOM,计算机要做的许多事情完全取决于生成和处理树。

什么是树? (What’s a Tree?)

In short, a tree is a collection of nodes/values where each node points to descending nodes and all connect to a single parent node. Let’s look at the tree you’re probably most familiar with: the DOM.

简而言之,树是节点/值的集合,其中每个节点都指向降序节点,并且所有节点都连接到单个父节点。 让我们看一下您可能最熟悉的树:DOM。

At the root of the tree we have the document (actually the window but shhh) and every HTML tag we add creates a new child node under it. The main point about trees is that no matter how many nodes we have we could pick anyone at random and always be able to trace our way back to the root or any other element.

在树的根部有document (实际上是窗口,但shhh),我们添加的每个HTML标签都在其下创建了一个新的子节点。 关于树的要点是,无论我们有多少节点,我们都可以随机选择任何人,并且始终能够追溯到根或任何其他元素。

有效树与无效树 (Valid vs Invalid Trees)

Previously, when we created linked lists we were technically already making viable trees because they have a root (the head/tail) and each node was connected to a child with the next/prev pointers. Starting from any node we could always find our way back to a single root, but it’s a bit trivial with just a single chain of nodes.

以前,当我们创建链接列表时,从技术上讲我们已经在制造可行的树,因为它们有一个根(头/尾),并且每个节点都通过next / prev指针连接到一个子节点。 从任何节点开始,我们总能找到返回到单个根的方法,但是仅包含单个节点链就显得微不足道了。

All of these are valid trees. Most of our different structures based off of trees will be pertaining more with how the data inside the trees are organized, but they’ll all look something like this.

所有这些都是有效的树。 我们基于树的大多数不同结构都将更多地与树中数据的组织方式有关,但它们看起来都是这样。

There are some rules we need to follow to have a proper tree. A tree cannot reference its own root, for example if we ran a traversal algorithm on a circular linked list then it would quickly become a recursion nightmare. We also can’t have more than one root or a node with more than one parent.

要拥有合适的树,我们需要遵循一些规则。 一棵树不能引用它自己的根,例如,如果我们在循环链表上运行遍历算法,那么它将很快成为递归的噩梦。 我们也不能有多个根或一个节点有多个父级。

Finally, a tree must have a directionality to it with everything moving out from the root. A cluster of nodes without a direction is actually its own much more complicated structure, graphs, which we’ll cover in future articles 😉.

最后,一棵树必须具有方向性,一切都将从根中移出。 一个没有方向的节点簇实际上是它自己更复杂的结构,即图,我们将在以后的文章中进行介绍。

We can’t just start linking nodes together and call it a tree, if we break these rules then future data structures and algorithms just won’t be possible.

我们不能只是开始将节点链接在一起并称其为树,如果违反这些规则,那么将来的数据结构和算法将无法实现。

树木解剖 (Tree Anatomy)

While pretty boring it’s still necessary to cover the most important terminology you’ll constantly see as you learn more about trees.

虽然很无聊,但是仍然有必要涵盖最重要的术语,因为您会在学习有关树的更多知识时不断看到。

  • Node - Each single object or data point.

    Node -每个单个对象或数据点。

  • Root - The first and uppermost node in the tree from which all other nodes are derived from.

    Root -树中所有其他节点都源自的第一个和最高节点。

  • Edge - A connection between two nodes.

    Edge -两个节点之间的连接。

  • Parent - The immediate ancestor of a lower node.

    Parent -下级节点的直接祖先。

  • child - The immediate descendant of a higher node.

    child更高节点的直接后代。

  • Siblings - Two nodes on the same depth with the same parent.

    Siblings -具有相同父对象的相同深度的两个节点。

  • Leafs - The bottommost nodes with no children.

    Leafs -没有孩子的最底层节点。

  • Depth - The height of the tree measured in levels with the number of edges away from the root, so level 2 is only two edges away from the root.

    Depth -树木的高度,其水平程度是指远离根部的边缘数,因此2级水平仅为指远离根部的两个边缘。

  • Breadth - The width of the tree measured by the number of leafs.

    Breadth -树的宽度,以叶子数衡量。

  • Subtree - A node and its descendants which could be treated as an independent tree. For example, if we created a dictionary as a tree and used a search algorithm that looked at each item alphabetically we could use the node for section of the first letter as the root instead of looking at every item in the tree.

    Subtree -节点及其后代,可以视为独立的树。 例如,如果我们将字典创建为树,并使用搜索算法按字母顺序查看每个项目,则可以将第一个字母的节的节点用作根,而不是查看树中的每个项目。

总结思想 (Closing Thoughts)

Right now this may seem like a whole lotta fluff but the devil really is in the details. As you progress to more elaborate structures it’ll become increasingly easier to overlook these details and drown yourself in very hard to debug errors.

现在看来,这似乎是一团糟,但真正的细节在于魔鬼。 随着您开发更精细的结构,忽略这些细节并使自己陷入难以调试的错误中变得越来越容易。

翻译自: https://www.digitalocean.com/community/tutorials/js-trees

javascript红黑树

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值