jgailor (1) [Avatar] Offline
#1
In both examples, the abstract base classes reference a concrete subclass (AList -> SimpleList and Tree -> BST respectively). This does not seem right, and the Scala REPL refuses to compile the code from either of these examples. My question is am I doing something wrong, is there missing code, or is this code just not correct Scala?
aslam.khan (50) [Avatar] Offline
#2
Re: Linked List / Binary Search Tree Code
Hello there

It's not a problem with the code, but the way the REPL works. The solution is in this thread http://www.manning-sandbox.com/thread.jspa?threadID=71139&tstart=0

Let me know if you get it working or still struggling. I intend to update the book with the REPL instructions.
gmurray13 (1) [Avatar] Offline
#3
Regarding the first post on this thread - although I've been able to get the binary search tree example working with the ":paste" tip mentioned above, I still don't understand why it's a clean design to have an abstract base class (i.e., Tree) make use of one of its concrete subclasses (i.e., BST) inside a method of the base class.

Why is this an acceptable design pattern?

Thanks in advance for the clarification.
367250 (1) [Avatar] Offline
#4
I agree with the previous poster and would appreciate a modified example that does not require an abstract class to reference a concrete subclass.
145444 (1) [Avatar] Offline
#5
Hi,

great book!

I found the depthFirst method on Tree a little confusing. Why to put the tree into a list ?
Here my suggestion

def depthFirst:List[Int] = {
       def loop(acc:List[Int], tree:Tree):List[Int] =
            if(tree.isEmpty) acc
            else tree.value :: loop(acc, tree.left) ++ loop(acc, tree.right)
          
       loop(List(), this)
  }

the problem here is ++ operator on lists - needs explanation what the op. does. We could also use the concat function implemented in the linked list chapter.

By the way, loop inner function is not tail recursive - see the hint in the book p. 111 in my book pdf-version.

Regards
Tomasz