Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) IsBinaryNewick

 

IsBinaryNewick is a Newick code snippet to determine if the Newick is a binary tree.

 


///IsBinaryNewick checks if a Newick is a binary tree,
///that is: each node splits in two (not more) branches
///From http://www.richelbilderbeek.nl/CppIsBinaryNewick.htm
bool IsBinaryNewick(const std::vector<int>& v)
{
  assert(IsNewick(v));
  const int sz = boost::numeric_cast<int>(v.size());
  if (sz == 3) return false;
  if (sz == 4) return true;

  //'sz - 3' because i looks forward 2 indices and last index is a ')'
  for (int i=1; i!=sz-3; ++i)
  {
    if (v[i]>0 && v[i+1]>0 && v[i+2]>0) return false;
  }
  return true;
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict