BK-Trees implementation in C++.

BK-Trees, or Burkhard-Keller Trees are a tree-based data structure engineered for quickly finding near-matches to a string, for example, as used by a spelling checker, or when doing a ‘fuzzy’ search for a term. The aim is to return, for example, “seek” and “peek” if I search for “aeek”. What makes BK-Trees so cool is that they take a problem which has no obvious solution besides brute-force search, and present a simple and elegant method for speeding up searches substantially.

#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <fstream>  
#include <iostream>  
#include <sstream>
#include <iterator>
#include <streambuf>

class BkTree {
public:
	BkTree();
	~BkTree();
	void insert(std::string m_item);
	int getWithinDistance(std::string center, size_t k, std::vector<std::string> *result = NULL);
private:
	size_t EditDistance( const std::string &s, const std::string &t );
	struct Node {
		std::string m_item;
		size_t m_distToParent;
		Node *m_firstChild;
		Node *m_nextSibling;
		Node(std::string x, size_t dist);
		~Node();
	};
	Node *m_root;
	int   m_size;
protected:
};
BkTree::BkTree() {
	m_root = NULL; 
	m_size = 0;
}
BkTree::~BkTree() { 
	if( m_root ) 
		delete m_root; 
}
BkTree::Node::Node(std::string x, size_t dist) {
	m_item         = x;
	m_distToParent = dist;
	m_firstChild   = m_nextSibling = NULL;
}
BkTree::Node::~Node() {
	if( m_firstChild ) 
		delete m_firstChild;
	if( m_nextSibling ) 
		delete m_nextSibling;
}
void BkTree::insert(std::string m_item) {
	if( !m_root ){
		m_size = 1;
		m_root = new Node(m_item, -1);
		return;
	}
	Node *t = m_root;
	while( true ) {
		size_t d = EditDistance( t->m_item, m_item );
		if( !d ) 
			return;
		Node *ch = t->m_firstChild;
		while( ch ) {
			if( ch->m_distToParent == d ) { 
				t = ch; 
				break; 
			}
			ch = ch->m_nextSibling;
		}
		if( !ch ) {
			Node *newChild = new Node(m_item, d);
			newChild->m_nextSibling = t->m_firstChild;
			t->m_firstChild = newChild;
			m_size++;
			break;
		}
	}
}
size_t BkTree::EditDistance( const std::string &left, const std::string &right ) {
	size_t asize = left.size();
	size_t bsize = right.size();
	std::vector<size_t> prevrow(asize+1);
	std::vector<size_t> thisrow(bsize+1);
	for(size_t i = 0; i <= bsize; i++)
		prevrow[i] = i;
	for(size_t i = 1; i <= asize; i ++) {
		thisrow[0] = i;
		for(size_t j = 1; j <= bsize; j++) {
			thisrow[j] = std::min(prevrow[j-1] + size_t(left[i-1] != right[j-1]), 
				1 + std::min(prevrow[j],thisrow[j-1]) );
		}
		std::swap(thisrow,prevrow);
	}
	return prevrow[bsize];
}
int BkTree::getWithinDistance(std::string center, size_t k, std::vector<std::string> *result) {
	if( !m_root ) return 0;
	int found = 0;
	std::queue< Node* > q;
	q.push( m_root );
	while( !q.empty() ) {
		Node *t = q.front(); 
		q.pop();
		size_t d = EditDistance( t->m_item, center );
		if( d <= k ) { 
			result->push_back( t->m_item );
			found++; 
		}
		Node *ch = t->m_firstChild;
		while( ch ) {
			if( d - k <= ch->m_distToParent && ch->m_distToParent <= d + k )
				q.push(ch);
			ch = ch->m_nextSibling;
		}
	}
	return found;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s