Trees concept

Trees represent one of the most important types of data structures in computing. They can be implemented in virtually any programming language.

The structure of a tree defines an arrangement of hierarchically arranged elements. The image below illustrates the representation of a tree, where you can see the analogy of the term used for the structure.

arvores
arvores

It is very useful in many real-world applications, such as directory structures in an operating system, graphical interfaces, databases and websites. See below two examples.

arvores

A tree shaping relationships in a social network

arvores

A tree modeling the folder structure in an operating system


Structure of a tree

Every tree is made up of two basic elements called:

  • nodes.
  • edges, that interconnect these nodes.

See the illustration in the image below.

arvores

Tree nodes represent spaces where various types of information can be stored, such as a telephone, a date, a name, a mailing list, a profile of a user on a social network, to name a few.

The edges represent relationships between these nodes. They represent the way nodes connect, and this relationship depends entirely on how the tree is used and the type of information the tree nodes store. For example:

  • in a tree that shapes relationships in a social network, the edges can represent degrees of friendship.
  • in a tree that shapes the folder structure of an operating system, edges can represent degrees of belonging (which folders belong to which other folders).

The big difference from a tree to other common data structures (such as arrays, lists, queues, and stacks) is that trees are not organized in a sequence. In a tree we cannot determine the next element and the previous element because its information is not organized in a queued manner.

See the illustration below for this difference.

arvores

In a list, each information is related to two other elements: the previous and the next

arvores

In a tree, each information (node) can be related to more than two elements

For this reason, trees are called nonlinear structures. And this makes it a very special structure with many different properties and characteristics.


Hierarchical Properties of a Tree

Root node

Every tree has one main node called root. The root represents the starting node of all tree types.

arvores

Child nodes and parents nodes

From the root, all lower nodes attached to a higher node are called child nodes. All higher nodes, which generate lower nodes, are called parent nodes.

arvores
arvores

Leaf node

Nodes that have no children are called leaf nodes.

arvores

The term “leaf node” alludes to the leaves of a tree, as they represent the end of the branch. The image below illustrates this metaphor.

arvores

Sibling nodes

Another important concept is the concept of brotherhood between nodes. Two nodes are siblings if they descend directly from the same parent node. See the examples in the illustrations below.

arvores
arvores

Degrees and order of a tree

“Degree” is the property that qualifies the nodes of a tree, it defines the number of children each node of a tree has. See the illustrations below.

arvores

“Order” is a concept that qualifies the entire tree. The order of a tree is defined by its highest degree node. Eg if the tree’s highest degree is 3, then the tree has order 3. See below for some illustrations.

arvores

Árvore de ordem 2

arvores

Árvore de ordem 3

arvores

Árvore de ordem 4


Ancestor and descendant

“Ancestor” is a concept that qualifies higher nodes than lower ones. When there is a set of edges that connect an upper node A to a lower node B, we say that A is ancestor of B.

arvores

“Descendant” is a concept that qualifies lower nodes in relation to higher nodes. When there is a set of edges that connect a lower node B to an upper node A, we say that B is descended from A.

arvores

Depth of a node

Depth is a concept that qualifies a node of a tree. It defines the distance from a given node to the root.

arvores

Level of a tree

The level defines the depth slices of a tree, so that the children of a node are always placed at later levels.

arvores

Height of a tree

Represents the largest depth of the tree. Represents the distance between the root and a leaf node of the highest level of the tree.

arvores

Subtree Concept

From the root, each new child represents a subtree, a new tree that recursively resets itself.

arvores

Computational representation

To computationally represent a tree, we need two structures: one to represent the tree nodes and one to represent the entire tree.

Representation of an order 2 tree (binary)

The code below presents a possible structural representation for a tree of order 2 (known as a binary tree). Note that in the node class (lines 7-18) we have two pointers (lines 9-10), one for the left child and one for the right child.

/* Class that represents binary Tree*/
public class BinaryTree {
    Node root; // pointer to the root node
}

/* Class representing tree nodes*/
public class Node {
    int value; // value stored in node
    Node left; // pointer to the left child node
    Node right; // pointer to the right child node
    /* Node Constuctor*/
    Node(int value) {
        this.value = value;
        right = null;
        left = null;
    }
}

Representation of an order 4 tree (quadtree)

The code below presents a possible structural representation for a tree of order 4 (known as quadtree). Note that in the node class (lines 7-16) we have a pointer array (line 9) rather than two left and right pointers.

/* Class that represents QuadTree*/
public class QuadTree {
    Node root; // pointer to the root node
}

/* Class representing tree nodes*/
public class Node {
    int value; // value stored in node
    Node[] children; // array of pointers to child nodes
    /* Node Constuctor*/
    Node(int value) {
        this.value = value;
        children = new Node[4]; // initialize the array of pointers
    }
}

The codes shown above represent only the structures of trees. In addition to structural representation, trees have several operations, such as the basic operations of: insertion, removal, search, among others.

autor

David Santiago

Master in Systems and Computing. Graduated in Information Systems. Professor of Programming Language, Algorithms, Data Structures and Development of Digital Games.

Outros artigos