title: How would I add an array of strings into a binary tree without hitting max iterations in Python?
body: <p>I have a dictionary of words containing 370100 elements (strings). I need to add them into a binary tree to sort and find them. How would I do this without hitting python's max iterations? I tried using sys.setrecursionlimit but it was slow and eventually spit out zsh: segmentation fault python tree.py.</p>
  <pre><code>import sys
  
  class Node(object):
  
  def __init__(self, value):
      self.value = value
      self.left = None
      self.right = None
      self.count = 1
  
  def __str__(self):
      return 'value: {0}, count: {1}'.format(self.value, self.count)
  
  def insert(root, value):
  if not root:
      return Node(value)
  elif root.value == value:
      root.count += 1
  elif value &lt; root.value:
      root.left = insert(root.left, value)
  else:
      root.right = insert(root.right, value)
  
  return root
  
  def create(seq):
  root = None
  for word in seq:
      root = insert(root, word)
  
  return root
  
  def search(root, word, depth=1):
  if not root:
      return 0, 0
  elif root.value == word:
      return depth, root.count
  elif word &lt; root.value:
      return search(root.left, word, depth + 1)
  else:
      return search(root.right, word, depth + 1)
  
  def print_tree(root):
  if root:
      print_tree(root.left)
      print (root)
      print_tree(root.right)
  
  def toArr(txtfile):
  txtArr = []
  file = txtfile
  for line in file:
      txtArr.append(line)
  return txtArr
  
  
  
  dictionary = open('dictionary.txt', 'r')
  dic = toArr(dictionary)
  sys.setrecursionlimit(370100)
  
  tree = create(dic)
  
  print_tree(tree)
  </code></pre>
  <p>This is what I have. Any solutions would be appreciated! :)</p>