【发布时间】:2022-01-08 15:55:17
【问题描述】:
如何使用我们执行的子更新来更新 Tree 的父节点?特别是当您执行 BFS 或 DFS 搜索时,在节点上执行检查,然后更新该节点。是什么导致内存或编程语言知道“哦,嘿,我也必须对根节点进行此更新!”
在我的示例中,我使用的是 Trie(不是很重要,但我将其称为树)。我在下面有这个 BFS 搜索,它搜索了一堆节点,并将使用特定的单词值进行更新。我的评论是我的问题所在。该子节点当前存储在“deque”中。程序如何知道我的意思是更新 Root 中的值,而不仅仅是传递给变量“deque”的值?
对我来说,我认为应该发生的是 Root 不应该被更新,唯一被更新的是变量“deque”,然后在它完成之后,一切都被垃圾收集并且 Root 保持不变。相反,当“deque”更新时,Root 也会更新。也许我在数据结构课程中错过了这一点,但它一直困扰着我一段时间,而且我一直无法找到解释这一点的资源。
private static void BFS_UpdateAllWords(Node Root, string testword, string updatevalue)
{
Queue<Node> bfs_queue = new Queue<Node>();
bfs_queue.Enqueue(Root);
while (bfs_queue.Count > 0)
{
var deque = bfs_queue.Dequeue();
foreach (string childKey in deque.Children.Keys)
{ // Update all child nodes at the key
if (deque.Children[childKey].Word.Equals(testword))
{
// This part right here for any time of Tree traversal
deque.Children[childKey].WordType = updatevalue;
}
bfs_queue.Enqueue(deque.Children[childKey]);
}
}
}
【问题讨论】:
-
我不确定我是否理解您的问题。你认为正在发生什么?为什么你认为它正在发生?应该发生的事情。
标签: c# search memory data-structures computer-science