Tree

Introduction

A tree is a data structure that is organized in a hierarchy. Each element of the tree data structure is called a node. The first node of the tree is called the root node. Each node in a tree (except the root) has a parent node and zero or more child nodes. In the case of the last level of nodes, they have no child. They are called leaf nodes. The tree is the most appropriate data structure to store hierarchical records.

Note: There are many types of trees such as binary trees, red-black trees, AVL trees, and so on.

Binary tree

A binary tree is a type of tree in which each node has at most two children ( 0, 1, or 2). They are referred to as left and right children, respectively.

Each node in the binary tree contains the following three components:

  • Data element

  • Pointer to left subtree

  • Pointer to right subtree

An example of a binary tree is given below:

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.