Understanding and Implementing Trees

Our exploration of data structures has so far touched the linear parts only. Whether we used arrays, linked lists, stacks, or queues, all are linear data structures. We have seen the complexities of linear data structure operations, and most of the time, the insertion and deletion can be performed with O(1) complexity. However, the searching is a little complicated and takes O(n) complexity. The only exception is a PHP array, which, in fact, works as a hash table and can be searched in O(1) if the index or keys are managed in such a way. In order to solve this problem, we can use a hierarchical data structure instead of the linear one. Hierarchical data can solve many issues that a linear data structure cannot solve easily. Whenever we are talking about family tree, organization structure, and network connectivity diagrams, we are actually talking about hierarchical data. Trees are a special Abstract Data Type (ADT) that represents hierarchical data. Unlike a linked list, which is also an ADT, trees are hierarchical compared to the linear nature of linked lists. In this chapter, we will explore the world of trees. A perfect example of a tree structure can be a family tree, just like the following image:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset