Hey everyone,

So awhile back I built a program that contained a function to merge two databases (well SQLite ones that are read in as Lists). When the first database is read in, well the file, it's read into a Binary Search Tree, while the second is read into a List. The code will then go on to checking the BST for entries in the List. If a duplicate is found, the entry is removed from the List. Once that is all done, the BST is then converted to a sorted List, and merged with the second list that now contains no duplicated

Now I wanted to use a BST, because well I need to expand my knowledge for one, and I felt like it would make the comparison process a heck of a lot quicker.

Anyway here's where the error comes into play. Now I have run this program multiple times (well this part of the program) with no problems what so ever. However, today I went run it and everything looked good until I tried converting my BST back to a List. I get the following error "StackOverflowException was unhandled.

That's it. I have no clue what's causing it, and even more weird, this is the first time. After countless runs, I am now getting the error. Here's the code I use to convert the BST back to a List

public static void ConvertToList (BinaryTreeNode_ImageData node, ref List<ImageData> ImageData) //converts a BST to a List (sorted)
    //this is recursively called, so we ref ImageData, that way it's the same item all the way back

    if (node != null)
        ConvertToList(node.Left, ref ImageData);

        ImageData.Add(new ImageData());
        ImageData [ImageData.Count - 1].ImageLink = node.Data.ImageLink;
        ImageData [ImageData.Count - 1].Author = node.Data.Author;
        ImageData [ImageData.Count - 1].Width = node.Data.Width;
        ImageData [ImageData.Count - 1].Height = node.Data.Height;

        ConvertToList(node.Right, ref ImageData);

So anyone got any ideas? Am I really maxing out this List? It's only about 23,009 entries long (I swear I have worked with lists that contain more, but then again I could be wrong). If you wonder all this does is hold strings and ints

Any help would be appreciated, thanks

Oh I should mention this is running on a BackgroundWorker, so I guess maybe a thread could be involved

Recommended Answers

All 5 Replies

There is no need to pass ImageData as a reference, you're just making it use a more and more indirect way of accessing your list (objects are passed by reference already).

That's also a ... unique way to add your data. The rest of us create object, populate their fields then add them to the list.

Naming a variable the same as it's class is confusing, don't do it.

That said, I suspect you have a loop in your tree where a node either references itself, or one of its ancestors so you keep looping through the same objects over and over again (or it's the ref-to-a-ref-to-a-ref-to-a-ref chain you are building).

Well see I originally store the data in a SQLite database, and I read them in to merge. So I read them into a BST to allow for easier searching for duplicates, but writing back to file I put into a list.

Yeah I do program weird sometimes.

I'll have to look into what you have said, see if I can find a fix

You could try using a Hashtable rather than a BST. You'd need to override the GetHashCode of your ImageData class so that it uses the various properties to make the hash. The value part of the hashtable could be a list of your ImageData objects, then you can iterate through each list that contains more than one item and ensure that the contents are or are not different.

If you use the default GetHashCode, this method won't work.

commented: Got me started in the right direction +3

Hmm that sounds like a good idea, I'll have to check that out.

Well after some research I ended up using a Dictionary<> variable. It not only fixed the overflow issue, it was also a lot quicker then using a BST in all ways.

(I used the ImageLink variable as the key, and the whole object was the value)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.