old_contents: # -*- coding: utf-8 -*-
  
  import collections
  
  import midas.tools as md_tools
  
  class BucketTree(dict):
  
      def __init__(self, splitfunc, name='root'):
          dict.__init__(self)
          self.splitfunc = splitfunc
          self.name = name
          self.leafs = []
          self.bucket = collections.defaultdict(list)
  
      def grow(self, item, key):
          if key is None:
              self.leafs.append(item)
              return
          split = self.splitfunc(key)
          if len(split) == 2:
              head, tail = split 
          else:
              head, tail = split[0], None
          if head not in self:
              self[head] = BucketTree(self.splitfunc, head)
          self[head].grow(item, tail)
  
      def fill(self, item, key):
          if key is None:
              self.bucket[None].append(item)
              return
          split = self.splitfunc(key)
          if len(split) == 2:
              head, tail = split
          else:
              head, tail = split[0], None
          if head not in self:
              self.bucket[head].append(item)
          else:
              self[head].fill(item, tail)
  
  def build_tree(sites):
      root = Parent()
      for s in sites:
          domain = s.lower().split('/', 1)[0]
          root.add_branch(domain, s)
      return root
  
new_contents: # -*- coding: utf-8 -*-
  
  import collections
  
  import midas.tools as md_tools
  import midas.statistics as md_stats
  
  class BucketTree(dict):
  
      def __init__(self, splitfunc, name='root'):
          dict.__init__(self)
          self.splitfunc = splitfunc
          self.name = name
          self.leafs = []
          self.bucket = collections.defaultdict(list)
  
      def grow(self, item, key):
          if key is None:
              self.leafs.append(item)
              return
          split = self.splitfunc(key)
          if len(split) == 2:
              head, tail = split 
          else:
              head, tail = split[0], None
          if head not in self:
              self[head] = BucketTree(self.splitfunc, head)
          self[head].grow(item, tail)
  
      def fill(self, item, key):
          if key is None:
              self.bucket[None].append(item)
              return
          split = self.splitfunc(key)
          if len(split) == 2:
              head, tail = split
          else:
              head, tail = split[0], None
          if head not in self:
              self.bucket[head].append(item)
          else:
              self[head].fill(item, tail)
  
  def get_all_urls(company):
      if company.homepage_url is not None and len(company.homepage_url) > 0:
          yield company.homepage_url
      if len(company.external_links) > 0:
          for link in company.external_links:
              yield link
  
  def companies_grown_tree(comps=None):
      if comps is None:
          comps = md_stats.all_companies()
      tree = BucketTree()
      for c in comps:
          for url in get_all_urls(c):
              tree.grow(c, md_tools.netloc(url).lower())
      return tree
  
subject: build tree from companies