Tree (Data Structure)
Also known as: Tree
Hierarchical data structure of nodes connected by edges, with a single root and no cycles, used for file systems, indexes and parsers.
A tree organizes data as a hierarchy. It consists of nodes connected by links, with one node designated the root and every other node having exactly one parent; nodes with no children are called leaves. Unlike a general graph, a tree contains no cycles, so there is exactly one path between any two nodes. A family tree, an organization chart and the folders on a computer's disk are familiar examples. Each node and its descendants form a subtree, which is why algorithms on trees are often naturally written with recursion.
Many specialized trees exist. In a binary search tree, each node has at most two children, with smaller keys to the left and larger keys to the right, so search takes time proportional to the tree's height. Self-balancing variants such as AVL trees and red-black trees keep that height logarithmic as items are added and removed. B-trees, introduced by Rudolf Bayer and Edward McCreight in the early 1970s, store many keys per node to minimize slow disk reads and are the standard index structure in databases and file systems. Tries store strings character by character for fast prefix searches.
Trees are everywhere in computing. Browsers represent a web page as a tree called the Document Object Model; compilers parse source code into abstract syntax trees; and version control systems such as Git record directory snapshots as trees of hashed objects, an idea related to the Merkle tree used in blockchains. Decision trees are a basic tool in machine learning. Choosing the right kind of tree, and keeping it balanced, often determines whether an operation on millions of records takes microseconds or minutes.
Key points
- A tree is a hierarchy of nodes with a single root, in which every other node has exactly one parent.
- Binary search trees support fast ordered lookups, and self-balancing variants keep their height logarithmic.
- B-trees, designed for disk storage, are the standard index structure in databases and file systems.
- Web pages, parsed source code and file systems are all commonly represented as trees.
Related entries
Data Structure
Way of organizing and storing data in a computer so that it can be accessed and modified efficiently, such as an array, list, tree or hash table.
Graph Theory
Branch of mathematics that studies networks of nodes connected by edges, used to model relationships from road maps to social networks.
Recursion
Technique in which a function solves a problem by calling itself on smaller instances of the same problem until it reaches a simple base case.
Database
Organized collection of structured data stored electronically and managed by software that lets users store, query and update it reliably.
Document Object Model
Programming interface that represents a web page as a tree of objects, letting scripts read and change its content, structure and style.
Merkle Tree
Tree data structure in which each non-leaf node holds a hash of its children, enabling efficient and secure verification of large data sets.
Hash Table
Data structure that stores key-value pairs and uses a hash function to compute where each item lives, giving very fast average lookups.
Big O Notation
Mathematical notation describing how an algorithm's running time or memory use grows as its input size grows, ignoring constant factors.
Referenced by
Other entries that discuss or point to Tree (Data Structure).