树的数据结构代码

The tree data structure can form some of the most useful and complex data structures in all of programming. In fact the tree is so powerful that I can make the bold claim:

在所有编程中,树数据结构可以形成一些最有用和最复杂的数据结构。 实际上,树是如此强大,我可以大胆地宣称:

Once you understand trees you'll be able to understand many other data structures and algorithms with ease.

理解树后,您将能够轻松理解许多其他数据结构和算法。

There is one caveat. There are so many types of trees it may be impossible to know where to start! There are B-trees, Red Black Trees, Binary Trees, AVL Trees and many others. There are abundant choices and each seems valuable to learn.

有一个警告。 树木种类繁多,可能无法知道从哪里开始! 有B树,红黑树,二叉树,AVL树等。 有很多选择,每种选择似乎都很有价值。

This presents a problem. As someone learning about trees you may find yourself asking, which tree data structure do I learn about first? Which tree is most important for me? There are so many, where do I start?

这带来了问题。 当有人学习树木时,您可能会问自己,我首先要了解哪种树木数据结构? 哪棵树对我来说最重要? 有这么多,我从哪里开始?

Learning about trees is like learning about the numerous marvels in our current world. We have a lot of choices, in fact we may even have too much choice.

学习树木就像学习当今世界的无数奇迹。 我们有很多选择,实际上我们甚至可能有太多选择。

Psychologists call it Overchoice or "choice overload", that is when faced with many options, people have a difficult choice deciding on what to do. I call it a beginning coder's worst nightmare.

心理学家将其称为过度选择 ”或“ 选择过多 ”,即当面对许多选择时,人们很难决定要做什么。 我称它为开始编码的最糟糕的噩梦。

However there is no need to panic. From my knowledge of using the tree data structure, as with most things in life, the Pareto principle (what we call the 80/20 rule) applies.

但是,无需惊慌。 根据我对使用树数据结构的了解,就像生活中的大多数事物一样,帕累托原理(我们称为80/20规则)适用。

What this means is that as a programmer, 80% of cases where you will need to use trees will be covered by approximately 20% of the types of trees that you will attempt to learn.

这意味着作为一名程序员,您将需要使用树的80%的情况将覆盖您尝试学习的树类型的大约20%。

For this reason we will focus only on these 20% which I think are the most important trees you need to understand. Don't get me wrong here, I'm not saying don't learn other types of trees. I'm saying learn these first, then focus on the others to really get that edge.

因此,我们仅关注这20%,我认为这是您需要了解的最重要的树。 不要误会我的意思,我不是说不要学习其他类型的树木。 我的意思是首先学习这些知识,然后专注于其他知识以真正获得优势。

Even when you do figure out which tree data structure you want to learn, you are faced with another problem.

即使您确定要学习哪种树数据结构,也要面对另一个问题。

There are a lot of resources out there that teach you about trees, however they all present you with some code in a particular language be it JavaScript, Java, Python or others as part of the explanation.

那里有很多资源可以教您有关树的知识,但是作为解释的一部分,它们都为您提供了某种特定语言的代码,无论是JavaScript,Java,Python还是其他语言。

In this post I break that status quo and teach you about the essential tree data structures, and all without having you write a single line of code.

在这篇文章中,我将打破现状,并向您介绍基本的树数据结构,而所有这些都无需您编写任何代码。

Join me on a journey into the world of trees, regardless of which programming language you are using, you will be able to learn all the basics you need to know about the tree data structure.

和我一起进入树的世界,无论您使用哪种编程语言,都将能够了解树数据结构所需的所有基础知识。

到达树的根 (Getting to the Root of Trees)

Let's get to the root of our discussion (pun intended). The way I like to explain trees is by relating it to something we are all familiar with, that of the biological tree. In case you are not familiar, let's look at one right now:

让我们开始讨论的根源(双关语意)。 我想解释树木的方法是将其与我们都熟悉的生物树相关联。 如果您不熟悉,让我们现在来看一个:

Look at our tree, isn't it beautiful!? We see that a tree is a giant plant with a trunk, a branch and leaves. There are also roots hidden beneath the ground that also form part of the organism.

看着我们的树,是不是很漂亮!? 我们看到一棵树是有树干,树枝和树叶的巨型植物。 地下还隐藏着一些根,这些根也构成了有机体的一部分。

A tree in computer science isn't so different. Let's look at one here:

