简体   繁体   English

关于从二叉树复制/获取节点的问题

[英]problem regarding copying/taking a node from the binary tree

question(language used - js) - when you copy a node from the binary tree, are the whole branches below that node (you know binary tree node has properties left and right , below them, are more nodes.) also get copied inside the variable?问题(使用的语言 - js) - 当您从二叉树复制节点时,该节点下方的整个分支(您知道二叉树节点具有leftright属性,在它们下方,有更多节点。)也被复制到多变的? Well... node is an object in the binary tree...and objects are reference values , so I am guessing that the same node of the tree is enqueued there, instead of copy of that node .嗯... node是二叉树中的一个对象... objectsreference values ,所以我猜树的同一个node在那里排队,而不是copy of that nodecopy of that node

I am trying to do breadthFirstSearch in the binary tree, where I have to put the node of the tree in the queue .我正在尝试在二叉树中进行breadthFirstSearch ,我必须将树的node放入queue I want to know that, when I enqueue the node of the binary tree in queue , is the copy of that specific node along with its keys or properties (as we know) gets added to the queue or only reference to that node is added?我想知道的是,当我enqueuenode中的binary treequeue ,是特定的副本node与其一起keysproperties (如我们所知)被添加到queue或仅与参考node添加? Because if that specific node is copied, and the tree has 1000s of nodes in it, then the whole branch below that node might get copied too(since I don't know if this actually happens or not)....which is very very bad for performance and memory.因为如果该特定节点被复制,并且树中有 1000 个节点,那么该node下方的整个分支也可能被复制(因为我不知道这是否真的发生了)......这是非常对性能和内存非常不利。 If it's only referenced to that node, then it's completely fine as I am not copying the whole branch of the tree.如果它只引用到该节点,那么它完全没问题,因为我没有复制树的整个分支。

JavaScript assigns objects and arrays by reference, which means a given object or array can be assigned to any number of variables without duplicating it. JavaScript 通过引用分配对象数组,这意味着可以将给定的对象或数组分配给任意数量的变量,而无需复制它。 Besides, objects and arrays are mutable , which means you can modify their contents after they have been created.此外,对象和数组是可变的,这意味着您可以在创建它们后修改它们的内容。

For instance:例如:

a = [1,2,3];     // a is a reference to a freshly created array
b = a;           // b is a second reference to the same array
a.pop();         // a is used to remove the last element of the underlying array
console.log (b); // b is also affected

output:输出:

Array [ 1, 2 ] // through `a` we modified the contents of `b`.

The main advantage of mutability is efficiency, as you pointed out.正如您所指出的,可变性的主要优点是效率。 On the other hand, working with mutable objects is tricky, as you are left in charge of guaranteeing the consistency of your references (in layman's terms, you are constantly at risk of inadvertently clobbering the contents of some 'b' through some 'a').另一方面,使用可变对象很棘手,因为您需要负责保证引用的一致性(用外行的话来说,您经常面临无意中通过某些 'a' 破坏某些 'b' 的内容的风险)。
Since JavaScript has a very limited notion of variable typing , there is no simple way to tell which identifier(s) allow to access and modify a given piece of data at some point.由于 JavaScript 对变量类型的概念非常有限,因此没有简单的方法来判断哪些标识符允许在某个时候访问和修改给定的数据。
Defining a notion of equality is also difficult: are we talking about equality of references (a and b referencing the same underlying object) or equality of contents (a and b referencing two distinct objects that happen to hold identical data)?定义相等的概念也很困难:我们是在谈论引用的相等(a 和 b 引用相同的底层对象)还是内容的相等(a 和 b 引用碰巧拥有相同数据的两个不同对象)?
This pitfall is the source of countless puzzling bugs.这个陷阱是无数令人费解的错误的根源。
To make matters worse, built-in methods offer a mix of approaches.更糟糕的是,内置方法提供了多种方法。 For instance, some chainable Array methods ( filter , map , reduce , find ) return new arrays, others ( pop / push , shift / unshift , splice , sort ) modify the array in place.例如,一些可链接的数组方法( filtermapreducefind )返回新数组,其他方法( pop / pushshift / unshiftsplicesort )就地修改数组。

JavaScript is an old language which evolved through 6 versions while maintaining upward compatibility, ending up with a collection of mechanisms that are very difficult to specify in a rigorous, deterministic way. JavaScript 是一门古老的语言,经过 6 个版本的演变,同时保持向上兼容性,最终形成了一系列很难以严格、确定性方式指定的机制。 This is why the official specification of the version implemented in current browsers (ECMA 6.0) is, unfortunately, anything but educational material.这就是为什么在当前浏览器 (ECMA 6.0) 中实现的版本的官方规范不幸的是,除了教育材料之外什么都没有。
I doubt a language learner would get much benefit from reading it beyond the initialoverview , which doesn't dwell into details.我怀疑语言学习者会从阅读它超出最初的概述中获得很多好处,它没有详细说明。
Unless you want to specialize in JS and tackle the spec yourself, you'll have to rely on gloss by bloggers, like this one or perhaps this one .除非你想专注于 JS 并自己处理规范,否则你将不得不依赖博主的光泽,比如这个或者这个

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

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