Book Image

C# Data Structures and Algorithms

By : Marcin Jamro
Book Image

C# Data Structures and Algorithms

By: Marcin Jamro

Overview of this book

Data structures allow organizing data efficiently. They are critical to various problems and their suitable implementation can provide a complete solution that acts like reusable code. In this book, you will learn how to use various data structures while developing in the C# language as well as how to implement some of the most common algorithms used with such data structures. At the beginning, you will get to know arrays, lists, dictionaries, and sets together with real-world examples of your application. Then, you will learn how to create and use stacks and queues. In the following part of the book, the more complex data structures will be introduced, namely trees and graphs, together with some algorithms for searching the shortest path in a graph. We will also discuss how to organize the code in a manageable, consistent, and extendable way. By the end of the book,you will learn how to build components that are easy to understand, debug, and use in different applications.
Table of Contents (14 chapters)

Hash sets


In some algorithms, it is necessary to perform operations on sets with various data. However, what is a set? A set is a collection of distinct objects without duplicated elements and without a particular order. Therefore, you can only get to know whether a given element is in the set or not. The sets are strictly connected with the mathematical models and operations, such as union, intersection, subtraction, and symmetric difference.

A set can store various data, such as integer or string values, as shown in the following diagram. Of course, you can also create a set with instances of a user-defined class, as well as add and remove elements from the set at any time.

Before seeing sets in action, it is a good idea to remind you of some basic operations that can be performed on two sets, named A and B. Let's start with the union and intersection, as shown in the following illustration. As you can see, the union (shown on the left as A∪B) is a set with all elements that belong to A...