计算机科学中的一棵树并没有什么不同。 让我们在这里看一个:

A computer science tree is very similar to a regular tree – it resembles an upside down biological tree a little doesn't it? It not only looks similar, but it also has parts that are named similar to our good ol' tangible tree.

计算机科学树与常规树非常相似,有点像颠倒的生物树,不是吗? 它不仅看起来相似,而且还具有与我们的优质有形树相似的名称。

Before we learn about the types of trees though, there are a few facts about trees you must know.

但是,在我们了解树木的类型之前,您必须了解一些有关树木的事实。

关于树,您需要了解以下5个事实: (Here are 5 facts you need to know about trees:)

1. Each of the circles in the tree is called a node and each line is called an edge.

1.树中的每个圆称为节点,每条线称为边。

2. The root node is the part of the tree that all the other parts are built upon.

2.根节点是树的所有其他部分所基于的部分。

3. There are parent nodes connected to other nodes in the direction of the root, and         child nodes connected in the direction away from the root.

3.有些父节点在根的方向上连接到其他节点,而子节点在远离根的方向上连接。

4. The last nodes of the trees are called leaves

4.树的最后一个节点称为叶子

5. The process of navigating a tree is called traversal.

5.导航树的过程称为遍历。

If you like to see things visually, here is a diagram of the tree we looked at earlier identifying the parts:

如果您想从视觉上看事物,这是我们之前看过的识别零件的树图:

You should also know that when a tree is the child of a node, it is called a subtree. Look at the diagram above, the node labelled "Parent" along with its two child nodes can be classified as a subtree.

您还应该知道,当树是节点的子代时,它称为子树。 如上图所示,标记为“父代”的节点及其两个子节点可以归为子树。

Great, now you have an idea about basic trees. So let's dive into some of the most useful type of trees you will encounter.

太好了,现在您对基本树有了一个想法。 因此,让我们深入了解您将遇到的一些最有用的树木类型。

一般树 (The General Tree)

The first type of tree we need to know about is the general tree. The general tree is what we call a superset. This is because all other types of trees are derived from the general tree.

我们需要了解的第一类树是普通树。 一般树就是我们所谓的超集。 这是因为所有其他类型的树都是从常规树派生的。

Trees are hierarchical in the way they store data. Whereas simpler data structures may store data in a linear manner (think an array), trees are non-linear.

树在存储数据方面是分层的。 较简单的数据结构可以线性方式存储数据(认为是数组),而树则是非线性的。

The general tree is the embodiment of a hierarchical tree structure as it has no restrictions on how many children each node can have, and has no restraint imposed on the hierarchy of the tree.

通用树是分层树结构的体现,因为它对每个节点可以拥有多少个子节点没有限制,并且对树的分层结构没有任何限制。

二叉树 (The Binary Tree)

It is impossible to talk about trees without talking about the binary tree (okay not totally impossible, but you know what I mean).

谈论树而不谈论二叉树是不可能的(好的并非完全不可能,但是您知道我的意思)。

Simply put, a binary tree is a type of tree that has a restriction. In the binary tree, each parent can only be linked to two child nodes within the tree.

简而言之,二叉树是一种有限制的树。 在二叉树中,每个父节点只能链接到树中的两个子节点。

There is one binary tree type that illustrates this best: the binary search tree. Trees you see aren't just empty circles connected by lines. Each of the node in the tree has a value associated with it and the entirety of the tree is a key-value structure.

有一种二进制树类型可以很好地说明这一点:二进制搜索树。 您看到的树不仅是由线连接的空圆。 树中的每个节点都有一个与其关联的值,并且树的整体是键值结构。

Binary search trees keep their keys sorted. They sort it like this: all the nodes are greater that the nodes in their left subtree, but are smaller than the nodes in their right subtree. Confused? Maybe a picture will help:

二叉搜索树将其键排序。 他们按如下方式进行排序:所有节点都大于其左子树中的节点,但小于其右子树中的节点。 困惑? 也许图片会有所帮助:

Look closely at this tree and you will learn a little secret. In the binary tree the smallest node is located at the leftmost subtree stemming from the root node. Wanna guess where we can find the largest node?

仔细观察这棵树,您将学到一个小秘密。 在二叉树中,最小的节点位于根节点的最左侧子树上。 想猜我们在哪里可以找到最大的节点?

红黑树 (Red-Black Tree)

