Trees
Tree Elements

Tree Elements

Overview

Finally, we are looking at Trees!

A tree data structure is a form of organizing elements in a hierarchical order, where nodes represent individual elements, and edges connect them and define their relationships. The first node at the top is known as the root, and each node can have multiple child nodes, forming branching structures (just like a normal tree, hence the name Tree). Trees are essential in computer science for tasks like representing hierarchical data, organizing information efficiently, and enabling various search and traversal algorithms. In less abstract words - file systems for directories and subdirectories, databases, browsers (DOM - Document Object Model) and many other places.

Again similarly to a real-world tree, a data structure tree has different elements which are presented in the animation and here's their explanation too:

Root: The root node is "A" which is the topmost node in the tree. It serves as the starting point for traversing the tree.

Edges: The edges are the connections between the nodes. In the tree you see in the animation, the edges are represented by the lines connecting the nodes. For example, there is an edge between "A" and "B," between "A" and "C," between "B" and "D," between "B" and "E" and so on.

Parent and Child: "A" is the parent of nodes "B" and "C." "B" is the parent of nodes "D" and "E." "C" is the parent of nodes "F" and "G." The nodes "B" "C" "D" "E" "F" and "G" are children of their respective parent nodes.

Subtrees: Subtrees are smaller trees that originate from a specific node and include all its descendants. In this tree, we can have subtrees rooted at "B" (B-D-E), "C" (C-F-G), "D" (a single node D), "E" (a single node E), "F" (a single node F), and "G" (a single node G).

Siblings: Nodes that share the same parent are called siblings. In this tree, "B" and "C" are siblings because they have the same parent, "A." Similarly, "D" and "E" are siblings, and "F" and "G" are siblings.

Leaf Nodes: Leaf nodes are nodes that do not have any children; they are the endpoints of the tree branches. In this tree, the leaf nodes are "D" "E" "," and "G" because they do not have any children.

Depth: How many edges are present between the root and and any given node.

Height: The height of a tree is the length of the longest path from the root node to a leaf node. In this tree, the height is 2 because the longest path from the root "A" to a leaf node is "A-C-F" or "A-B-E."

In conclusion, the tree data structure is built upon interconnected nodes. At the base we have the only standalone node - the root, while every node establishes parent-child relationships with others. Subtrees are self-contained mini-trees nestled within the larger structure, siblings share a common parent, leaf nodes have no children, and the height represents the longest path from the root to a leaf node.