From the course: C# Algorithms

Unlock the full course today

Join today to access over 24,600 courses taught by industry experts.

Using recursion to implement tree traversals in C#

Using recursion to implement tree traversals in C# - C# Tutorial

From the course: C# Algorithms

Using recursion to implement tree traversals in C#

- [Instructor] And a tree traversal algorithm each node in a tree is accessed in a particular order. With the pre-order traversal, we visit the root first. With the in order traversal, we recursively visit the left tree and then the root. With the post order traversal, we visit the root last. With each traversal, we apply the same pattern to parts of the tree. Then we continue that pattern throughout the tree. The concept of recursion can be a useful tool in this type of algorithm, because we're doing the same operation with a slight modification to the input. Let's look at how we can apply this to tree traversal algorithms. We have some sample code here that we've carried over from the previous videos, with a few modifications. There is a node class, as well as a binary tree class. The binary tree class will hold our traversal. In the main function we create a small binary tree. Note it's not a binary search tree because it does not meet the ordering constraint. Let's traverse our…

Contents