Let's look at a variant of the binary search tree that people tend to over-complicate. I'm talking about the Red-Black Tree.

让我们看一下人们倾向于过于复杂的二叉搜索树的一种变体。 我说的是红黑树。

There are many cases of trees where data may be inserted and deleted. So variations of the binary search tree have been created which makes this constant insertion and deletion more efficient.

在许多情况下,可能会插入和删除数据的树。 因此,已经创建了二进制搜索树的变体,从而使这种恒定的插入和删除更加有效。

The Red-Black tree is one such configuration of the binary search tree that makes the insertion and deletion process more efficient.

红黑树是二进制搜索树的一种这样的配置,它使插入和删除过程更有效。

The tree does this by having a bit that adds an attribute to the node. This attribute that is added on the node is color, and this color can be interpreted as red or black. Hence the name Red-Black tree.

树通过为节点添加属性来实现此目的。 节点上添加的该属性是颜色,并且该颜色可以解释为红色或黑色。 因此,名称为红黑树。

Let's look at how a Red-Black tree many be arranged:

让我们看看如何排列一棵红黑树:

In the Red-Black tree, the root node is usually black and each red node has children that are black.

在红黑树中,根节点通常为黑色,每个红色节点都有黑色的子节点。

If you made it this far, then congratulations! You already understand enough to make a foray into the world of tree data structures.

如果您做到了这一点,那么恭喜! 您已经足够了解,可以涉足树数据结构领域。

在哪里使用树木? (Where Are Trees Used?)

At this point you may be wondering what trees are used for. That's a good question! Trees are used in many facets of development including use in:

此时,您可能想知道树木的用途。 这是个好问题! 树木用于开发的许多方面,包括:

  1. Databases资料库
  2. Compilers编译器
  3. Networking联网
  4. Heaps堆
  5. Machine Learning Algorithms机器学习算法

There are countless uses for trees and the only limit in their use is the imagination of the designer.

树木有无数用途,唯一的限制是设计师的想象力。

结语 (Wrapping Up)

In this post we began our journey into the world of the tree. Even though we covered some ground, we merely scrapped the surface of this vast and intricate data structure.

在这篇文章中,我们开始了进入树的世界的旅程。 即使我们有所了解,我们也只是废弃了这个庞大而复杂的数据结构的表面。

We whet our appetite for tree data structures by covering what trees are and looked at their structure. We then discussed three common types of trees including general trees, binary trees and red-black trees. Finally we looked at some places where trees may be used.

通过介绍树木是什么,并研究它们的结构,我们对树木数据结构产生了浓厚的兴趣。 然后,我们讨论了三种常见的树类型,包括普通树,二叉树和红黑树。 最后,我们看了一些可以使用树木的地方。

By the end of this post you should have a solid foundation to venture into the world of trees!

在这篇文章的结尾,您应该有扎实的基础来冒险进入树木世界!

下一步去哪里? (Where to Go Next?)

Want to learn about trees and other data structures without writing a single line of code? The pick up the book "Codeless Data Structures and Algorithms", where you'll learn all you need to know about data structures and algorithms without writing a single line of code!

是否想学习树和其他数据结构而无需编写任何代码? 拿起《无代码数据结构和算法》这本书,在这里您无需编写任何代码就能学到关于数据结构和算法的所有知识!

We will not only greatly expand on what we learned, but we'll cover juicy topics not covered here like tree balancing, AVL trees, B-Trees, Heaps and a ton of topics in the realm of data structures and algorithms!

我们不仅将极大地扩展我们所学的知识,还将涵盖此处未涵盖的多汁主题,例如树平衡,AVL树,B树,堆以及在数据结构和算法领域中的大量主题!

You can read the book here:

您可以在这里阅读这本书:

翻译自: https://www.freecodecamp.org/news/the-codeless-guide-to-tree-data-structures/

树的数据结构代码

