简体   繁体   English

树抽象数据类型

[英]Tree Abstract Data Type

I'm doing a unit called Data Structures and Algorithms. 我正在做一个叫做数据结构和算法的单元。 We've just started and my professor has just taught us the basics of what Algebraic Semantics is and what Axioms are etc. Till now, I've just used Trees in the form of arrays. 我们才刚刚开始,我的教授刚刚教了我们代数语义学和公理是什么的基础。到目前为止,我只是以数组的形式使用树。 Not using the signature for pre-ordered tree as tree(value, tree, tree) where value is the value in the node, left node is the first tree and right node is the second tree. 不将预签名树的签名用作树(值,树,树),其中value是节点中的值,左节点是第一棵树,右节点是第二棵树。

Now that i'm defining my tree as either tree(value, tree, tree) or Nil, I can't figure out how to go about defining the algebra for addNode(value, tree). 现在,我将树定义为tree(值,树,树)或Nil,我不知道如何定义addNode(值,树)的代数。

It just gets more and more complicated with each level, plus, I can't possibly think of anyway to scan through one level as once, been trying since like an hour now. 每个级别都变得越来越复杂,而且,我想不起来想一次扫描一个级别,因为从现在开始大约一个小时就一直在尝试。 The algebra just branches out into more and more if-elses as we go down the tree. 当我们从树上下来时,代数只是分支成越来越多的if-eles。 Am I doing it right? 我做对了吗? Can you point me in the right direction? 你能为我指出正确的方向吗? Or trees cannot be implemented as tree(value, tree, tree)? 还是不能将树实现为树(值,树,树)?

It's a part of my tutorial(not worth any marks, in the additional questions), but I'm not looking for an instant answer, I like the subject, and would like to learn more. 这是我的教程的一部分(在其他问题中不值任何分数),但是我不是在寻找即时答案,我喜欢这个主题,并且想了解更多。

Edit 1: I checked out Wikipedia, and I don't want to use the textbook for the clear answer, I'm just looking for a hint towards the right direction, whether my approach is right or it's completely impossible to define a tree as tree(value, tree, tree). 编辑1:我签出了Wikipedia,但我不想使用教科书给出明确的答案,我只是想向正确的方向提供提示,无论我的方法是正确的还是将树定义为完全不可行树(值,树,树)。 I know you can represent a tree ADT in form a list. 我知道您可以以列表形式表示树型ADT。 But I wanted to really think about it. 但我想真正考虑一下。 Hope it makes sense. 希望有道理。 Thanks a lot guys! 非常感谢你们!

Edit 2: Uhmm, it's hard to explain over the internet. 编辑2:嗯,很难在互联网上解释。 Let's say I'm defining a new data structure called "tree". 假设我正在定义一个名为“ tree”的新数据结构。 I can define it any way I want, and it must behave like a balanced binary tree(though, values of parents and children dont matter) So I define it as tree : tree(value, tree, tree) OR nil It's not programming code, it's just how I define it. 我可以用自己想要的任何方式定义它,并且它的行为必须像平衡的二叉树(尽管父母和孩子的值无关紧要),所以我将其定义为tree:tree(value,tree,tree)OR nil这不是编程代码,这就是我的定义方式。 Tree is a value + 2 other trees or Tree is nil. Tree是一个值+ 2个其他树,或者Tree是nil。 Now addNode(value, tree) adds a node to tree while still keeping it balanced. 现在,addNode(value,tree)将一个节点添加到树中,同时保持其平衡。 It's not programming code, it's just algebraic semantics. 这不是编程代码,而只是代数语义。 I don't know if I can explain it properly. 我不知道我能否正确解释。 But I got to a solution that I can achieve using Queues or Stacks, but that's another ADT I'll have to define, which is not valid. 但是我找到了一个可以使用队列或堆栈实现的解决方案,但这是我必须定义的另一个ADT,这是无效的。

Edit 3: Seems like I had assumed many things that made the problem harder than it really was supposed to be. 编辑3:好像我已经做了很多事情,使问题变得比原本想的要难。 First of all, from the little explanation I gave, Gamecat's answer was perfect. 首先,根据我给的小解释,Gamecat的答案是完美的。 But I agree with the comments, it's perfectly valid to include other ADTs. 但是我同意这些意见,包括其他ADT是完全正确的。 In fact, when we build anything that uses an Int, we're using an ADT for that structure. 实际上,当我们构建使用Int的任何东西时,我们都在对该结构使用ADT。 I thought each ADT had to be unique. 我认为每个ADT都必须是唯一的。 Anyways, thanks a lot guys for your answers! 无论如何,非常感谢你们的回答!

If you want to add a node to a tree, you can use a recursive function. 如果要将节点添加到树,则可以使用递归函数。

I assume the tree is ordered. 我认为树是有序的。 So you should get something like this: 所以你应该得到这样的东西:

AddNode(value, tree)

if tree is empty, create a new tree with value as node and no subtrees.
if value lesser than the treenode, call AddNode on the left branch.
else call AddNode on the right branch. (if duplicates are allowed).

Be sure to update the subtrees if they are changed! 如果更改了子树,请确保对其进行更新!

You can convert this to a non recursive function by: 您可以通过以下方法将其转换为非递归函数:

if tree is empty, return a new tree with value as node and no subtrees.
if value is lesser than treenode, and there is no left subtree, create a new left subtree with value as node and no subtrees.
if value is lesser that treenode, and there is a left subtree, try again with the left subtree.
if value is greater or equal than treenode, and there is no right subtree, create a new right subtree with value as node and no subtrees.
if value is greater or equal than treenode, and there is a right subtree, try again with the right subtree.

If the tree needs to be balanced. 如果树需要平衡。 You need to store get a balance weight (which can be -1, 0 or 1). 您需要存储以获得平衡重(可以是-1、0或1)。 If you need to add a node on a "heavy" site, you need to reshuffle this. 如果您需要在“繁重”的站点上添加节点,则需要重新组合。 For example if the left side has one node more than the right side and you need to add one more node to the left. 例如,如果左侧比右侧多一个节点,而您需要在左侧再增加一个节点。 You need to get the node with the highest value from the left subtree and promote that to the current top. 您需要从左侧子树中获取具有最高值的节点,并将其提升到当前顶部。 The previous top is added to the right subtree. 前一个顶部已添加到右侧子树中。 Be sure to keep the subtrees balanced too. 确保子树也保持平衡。

Example: add the nodes 0,1,2,3,4 示例:添加节点0、1、2、3、4

Add(0)           0

Add(1)           0
                  \
                   1

Add(2)           0 (2)  =>      1 (2) =>  1
                  \            /         / \
                   1          0         0   2

Add(3)           1
                / \
               0   2
                    \ 
                     3

Add(4)           1 (4)     => 2 (4)  =>      2
                / \          / \            / \
               0   2        1   3          1   3
                    \      /              /     \
                     3    0              0       4

This is a hard question, since it's so vague. 这是一个很难的问题,因为它很模糊。 I assume you have a text book or something similiar as part of your course material. 我假设您有一本教科书或类似的东西作为您课程材料的一部分。 Even so, it feels as if many the things you're having problems with are explained by a basic resource, such as the Wikipedia entry on binary trees. 即使这样,感觉您遇到的许多问题似乎都是由基本资源来解释的,例如二进制树上的Wikipedia条目

This page describes how to do various tree traversals, and how trees can be represented. 本页描述如何进行各种树遍历以及如何表示树。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM