Recursive algorithm in data structure