Why we need tree data structure?

Welcome back folks. welcome-back-minions.gif

Overview

Today, we will check out why we actually need tree data structure when we have lots of data available. Why data structure like array, linked list, and other linear data structure fails with big data?

So, lets start understanding the Tree Data Structure.

What is Tree Data Structure?

To the simplest, Tree is a non-linear or a hierarchical data structure having a nodes connected with the edges of the other nodes, i.e. collection of nodes. Nodes in the tree contains the data that we need to store.

Whiteboard (4).png

Why we need Tree Data Structure?

So, the main discussion starts here, why we need tree data structure if we have arrays, linked list and other linear data structure. What makes tree data structure special from arrays and linked list when comes to fast searching and fast insertion.

1. Slow insertion in Ordered Array

When we want to insert an element in an ordered array we need to create a room for the element and shift all the higher elements one space up. This approach takes a lot of time as it first search an index and then shifts the element. But using the binary search, searching is easy in Ordered Array.

2. Slow searching in Linked List

Searching in a Linked List takes a lot time as it iterate each and every element and then compare it with the search key. Where as insertion and deletion in Linked List is much faster than the other data structure.

So, we need a data structure that power ups the advantage of both these data structure. Tree came into the rescue. It perform the insertion and deletion as fast as linked list and done searching as quick as ordered array.

Conclusion

Usually, because it combines the advantages of two other data structures, i.e. Ordered Array and Linked List. You can search a tree quickly, as you can in an ordered array, and you can also insert and delete items quickly, as you can with a linked list.

Reference

Data Structure and Algorithms in Java by Robert Lafore

Subscribe to the newsletter to read more blogs on data structures and algorithms!

Did you find this article valuable?

Support Abhishek Vaish by becoming a sponsor. Any amount is appreciated!