树的数据结构代码_如何以无代码方式学习树数据结构相关推荐

  1. 无代码是什么?无代码好用吗?

    无代码是什么?无代码就是运用可视化和规范化的"拖拉拽"搭建简单的应用场景,现在由于市场的需要,无代码平台可谓是百家争鸣,一夜之间无数的无代码平台开始出现,很多程序员对"无 ...

  2. 如何在钉钉上开发自己的应用_对企业来说无代码开发平台是否安全

    基于十年来的行业探索经验,比较了管理软件不同开发方式的优缺点,总结了管理软件未来的发展趋势,率先系统阐述了无代码开发的概念:详细说明了无代码开发的广泛应用场景以及给企业数字化转型带来的极高价值,为管理 ...

  3. python3版本800行的代码_用800行代码做个行为树(Behavior Tree)的库(3)

    行为树最后一个要讲的地方,是关于前提(Precondition),在第一部分里,我略微提到了一下,这次我们来仔细看看,再来看看关于前提的纯虚基类的定义: 1: class BevNodePrecond ...

  4. 什么样的代码是好代码_什么是好代码?

    什么样的代码是好代码 编码最佳实践 (Coding Best-Practices) In the following section, I will introduce the topic at ha ...

  5. 一般项目中哪里体现了数据结构_优秀程序员都应该学习的数据结构与算法项目(GitHub 开源清单)...

    前言 算法为王. 想学好前端,先练好内功,内功不行,就算招式练的再花哨,终究成不了高手:只有内功深厚者,前端之路才会走得更远. 强烈推荐 GitHub 上值得前端学习的数据结构与算法项目,包含 gif ...

  6. matlab算法应用论文(带代码)_左手论文 右手代码 深入理解网红算法XGBoost

    这篇文章的起源是上周给同事讲解XGBoost原理+白板手推公式,结果差点翻车,发现有些地方理解还是不够深入,于是又花了一周时间把之前啃过的所有资料和笔记仔细梳理一遍,又想起自己的知乎专栏已经快一年没更 ...

  7. python 代码_如何让Python代码加速运行?

    Python 是一种脚本语言,相比 C/C++ 这样的编译语言,在效率和性能方面存在一些不足.但是,有很多时候,Python 的效率并没有想象中的那么夸张.本文对一些 Python 代码加速运行的技巧 ...

  8. l7sa008b故障代码_奥克斯空调故障显示代码

    奥克斯空调故障显示代码 一.70S .100S.120S.45T.50T.60T.45TA.50TA.60TA.70T1,当发生故障时,面板液晶屏上显示"故障"和相应代码. 显示代 ...

  9. l7sa008b故障代码_奥克斯空调故障代码大全

    一.70S .100S.120S.45T.50T.60T.45TA.50TA.60TA.70T1,当发生故障时,面板液晶屏上显示"故障"和相应代码. 显示代码故障原因 E1室温传感 ...

最新文章

  1. 杭电2682--Tree(Prim)
  2. 数据结构和算法分析:第三章 表、队列和栈
  3. github开源项目免费使用Azure PipeLine
  4. Apollo进阶课程 ④ | 开源模块讲解(下)
  5. 田刚院士:鼓励发展新型特色研究型大学
  6. 3.3.4.5. 日期计算
  7. 【OCR技术】大批量生成文字训练集
  8. signature=d66576fde8d472a0c1dddd8b37be6b72,Signature process
  9. python实验报告_20183122 实验一《Python程序设计》实验报告
  10. pdf签名无效解决办法_我花了一整天测试了20+款PDF工具,进来评评谁是MVP
  11. 一步一步教你写股票走势图——分时图三(对齐图表、自定义高亮)
  12. 运维小知识---If you insist running as root, then set the environment variable RUN_AS_USER=root......
  13. 我从华为身上学到的项目管理经验 -- 设计篇
  14. grpc系列3-自定义端镜像GOAWAY with error code ENHANCE_YOUR_CALM and debug data equal to “too_many_pings“
  15. C. The Intriguing Obsession
  16. [css] scale
  17. Keras中predict()方法和predict_classes()方法的区别
  18. char和varchar区别
  19. 钜泉光电2018年 IC校招笔试题目
  20. win7系统共享看不到别的计算机呢,系统之家win7系统在同一个工作组看不到其他电脑的解决方法...

热门文章

  1. Java MyShopping管理系统 > 购物结算+管理系统
  2. 阿里云域名和ip绑定最新详细步骤
  3. 红米5plus 刷twrp
  4. 图片的放大ZommJS
  5. 如何在高压系统中实现电源和信号线的电气隔离
  6. Golang四舍五入保留两位小数
  7. CANoe-第3个仿真工程-总线仿真-1概述
  8. 工业poe交换机供电方法
  9. IDE Eval Reset 插件安装使用
  10. 解决svn冲突的办法