Sunday, November 3, 2013

Binary Tree in Ruby

I've returned back to Dave Thomas's katas and picked up number 11: Sorting. Harking back to the reuse we saw in the data munging kata we must sort through a pair of unordered lists efficiently without using built in libraries. I chose to implement a binary tree and enjoyed reminding myself of this algorithm very much. As usual, source is at the GitHub.

    def insert(node)
      self.count_all = self.count_all + 1
      insert_left(node) if node.word < word
      self.count = count + 1 if node.word == word
      insert_right(node) if node.word > word
    end

No comments:

Post a Comment