Trees in Data Structure

Introduction to Tree Data Structure

    Difficulty Level : Easy
    Last Updated : 07 Feb, 2022

The data structure is a specialized method to organize and store data in the computer to be used more effectively. There are various types of data structures, such as stack, linked list, and queue, arranged in sequential order. For example, a linked data structure consists of a set of nodes that are linked together by links or points.  

Similarly, the tree data structure is a kind of hierarchal data arranged in a tree-like structure. It consists of a central node, structural nodes, and sub nodes, which are connected via edges. We can also say that tree data structure has roots, branches, and leaves connected with one another.  

A tree is non-linear and a hierarchical data structure consisting of a collection of nodes such that each node of the tree stores a value, a list of references to nodes (the “children”).

Recursive Definition: : A tree consists of a root, and zero or more subtrees T1, T2, … , Tk such that there is an edge from the root of the tree to the root of each subtree.

Basic Terminology In Tree Data Structure:

    Parent Node: The node which is a predecessor of a node is called the parent node of that node. {2} is the parent node of {6, 7}.
    Child Node: The node which is the immediate successor of a node is called the child node of that node. Examples: {6, 7} are the child nodes of {2}.
    Root Node: The topmost node of a tree or the node which does not have any parent node is called the root node. {1} is the root node of the tree. A non-empty tree must contain exactly one root node and exactly one path from the root to all other nodes of the tree.
    Degree of a Node: The total count of subtrees attached to that node is called the degree of the node. The degree of a leaf node must be 0. The degree of a tree is the degree of its root. The degree of the node {3} is 3.
    Leaf Node or External Node: The nodes which do not have any child nodes are called leaf nodes. {6, 14, 8, 9, 15, 16, 4, 11, 12, 17, 18, 19} are the leaf nodes of the tree.
    Ancestor of a Node: Any predecessor nodes on the path of the root to that node are called Ancestors of that node. {1, 2} are the parent nodes of the node {7}
    Descendant: Any successor node on the path from the leaf node to that node. {7, 14} are the descendants of the node. {2}.
    Sibling: Children of the same parent node are called siblings. {8, 9, 10} are called siblings.
    Depth of a node: The count of edges from the root to the node. Depth of node {14} is 3.
    Height of a node: The number of edges on the longest path from that node to a leaf. Height of node {3} is 2.
    Height of a tree: The height of a tree is the height of the root node i.e the count of edges from the root to the deepest node. The height of the above tree is 3.
    Level of a node: The count of edges on the path from the root node to that node. The root node has level 0.
    Internal node: A node with at least one child is called Internal Node.
    Neighbour of a Node: Parent or child nodes of that node are called neighbors of that node.
    Subtree: Any node of the tree along with its descendant

Let’s consider another example of tree data structure.

 

Here,

Node A is the root node

B is the parent of D and E

D and E are the siblings

D, E, and F are the leaf nodes

A and B are the ancestors of E

Few examples on Tree Data Structure: A code to demonstrate few of the above terminologies has been described below:


Posted on by