Scott on Writing

Musings on technical writing...

An Extensive Examination of Data Structures - Updated for 2.0

Starting back in November 2003 I wrote a six-part article series for the C# section on MSDN Online titled, An Extensive Examination of Data Structures.  This article series examined a number of the built-in data structures in the .NET Framework - arrays, the ArrayList, the Hashtable, the Queue, and the Stack - as well as the fundamentals and working examples of other common data structures - a binary search tree, a SkipList, a Graph, and a Set.

A few months ago I started retooling these articles for the .NET Framework version 2.0, and the six parts of the original article series have been updated and are now published on MSDN Online:

  • Part 1: An Introduction to Data Sturctures - this first part starts by looking at the fundamentals of data structures and then examines two of the most commonly used data structures - the array and the List (the List being a new data structure in 2.0 that is, essentially, a Generics version of the ArrayList).  This article series also includes a discussion on what Generics are and their benefits.
    • New to the 2.0 Series - discussion on generics; discussion of the built-in List data structure.
  • Part 2: The Queue, Stack, and Hashtable - this second part starts with an examination of the Queue and Stacks, two data structures that allow for a collection of items to be stored, but place limits on the order with which items are added and removed.  The second half of the article looks at the Hashtable and Dictionary objects, which provide a way to store items using key/value semantics.
    • New to the 2.0 Series - the generic versions of the Queue and Stack are used in the discussion; the Dictionary class, new to 2.0, is discussed.
  • Part 3: Binary Trees and BSTs - the third part of this article series is the first that looks at data structures not found in the .NET Framework BCL.  Trees are discussed, and implementations of a binary tree class and a binary search tree class are provided.
    • New to the 2.0 Series - the binary tree and BST classes use Generics to allow for type-safe trees; used the yield syntax to provide iterators for the tree data.
  • Part 4: Building a Better Binary Search Tree - BSTs have some shortcomings in that the order with which the data was inserted into the tree can greatly affect its running time.  To combat this phenomenon, computer scientists have created various data structures to circumvent the problem.  One technique is to have the trees automatically self-balance on insert.  Another technique is the SkipList, which maintains a pseudo-tree with randomly assigned heights following a particular distribution that affords the optimal big-O running time.
    • New to the 2.0 Series - the SkipList is now type-safe, thanks to Generics.
  • Part 5: From Trees to Graphs - graphs are a more generalized version of a tree.  In this fifth part of the article series, the concepts of graphs are discussed and a Graph class is introduced.
    • New to the 2.0 Series - the Graph class uses Generics; the Graph class has been refactored into a better design.
  • Part 6: Efficiently Representing Sets - a set is a collection of unordered items, and is a construct that's commonly used in mathematics.  There are a number of real-world computer science problems that need to be able to efficiently work with sets, yet there is no support for sets in the .NET Framework BCL.  This sixth and final part of the article series examines a set class and provides a discussion on a particular set scenario: maintaining a collection of disjoint sets.

 

posted on Thursday, February 17, 2005 8:37 AM

Feedback

# Examining Data Structures in .NET Framework 2.0 2/17/2005 9:33 AM miguel jimenez's coding blog

# re: An Extensive Examination of Data Structures - Updated for 2.0 2/18/2005 5:33 AM BigJim in STL

Scott

What an excellent set of articles - this should be required reading for anyone doing .NET development. I thought I was up to snuff on things like stacks and array lists, but these articles helped in understanding some of the more complicated scenarios.

I especially liked the generics stuff- I had previously tried to explain these to my manager as one of the benefits of ASP.NET 2.0, but couldn't quite phrase it the right way. Once he saw your 1st article, he saw what I was trying to tell him.

Thanks again!

# An Extensive Examination of Data Structures - Updated for 2.0 2/18/2005 11:23 AM andrew connell

# re: An Extensive Examination of Data Structures - Updated for 2.0 2/23/2005 1:11 PM Eric

Great articles!

Why not write a whole book on the subject of Data Structures in C#? This is one reason why colleges don't want to use C# over Java - there's a lot of Java books on Data Structures.

# FW: An Extensive Examination of Data Structures - Updated for 2.0 6/19/2005 10:13 PM Lance's Whiteboard

# re: An Extensive Examination of Data Structures - Updated for 2.0 1/9/2006 2:03 AM Paolo

The sample does NOT work in VS 2005.

# re: An Extensive Examination of Data Structures - Updated for 2.0 2/21/2006 3:14 AM Matthieu

I cann't even get the sample to compile.

# re: An Extensive Examination of Data Structures - Updated for 2.0 11/25/2006 11:12 AM Mohsin

