# coding: utf-8

import json
from random import randrange, shuffle


class Node(object):

    def __init__(self, word):
        self.word = word
        self.count = 1
        self.left = None
        self.right = None

    def __str__(self):
        return '{{"word": "{}", "count": {}, "left": {}, "right": {}}}'.format(
            self.word, self.count, str(self.left or 'null'), str(self.right or 'null'))


def add_to_tree(root_node, word):

    if word == root_node.word:
        root_node.count += 1
    elif word < root_node.word:
        if root_node.left:
            add_to_tree(root_node.left, word)
        else:
            root_node.left = Node(word)
    elif word > root_node.word:
        if root_node.right:
            add_to_tree(root_node.right, word)
        else:
            root_node.right = Node(word)


def main():
    get_alpha = lambda: chr(randrange(97, 123))
    get_word = lambda: ''.join([get_alpha() for i in range(randrange(4, 10))])
    get_article = lambda: ' '.join([' '.join([get_word()] * randrange(10))
        for i in range(randrange(2000, 3000))])
    words = get_article().split()
    shuffle(words)

    root = Node(words.pop())
    for word in words:
        add_to_tree(root, word)
    print json.dumps(json.loads(str(root)), indent=2)


if __name__ == '__main__':
    main()

@@ -225,257 +225,444 @@
 def 
-__str__(self):%0A        return '%7B%7B%22word%22: %22%7B%7D%22, %22count%22: %7B%7D, %22left%22: %7B%7D, %22right%22: %7B%7D%7D%7D'.format(%0A            self.word, self.count, str(self.left or 'null'), str(self.right or 'null'))%0A%0A%0Adef add_to_tree(root_node
+add(self, word):%0A        if word == self.word:%0A            self.count += 1%0A        elif word %3C self.word:%0A            if self.left:%0A                self.left.add(word)%0A            else:%0A                self.left = Node(word)%0A        elif word %3E self.word:%0A            if self.right:%0A                self.right.add(word)%0A            else:%0A                self.right = Node(word)%0A%0A    def add_words(self
 , word
+s
 ):%0A
-%0A
     
-if word == root_node.
+    for word in 
 word
+s
 :%0A  
@@ -671,43 +671,72 @@
     
-root_node.count += 1%0A
+    self.add(word)%0A%0A    def search(self, word):%0A    
     
-el
 if word 
 %3C ro
@@ -735,19 +735,15 @@
 ord 
-%3C root_node
+== self
 .wor
@@ -757,158 +757,289 @@
     
-if root_node.left:%0A            add_to_tree(root_node.left, word)%0A        else:%0A            root_node.left = Node(word)%0A    elif word %3E root_node
+    return self%0A        elif word %3C self.word and self.left:%0A            return self.left.search(word)%0A        elif word %3E self.word and self.right:%0A            return self.right.search(word)%0A        return None%0A%0A    def items(self):%0A        return %7B%0A            'word': self
 .word
-:
+,
 %0A   
@@ -1047,128 +1047,242 @@
     
-if root_node.right:%0A            add_to_tree(root_node.right, word)%0A        else:%0A            root_node.right = Node(word
+    'count': self.count,%0A            'left': self.left.items() if self.left else None,%0A            'right': self.right.items() if self.right else None,%0A        %7D%0A%0A    def __str__(self):%0A        return json.dumps(self.items(), indent=2
 )%0A%0A%0A
@@ -1597,16 +1597,36 @@
 words)%0A%0A
+    a = words.pop()%0A
     root
@@ -1654,111 +1654,68 @@
     
-for word in 
+root.add_words(
 words
-:
+)
 %0A    
-    add_to_tree(root, word)%0A    print json.dumps(json.loads(str(root)), indent=2
+print root%0A    print root.search(a
 )%0A%0A%0A

Update tree.py