title: Python: Create a Binary search Tree using a list
body: <p>The objective of my code is to get each seperate word from a txt file and put it into a list and then making a binary search tree using that list to count the frequency of each word and printing each word in alphabetical order along with its frequency. Each word in the can only contain letters, numbers, -, or ' The part that I am unable to do with my beginner programming knowledge is to make the Binary Search Tree using the list I have (I am only able to insert the whole list in one Node instead of putting each word in a Node to make the tree). The code I have so far is this:</p>
  
  <pre><code>def read_words(filename):
      openfile = open(filename, "r")
      templist = []
      letterslist = []
      for lines in openfile:
          for i in lines:
              ii = i.lower()
              letterslist.append(ii)
      for p in letterslist:
          if p not in ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',"'","-",' '] and p.isdigit() == False:
              letterslist.remove(p)      
      wordslist = list("".join(letterslist).split())
      return wordslist
  
  class BinaryTree:
      class _Node:
          def __init__(self, value, left=None, right=None):
              self._left = left
              self._right = right
              self._value = value
              self._count = 1
  
  
      def __init__(self):
          self.root = None
  
      def isEmpty(self):
          return self.root == None
  
      def insert(self, value) :
          if self.isEmpty() :
              self.root = self._Node(value)
              return
          parent = None
          pointer = self.root
          while (pointer != None) :
              if value == pointer._value:
                  pointer._count += 1
                  return
  
              elif value &lt; pointer._value:
                  parent = pointer
                  pointer = pointer._left
  
              else :
                  parent = pointer
                  pointer = pointer._right            
  
          if (value &lt;= parent._value) :
              parent._left = self._Node(value)
          else :
              parent._right = self._Node(value)    
  
      def printTree(self):
          pointer = self.root
          if pointer._left is not None:
              pointer._left.printTree()
          print(str(pointer._value) + " " + str(pointer._count))
          if pointer._right is not None:
              pointer._right.printTree()
  
  
  
  
      def createTree(self,words):
          if len(words) &gt; 0:
              for word in words:
                  BinaryTree().insert(word)
              return BinaryTree()
          else:
              return None
  
      def search(self,tree, word):
          node = tree
          depth = 0
          count = 0
          while True:
              print(node.value)
              depth += 1
              if node.value == word:
                  count = node.count
                  break
              elif word &lt; node.value:
                  node = node.left
              elif word &gt; node.value:
                  node = node.right
          return depth, count
  
  
  def main():
      words = read_words('sample.txt')
      b = BinaryTree()
      b.insert(words)
      b.createTree(words)
      b.printTree()
  </code></pre>