A very nice and helpful article "An Extensive Examination of Data Structures Using C# 2.0" by you at MSDN http://msdn2.microsoft.com/en-us/library/ms364091(VS.80).aspx , while I was reading through it in Part 3: Binary Tree and BSTs http://msdn2.microsoft.com/en-us/library/ms379572(vs.80).aspx . I encountered an error in the following code:

public class NodeList<T> : Collection<Node<T>>
{
public NodeList() : base() { }

public NodeList(int initialSize)
{
// Add the specified number of items
for (int i = 0; i < initialSize; i++)
base.Items.Add(default(Node<T>));
}

public Node<T> FindByValue(T value)
{
// search the list for the value
foreach (Node<T> node in Items)
if (node.Value.Equals(value))
return node;

// if we reached here, we didn't find a matching node
return null;
}
}

The error is that there is no "Collection" class in 'System.Collections.Generics' namespace, but there is an Interface with the name "ICollection" and as you say:

The NodeList class contains a strongly-typed collection of Node<T> instances. As the following code shows, the NodeList class is derived from the Collection<T> class in the System.Collections.Generics namespace. The Collection<T> class provides the base functionality for a strong-typed collection, with methods like Add(T) , Remove(T), and Clear(), and properties like Count and a default indexer. In addition to the methods and properties inherited from Collection<T>, NodeList provides a constructor that creates a specified number of nodes in the collection, and a method that searches the collection for an element of a particular value.

Also, I downloaded the complete code (msi package) and installed it but when I try to compile it, it gives me following error:

"Error 1 The type or namespace name 'Collection' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\...\My Documents\MSDN\DataStructures20\skmDataStructures2\NodeList.cs 15 32 skmDataStructures2"

Which validates the mistake made in the article and in the coding part as well. I hope that you can correct it as soon as possible. You can let me know if there are any updates available or if the problem has already been solved. Thanks in advance.

Regards,
Mohsin.

# re: An Extensive Examination of Data Structures - Updated for 2.0 11/25/2006 2:56 PM Scott Mitchell

Mohsin, the code was written for the Beta 2 version of the .NET Framework version 2.0. I've not had the time to update the code/article for the RTM version, I'm afraid.

# re: An Extensive Examination of Data Structures - Updated for 2.0 3/15/2007 1:45 AM Atag

I have the same problem when I use this code. Mohsin and others, have you solved this problem? (I'm a newbie in C#)

# re: An Extensive Examination of Data Structures - Updated for 2.0 10/8/2007 9:08 AM sa

see waltergr's post at
http://www.codeguru.com/forum/archive/index.php/t-412170.html

to fix the problem:

1. In NodeList.cs, add "using System.Collections.ObjectModel;"

2. In SkipList.cs, BinarySearchTree.cs, and Graph.cs, find the implementation of public IEnumerator<T> GetEnumerator() and add this below it:

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

3. Remove lines in *Tester\Program.cs that say "Application.EnableRTLMirroring();"



Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

Add To Your Reader

My Links

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<May 2008>
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Comment Stats

DayTotal% of Total
Sunday 1866.8%
Monday 37913.9%
Tuesday 45316.7%
Wednesday 50418.5%
Thursday 53519.7%
Friday 49418.2%
Saturday 1666.1%
Total 2717100.0%

Hour1Total% of Total
12:00 AM 652.4%
1:00 AM 682.5%
2:00 AM 622.3%
3:00 AM 742.7%
4:00 AM 572.1%
5:00 AM 1033.8%
6:00 AM 1084.0%
7:00 AM 1585.8%
8:00 AM 1716.3%
9:00 AM 1475.4%
10:00 AM 1716.3%
11:00 AM 1816.7%
12:00 PM 1886.9%
1:00 PM 1696.2%
2:00 PM 1605.9%
3:00 PM 1324.9%
4:00 PM 1073.9%
5:00 PM 923.4%
6:00 PM 913.3%
7:00 PM 963.5%
8:00 PM 833.1%
9:00 PM 782.9%
10:00 PM 792.9%
11:00 PM 772.8%
Total 2717100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.54144
Monday 5.22339
Tuesday 4.28419
Wednesday 7.67637
Thursday 6.90607
Friday 5.48411
Saturday 5.33160
Total 5.842717

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.35107
9:00 AM 6.32278
10:00 AM 6.47246
11:00 AM 4.41181
12:00 PM 6.88330
1:00 PM 3.00111
2:00 PM 5.41222
3:00 PM 8.64285
4:00 PM 4.0589
5:00 PM 5.92154
6:00 PM 4.52113
7:00 PM 9.67174
8:00 PM 9.80147
9:00 PM 5.05111
10:00 PM 5.4265
11:00 PM 4.5732
Total 5.842717

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles