What is a Tree?

Data structures, amongst other things, are used to store and organize data. Different types of data are more efficiently organized using particular data structures.

Trees are non-linear data structures which are quite often used to represent hierarchical data such as the employee organizational structure in a company.

svg viewer

Structure of a Tree:

A collection of entities called nodes (represented as circles in our diagrams) are linked together with an edge to simulate a hierarchical structure. Each node contains data that can be of any type as long as all the nodes contain the same type of data.

Components of a Tree:

Root: A node with no incoming link to another node, i.e. no parent.

svg viewer
svg viewer

Child: A node with an incoming link from an upper node, i.e. a parent node. A child can only have one parent.

Parent: A node with an outgoing link to a lower node, i.e. a child node. A parent can have multiple children.

Siblings: Children nodes with the same parent.

svg viewer
svg viewer

Leaf: A node with no outgoing link to a lower node, i.e. no child node.

Depth: The depth of a node, ‘x’, is the number of edges (links) in the path from the root node to the node ‘x’. In the example on the right, the depth of node ‘x’ is 2.

svg viewer