actor binary tree lab4

forward 与 ! (tell) 的差异,举个例子:

Main(当前actor): topNode ! Insert(requester, id=1, ele = 2)

topNode: root ! Insert(requester, id=1, ele = 2)

对于root而言,Insert消息的来源是topNode

假如第二行替换为

topNode: root forward Insert(requester, id=1, ele = 2)

此时,对于root而言,Insert消息的来源是Main

从上面的例子可以看出,! 和 forward之间是有差别的,但是差别本身比较tricky,会增加理解的成本,所以传递消息时,把消息的actorRef作为参数传递会简单很多。

在lab4中,就需要考虑到!与forward的异同点

testcase 的 code

  test("proper inserts and lookups") {
    val topNode = system.actorOf(Props[BinaryTreeSet])

    topNode ! Contains(testActor, id = 1, 1)
    expectMsg(ContainsResult(1, false))

    topNode ! Insert(testActor, id = 2, 1)
    topNode ! Contains(testActor, id = 3, 1)

    expectMsg(OperationFinished(2))
    expectMsg(ContainsResult(3, true))
  }

结合topNode,与root的处理逻辑

  val normal: Receive = {
//    这个forward非常重要,不能写成 !,不然第一个testcase都不过去
    case operation: Operation => root ! operation
    case GC => {
      //garbageCollecting需要先变,因为节点的复制可能会很久
      val newRoot = createRoot
      context.become(garbageCollecting(newRoot))
      root ! CopyTo(newRoot) 

    }
    case _ => println("unknown operation")
  }
// root 的处理逻辑  case Insert(requester, id, elem) =>
      if(this.elem == elem) {
        if(this.removed) this.removed = false
        requester ! OperationFinished(id)
      } else {
        val child = if(this.elem > elem) Left else Right
        if(subtrees contains child) subtrees(child) ! Insert(requester, id, elem)
        else {
          subtrees += child -> context.actorOf(props(elem, false))
          requester ! OperationFinished(id)
        }
      }

在source code中,topNode收到消息后,把消息传递给root,root认为消息的来源是topNode。假如消息不绑定requester参数,那么通过sender获得actor是tiopNode,而不是main。我们在testcase中做assertion的话,肯定就是错的。

另外,上例中用到了testActor,它是implicit变量,用于处理发送到main中的消息。主要是测试方便。

一个debug了5个小时的bug

case msg: Operation =>
        pendingQueue = pendingQueue.enqueue(msg)

pendingQueue.enqueue(msg) 并不能更新pendingQueue自己,必须重新赋值才行。

时间: 2024-08-24 21:14:57

actor binary tree lab4的相关文章

Maximum Depth of Binary Tree

这道题为简单题 题目: Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 思路: 我是用递归做的,当然也可以用深搜和广搜,递归的话就是比较左右子树的深度然后返回 代码: 1 # Definition for a binary tre

226反转二叉树 Invert Binary Tree

Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Trivia:This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a wh

[leetcode] 104. Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 递归遍历 左子树 和 右子树 一刷: public int maxDepth(TreeNode root) { if(root == null){ return 0; } int

LeetCode 145 Binary Tree Postorder Traversal(二叉树的后续遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其后续遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recur

LeetCode Binary Tree Inorder Traversal

LeetCode解题之Binary Tree Inorder Traversal 原题 不用递归来实现树的中序遍历. 注意点: 无 例子: 输入: {1,#,2,3} 1 2 / 3 输出: [1,3,2] 解题思路 通过栈来实现,从根节点开始,不断寻找左节点,并把这些节点依次压入栈内,只有在该节点没有左节点或者它的左子树都已经遍历完成后,它才会从栈内弹出,这时候访问该节点,并它的右节点当做新的根节点一样不断遍历. AC源码 # Definition for a binary tree node

Java [Leetcode 107]Binary Tree Level Order Traversal II

题目描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order

Lowest Common Ancestor of a Binary Tree

题目连接 https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Common Ancestor of a Binary Tree Description Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on

Invert Binary Tree

package cn.edu.xidian.sselab; /** * title:Invert Binary Tree * content: * nvert a binary tree.  *     4 *   /   \ *  2     7 * / \   / \ *1   3 6   9 *to *     4 *   /   \ *  7     2 * / \   / \ *9   6 3   1 */public class InvertBinaryTree { /**    

leetcode笔记:Minimum Depth of Binary Tree

一. 题目描述 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 二. 题目分析 这道题属于二叉树的深度优先搜索,然后返回深度最小的值,可以递归(当然,也可以使用迭代)来实现.递归退出的条件是到达叶子节点或者到达空子树,使用空子树