Member-only story

Data Structures and Algorithms (DSA) using C# .NET Core — Binary Trees and Tree Traversal- IV

Hussain Patel
4 min readDec 31, 2023

--

Now that we have understood the basics of Tree & Binary Tree, the question is how we can read the data from each node, add a node, delete a node or search node (data) in a tree. To read data, we need to visit every node in the tree and this is called Tree Traversing. For e.g. in Fig.1 you want to display the data of each node in the tree, for that you need to visit each node and display the value.

Fig 1. Tree Traversal

In linear data structures like linked list, arrays, stacks and queues, the data is stored in sequential manner and there is only one way to read the data, but in case of Trees the data is organized in hierarchical manner and so you can traverse (visit) nodes in different ways.

Let see how we can read data in different ways from Tree in Fig.1

Starting from top (left to right): 1, 10, 4, 6, 8

Starting from bottom (left to right): 4, 6, 10, 8, 1

Note: Although we are reading the values from each node in the tree, we are not following the tree hierarchy — parent and child relationship. We can use the traversal methods that take into account the tree hierarchy i.e. the structure of tree — parent and child. For that lest consider we have a node class with data field to store data, a left node and a…

--

--

Hussain Patel
Hussain Patel

Written by Hussain Patel

Husband, Father , @theWorkingProgrammer, IOT, Photography

Responses (1)