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)

Binary trees


Generally speaking, each node in a basic tree can contain any number of children. However, in the case of binary trees, a node cannot contain more than two children. It means that it can contain zero, one, or two child nodes. Such a requirement has an important impact on the shape of a binary tree, as shown in the following two diagrams presenting binary trees:

As already mentioned, a node in a binary tree can contain at most two children. For this reason, they are referred to as the left child and right child. In the case of the binary tree shown on the left-hand side of the preceding diagram, node 21 has two children, 68 as the left child and 12 as the right child, while node 100 has only a left child.

Have you thought about how you can iterate through all the nodes in a tree? How can you specify an order of nodes during traversal of a tree? There are three common approaches: pre-order, in-order, and post-order, as shown in the following diagram:

As you can see in the diagram...