0

I have a file like this

A 100 200
A 120 220
B 140 250

Another file is like this

A 130 210
A 133 215
B 180 270

Then I have to compare each row from first file to each row of second file and find which rows have intersecting co-ordinates

Output will be like this

A 100 200 A 130 210
A 100 200 A 133 215
A 100 200 A 180 270

and it follows like that.

In my code it code like this I get a first row from first file and compare with all row of second file.

So I want to know how can I implement a tree like data structure to do this, so that complexity will be of log scale.

Toto
  • 89,455
  • 62
  • 89
  • 125
dissw.geek9
  • 245
  • 1
  • 6
  • 11

1 Answers1

3

You don't need a tree-like data structure. If your files are sorted by the first coordinate you can do it easily in linear time. Just maintain a buffer for each file where you hold the currently relevant lines, and you just need to advance the buffer in sync for both files. The point is, that lines that are relevant for intersection with a given line of the other file will always be adjacent to each other, so you know that once you discard a line you don't have to check it anymore (since the file is sorted).

Bitwise
  • 7,577
  • 6
  • 33
  • 50
  • Can you please explain how can load the file into buffer and compare from there ? -Thanks – dissw.geek9 Oct 25 '12 at 20:20
  • @dissw.geek9 the buffer can simply be an array of rows, where you read rows from the file and add to the end of the array, while you always discard from the start of the array. This is similar to a queue or FIFO data structure. – Bitwise Oct 25 '12 at 20:24