This article uses HTML markup. (May 2020) 
In computer science, a tree is a widely used abstract data type (ADT) that simulates a hierarchical tree structure, with a root value and subtrees of children with a parent node, represented as a set of linked nodes.
A tree data structure can be defined recursively as a collection of nodes (starting at a root node), where each node is a data structure consisting of a value, together with a list of references to nodes (the "children"), with the constraints that no reference is duplicated, and none points to the root.
Alternatively, a tree can be defined abstractly as a whole (globally) as an ordered tree, with a value assigned to each node. Both these perspectives are useful: while a tree can be analyzed mathematically as a whole, when actually represented as a data structure it is usually represented and worked with separately by node (rather than as a set of nodes and an adjacency list of edges between nodes, as one may represent a digraph, for instance). For example, looking at a tree as a whole, one can talk about "the parent node" of a given node, but in general as a data structure a given node only contains the list of its children, but does not contain a reference to its parent (if any).
A tree is a nonlinear data structure, compared to arrays, linked lists, stacks and queues which are linear data structures. A tree can be empty with no nodes or a tree is a structure consisting of one node called the root and zero or one or more subtrees.
Mathematically, an unordered tree^{[1]} (or "algebraic tree"^{[2]}) can be defined as an algebraic structure where X is the nonempty carrier set of nodes and parent is a function on X which assigns each node x its "parent" node, parent(x). The structure is subject to the condition that every nonempty subalgebra must have the same fixed point. That is, there must be a unique "root" node r, such that parent(r) = r and for every node x, some iterative application parent(parent(...parent(x)...)) equals r.
There are several equivalent definitions. As the closest alternative, one can define unordered trees as partial algebras (X, parent) which are obtained from the total algebras described above by letting parent(r) be undefined. That is, the root r is the only node on which the parent function is not defined and for every node x, the root is reachable from x in the directed graph (X, parent). This definition is in fact coincident with that of an antiarborescence. The TAoCP book uses the term oriented tree.^{[3]}
An unordered tree is a structure (X, ?) where X is a set of nodes and ? is a childtoparent relation between nodes such that:  
(1)  X is nonempty. 

(2)  X is weakly connected in ?. 
(3)  ? is functional. 
(4)  ? satisfies ACC: there is no infinite sequence x_{1} ? x_{2} ? x_{3} ? ···. 
The box on the right contains what can be regarded as a canonical definition
of unordered trees. It just describes the partial algebra (X, parent) as a relational structure (X, ?). Moreover, dedicated terminology can be provided for generalizations of unordered trees that correspond to distinguished subsets of the listed conditions:
(3) – directed pseudoforest,
(4) – directed acyclic graph ^{[a]},
(3,4) – unordered forest
,
(1,2,3) – directed pseudotree.
Another equivalent definition of an unordered tree is that of a settheoretic tree that is singlyrooted and whose height is at most ? (a finiteish
tree^{[4]}). That is, the algebraic structures (X, parent) are equivalent to partial orders that have a top element r and whose every principal upset (aka principal filter) is a finite chain.
To be precise, we should speak about an inverse settheoretic tree since the settheoretic definition usually employs opposite ordering.
The correspondence between (X, parent) and (X, ≤) is established via reflexive transitive closure / reduction, with the reduction resulting in the "partial" version without the root cycle.
The definition of trees in descriptive set theory (DST) utilizes the representation of partial orders (X, ≥) as prefix orders between finite sequences. In turns out that up to isomorphism, there is a onetoone correspondence between the (inverse of) DST trees and the tree structures defined so far.
We can refer to the four equivalent characterizations as to tree as an algebra, tree as a partial algebra, tree as a partial order, and tree as a prefix order. There is also a fifth equivalent definition – that of a graphtheoretic rooted tree which is just a connected acyclic rooted graph.
The expression of trees as (partial) algebras (X, parent) follows directly the implementation of tree structures using parent pointers. Typically, the partial version is used in which the root node has no parent defined. However, in some implementations or models even the parent(r) = r circularity is established. Notable examples:
Note that the above definition admits infinite trees. This allows for the description of infinite structures supported by some implementations via lazy evaluation. A notable example is the infinite regress of eigenclasses from the Ruby object model.^{[9]}
In this model, the tree established via superclass
links between nonterminal objects is infinite and has an infinite branch (a single infinite branch of "helix" objects – see the diagram).
In every unordered tree (X, parent) there is a distinguished partition of the set X of nodes into sibling sets. Two nonroot nodes x, y belong to the same sibling set if parent(x) = parent(y). The root node r forms the singleton sibling set {r}. ^{[b]} A tree is said to be locally finite or finitely branching if each of its sibling sets is finite.
Each pair of distinct siblings is incomparable in ≤. This is why the word unordered is used in the definition. Such a terminology might become misleading when all sibling sets are singletons, i.e. when the set X of all nodes is totally ordered (and thus wellordered) by ≤. In such a case we might speak about a singlybranching tree instead.
As with every partially ordered set, tree structures (X, ≤) can be represented by inclusion order – by set systemss in which ≤ is coincident with ⊆, the induced inclusion order. Consider a structure (U, F) such that U is a nonempty set, and F is a set of subsets of U such that the following are satisfied (by Nested Set Collection definition):
Then the structure (F, ⊆) is an unordered tree whose root equals U. Conversely, if (U, ≤) is an unordered tree, and F is the set {↓x  x ∈ U} of all principal ideals of (U, ≤) then the set system (U, F) satisfies the above properties.
The setsystem view of tree structures provides the default semantic model – in the majority of most popular cases, tree data structures represent containment hierarchy. This also offers a justification for order direction with the root at the top: The root node is a greater container than any other node. Notable examples:
An unordered tree (X, ≤) is wellfounded if the strict partial order < is a wellfounded relation. In particular, every finite tree is wellfounded. Assuming the axiom of dependent choice a tree is wellfounded if and only if it has no infinite branch.
Wellfounded trees can be defined recursively – by forming trees from a disjoint union of smaller trees. For the precise definition, suppose that X is a set of nodes. Using the reflexivity of partial orders, we can identify any tree (Y, ≤) on a subset of X with its partial order (≤) – a subset of X × X. The set R of all relations R that form a wellfounded tree (Y, R) on a subset Y of X is defined in stages R_{i}, so that R = ?{R_{i}  i is ordinal}. For each ordinal number i, let R belong to the ith stage R_{i} if and only if R is equal to
where F is a subset of ?{R_{k}  k < i} such that elements of F are pairwise disjoint, and x is a node that does not belong to dom(?F). (We use dom(S) to denote the domain of a relation S.) Observe that the lowest stage R_{0} consists of singlenode trees {(x,x)} since only empty F is possible. In each stage, (possibly) new trees R are built by taking a forest ?F with components F from lower stages and attaching a new root x atop of ?F.
In contrast to the tree height which is at most ω, the rank of wellfounded trees is unlimited^{[11]}, see the properties of "unfolding".
In computing, a common way to define wellfounded trees is via recursive ordered pairs
(F, x):
a tree is a forest F together with a fresh
node x.^{[12]}
A forest F in turn is a possibly empty set of trees with pairwise disjoint sets of nodes.
For the precise definition, proceed similarly as in the construction of names used in the settheoretic technique of forcing.
Let X be a set of nodes. In the superstructure over X,
define sets T, F of trees and forests, respectively, and a map nodes : T → ?(X)
assigning each tree t its underlying set of nodes
so that
(trees over X)  t ∈ T  ↔  t is a pair (F, x) from F × X such that for all s ∈ F, x ∉ nodes(s),  
(forests over X)  F ∈ F  ↔  F is a subset of T such that for every s,t ∈ F, s ≠ t, nodes(s) ∩ nodes(t) = ∅ ,  
(nodes of trees)  y ∈ nodes(t)  ↔  t = (F, x) ∈ T and either y = x or y ∈ nodes(s) for some s ∈ F . 
Circularities in the above conditions can be eliminated by stratifying each of T, F and nodes into stages like in the previous subsection.
Subsequently, define a subtree
relation ≤ on T as the reflexive transitive closure of the immediate subtree
relation ? defined between trees by
s ? t  ↔  s ∈ ?_{1}(t) 
where ?_{1}(t) is the projection of t onto the first coordinate, i.e.
it is the forest F such that t = (F, x) for some x ∈ X.
It can be observed that (T, ≤) is a multitree: for every t ∈ T, the principal ideal ↓t ordered by ≤ is a wellfounded tree as a partial order. Moreover,
for every tree t ∈ T, its nodes
order structure (nodes(t), ≤_{t}) is given by x ≤_{t}y if and only if there are forests F, G ∈ F such that both (F, x) and (G, y) are subtrees of t and (F, x) ≤ (G, y).
Another formalization as well as generalization of unordered trees can be obtained by reifying parentchild pairs of nodes. Each such ordered pair can be regarded as an abstract entity – an arrow
.
This results in a multidigraph
(X, A, s, t)
where
X is the set of nodes,
A is the set of arrows,
and s and t
are functions from A to X assigning each arrow its source and target, respectively.
The structure is subject to the following conditions:
In (1), the composition symbol ? is to be interpreted lefttoright. The condition says that inverse consecutivity of arrows^{[c]} is a total childtoparent map. Let this parent map between arrows be denoted p, i.e. p = s ? t^{−1}. Then we also have s = p ? t, thus a multidigraph satisfying (1,2) can also be axiomatized as (X, A, p, t), with the parent map p instead of s as a definitory constituent. Observe that the root arrow is necessarily a loop, i.e. its source and target coincide.
An important generalization of the above structure is established by allowing the target map t to be manytoone. This means that (2) is weakened to
Note that condition (1) asserts that only leaf arrows are allowed to have the same target. That is, the restriction of t to the range of p is still injective.
Multidigraphs satisfying (1,2') can be called arrow trees
–
their tree characteristics is imposed on arrows rather than nodes.
These structures can be regarded as the most essential abstraction of the Linux VFS because they reflect the hardlink structure of filesystems. Nodes are called inodes, arrows are dentries (or hard links). The parent and target maps p and t are respectively represented by d_parent
and d_inode
fields in the dentry data structure.^{[13]}
Each inode is assigned a fixed file type, of which the
directory type plays a special role of designed parents
:
(a) only directory inodes can appear as hardlink source and
(b) a directory inode cannot appear as the target of more than one hardlink.
Using dashed style for the first half of the root loop indicates that, similarly to the parent map, there is a partial version for the source map s in which the source of the root arrow is undefined. This variant is employed for further generalization, see #Using paths in a multidigraph.
Unordered trees naturally arise by "unfolding" of accessible pointed graphs.^{[14]} Let R = (X, R, r) be a pointed relational structure, i.e. such that X is the set of nodes, R is a relation between nodes (a subset of X × X), and r is a distinguished "root" node. Assume further that R is accessible, which means that X equals the preimage of {r} under the reflexive transitive closure of R, and call such a structure an accessible pointed graph or apg for short.(?) Then one can derive another apg R' = (X', R', r') – the unfolding of R – as follows:
Apparently, the structure (X', R') is an unordered tree in the "partialalgebra" version: R' is a partial map that relates each nonroot element of X' to its parent by path popping. The root element is obviously r'. Moreover, the following properties are satisfied:
preimage, the word
imageapplies).^{[d]}
As shown on the example of hardlink structure of file systems, many data structures in computing allow multiple links between nodes.
Therefore,
in order to properly exhibit the appearance of unordered trees among data structures it is necessary to generalize accessible pointed graphs to multidigraph setting.
To simplify the terminology, we make use of the term quiver which is an established synonym for multidigraph
.
Let an accessible pointed quiver or apq for short
be defined as a structure
M = (X, A, s, t)
where
X is a set of nodes,
A is a set of arrows,
s is a partial function from A to X (the source map),
and t is a total function from A to X (the target map).
Thus, M is a partial multidigraph
.
The structure is subject to the following conditions:
rootarrow, a_{r}, whose source s(a_{r}) is undefined.
M is said to be a tree if the target map t is a bijection between arrows and nodes. The unfolding of M is formed by the sequences mentioned in (2) – which are the accessibility paths (cf. Path algebra). As an apq, the unfolding can be written as M' = (X', A', s', t') where X' is the set of accessibility paths, A' coincides with X', s' coincides with path popping, and t' is the identity on X'. Like with apgs, unfolding is idempotent and always results in a tree. The underlying apg is obtained as the structure (X, R, t(a_{r})) where R = {(t(a),s(a))  a ∈ A \ {a_{r}}}.
The diagram above shows an example of an apq with 1+14 arrows. In JavaScript, Python or Ruby, the structure can be created by the following (exactly the same) code:
r = {};
r[1] = {}; r[2] = r[1]; r[3] = {}; r[4] = {};
r[1][5] = {}; r[1][14] = r[1][5];
r[3][7] = {}; r[3][8] = r[3][7]; r[3][13] = {};
r[4][9] = r[4]; r[4][10] = r[4]; r[4][11] = {};
r[3][7][6] = r[3]; r[3][7][12] = r[1][5];
Unordered trees and their generalizations form the essence of naming systems. There are two prominent examples of naming systems: file systems and (nested) associative arrays. The multidigraphbased structures from previous subsections provided anonymous abstractions for both cases. To obtain naming capabilities, arrows are to be equipped with names as identifiers. A name must be locally unique – within each sibling set of arrows^{[e]} there can be at most one arrow labelled by a given name.
source

name

target

s(a)  σ(a)  t(a) 

This can be formalized as a structure E = (X, ?, A, s, σ, t) where X is a set of nodes, ? is a set of names, A is a set of arrows, s is a partial function from A to X, σ is a partial function from A to ?, and t is a total function from A to X. For an arrow a, constituents of the triple (s(a), σ(a), t(a)) are respectively a's source, name and target. The structure is subject to the following conditions.
This structure can be called a nested dictionary or named apq.
In computing, such structures are ubiquitous.
The table above shows that arrows can be considered unreified
as the set
A' = {(s(a), σ(a), t(a))  a ∈ A \ {a_{r}}}
of sourcenametarget triples.
This leads to a relational structure (X, ?, A')
which can be viewed as a
relational database table.
Underlines in
source
and
name
indicate primary key.
The structure can be rephrased as a
deterministic labelled transition system:
X is a set of states
, ? is a set of labels
, A' is a set of labelled transitions
.
(Moreover, the root node r = t(a_{r}) is an initial state
, and the accessibility condition means that every state is reachable from the initial state.)
The diagram on the right shows a nested dictionary E that has the same underlying multidigraph as the example in the previous subsection. The structure can be created by the code below. Like before, exactly the same code applies for JavaScript, Python and Ruby.
First, a substructure, E_{0}, is created
by a single assignment of a literal {...}
to r
.
This structure, depicted by full lines, is an arrow tree
(therefore, it is a spanning tree).
The literal in turn appears to be a JSON serialization of E_{0}.
Subsequently, the remaining arrows are created by assignments of already existing nodes. Arrows that cause cycles are displayed in blue.
r = {"a":{"a":5,"b":5},"c":{"a":{"w":5},"c":{}},"d":{"w":1.3}}
r["b"] = r["a"]; r["c"]["b"] = r["c"]["a"]
r["c"]["a"]["p"] = r["c"]; r["d"]["e"] = r["d"]["self"] = r["d"]
In the Linux VFS, the name function σ is represented by the d_name
field in the dentry data structure.^{[13]}
The E_{0} structure above demonstrates a correspondence between JSONrepresentable structures and hardlink structures of file systems.
In both cases, there is a fixed set of builtin types of nodes
of which one type is a container type, except that in JSON, there are in fact two such types
– Object and Array. If the latter one is ignored (as well as the distinction between individual primitive data types) then the provided abstractions of filesystems and JSON data are the same
–
both are arrow trees equipped with naming σ and a distinction of container nodes.
The naming function σ of a nested dictionary E naturally extends from arrows to arrow paths.
Each sequence p = [a_{1}, …, a_{n}] of consecutive arrows is implicitly assigned a pathname (cf. Pathname)
– the sequence
σ(p) = [σ(a_{1}), …, σ(a_{n})] of arrow names.^{[f]}
Local uniqueness carries over to arrow paths: different sibling paths have different pathnames.
In particular, the rootoriginating arrow paths are in onetoone correspondence with their pathnames.
This correspondence provides a symbolic
representation of the unfolding of E via pathnames
–
the nodes in E are globally identified via a tree of pathnames.
The structures introduced in the previous subsection form just the core "hierarchical" part of tree data structures that appear in computing. In most cases, there is also an additional "horizontal" ordering between siblings. In search trees the order is commonly established by the "key" or value associated with each sibling, but in many trees that is not the case. For example, XML documents, lists within JSON files, and many other structures have order that does not depend on the values in the nodes, but is itself data  sorting the paragraphs of a novel alphabetically would lose information.^{[dubious – discuss]}
The correspondent expansion of the previously described tree structures (X, ≤) can be defined by endowing each sibling set with a linear order as follows.^{[16]}^{[17]} An alternative definition according to Kuboyama^{[1]} is presented in the next subsection.
An ordered tree is a structure (X, ≤_{V}, ≤_{S}) where X is a nonempty set of nodes and ≤_{V} and ≤_{S} are relations on X called vertical (or also hierarchical^{[1]}) order and sibling order, respectively. The structure is subject to the following conditions:
(The following condition can be omitted in the case of finite trees.)
Conditions (2) and (3) say that (X, ≤_{S}) is a componentwise linear order, each component being a sibling set. Condition (4) asserts that if a sibling set S is infinite then (S, ≤_{S}) is isomorphic to (N, ≤), the usual ordering of natural numbers.
Given this, there are three (another) distinguished partial orders which are uniquely given by the following prescriptions:
(<_{H})  =  (≤_{V}) ? (<_{S}) ? (≥_{V})  (the horizontal order), 
(<_{L?})  =  (>_{V}) ∪ (<_{H})  (the "discordant" linear order), 
(<_{L?})  =  (<_{V}) ∪ (<_{H})  (the "concordant" linear order). 
This amounts to a "VSHL^{±}" system of five partial orders ≤_{V}, ≤_{S}, ≤_{H}, ≤_{L?}, ≤_{L?} on the same set X of nodes, in which, except for the pair { ≤_{S}, ≤_{H} }, any two relations uniquely determine the other three, see the determinacy table.
Notes about notational conventions:
This yields six versions ?, <, ≤, ?, >, ≥ for a single partial order relation. Except for ? and ?, each version uniquely determines the others. Passing from ? to < requires that < be transitively reducible. This is always satisfied for all of <_{V}, <_{S} and <_{H} but might not hold for <_{L?} or <_{L?} if X is infinite.
The partial orders ≤_{V} and ≤_{H} are complementary: (<_{V}) ? (>_{V}) ? (<_{H}) ? (>_{H}) = X × X \ id_{X}. As a consequence, the "concordant" linear order <_{L?} is a linear extension of <_{V}. Similarly, <_{L?} is a linear extension of >_{V}.
The covering relations ?_{L?} and ?_{L?} correspond to preorder traversal and postorder traversal, respectively. If x ?_{L?} y then, according to whether y has a previous sibling or not, the x node is either the "rightmost" nonstrict descendant of the previous sibling of y or, in the latter case, x is the first child of y. Pairs of the latter case form the relation (?_{L?}) \ (<_{H}) which is a partial map that assigns each nonleaf node its first child node. Similarly, (?_{L?}) \ (>_{H}) assigns each nonleaf node with finitely many children its last child node.
The Kuboyama's definition of "rooted ordered trees"^{[1]} makes use of the horizontal order ≤_{H} as a definitory relation.^{[g]} (See also Suppes.^{[18]}) Using the notation and terminology introduced so far, the definition can be expressed as follows.
An ordered tree is a structure (X, ≤_{V}, ≤_{H}) such that conditions (1–5) are satisfied:
(Like before, the following condition can be omitted in the case of finite trees.)
The sibling order (≤_{S}) is obtained by (<_{S}) = (<_{H}) ? ((?_{V}) ? (?_{V})), i.e. two distinct nodes are in sibling order if and only if they are in horizontal order and are siblings.
The following table shows the determinacy of the "VSHL^{±}" system. Relational expressions in the table's body are equal to one of <_{V}, <_{S}, <_{H}, <_{L?}, or <_{L?} according to the column. It follows that except for the pair { ≤_{S}, ≤_{H} }, an ordered tree (X, ...) is uniquely determined by any two of the five relations.
<_{V}  <_{S}  <_{H}  <_{L?}  <_{L?}  

V,S  (≤_{V}) ? (<_{S}) ? (≥_{V})  
V,H  (<_{H}) ? ((?_{V})?(?_{V}))  (>_{V}) ∪ (<_{H})  (<_{V}) ∪ (<_{H})  
V,L?  (<_{L?}) ? ((?_{V})?(?_{V}))  (<_{L?}) \ (>_{V})  
V,L?  (<_{L?}) ? ((?_{V})?(?_{V}))  (<_{L?}) \ (<_{V})  
H,L?  (>_{L?}) \ (<_{H})  
H,L?  (<_{L?}) \ (<_{H})  
L?,L?  (>_{L?}) ∩ (<_{L?})  (<_{L?}) ∩ (<_{L?})  
S,L?  x ?_{V}y ↔ y = inf_{L?}(Y) where Y is the image of {x} under (≥_{S})?(?_{L?})  
S,L?  x ?_{V}y ↔ y = sup_{L?}(Y) where Y is the image of {x} under (≤_{S})?(?_{L?}) 
In the last two rows,
inf_{L?}(Y) denotes the infimum of Y in
(X, ≤_{L?}),
and
sup_{L?}(Y) denotes the supremum of Y in
(X, ≤_{L?}).
In both rows, (≤_{S}) resp. (≥_{S}) can be equivalently replaced by the sibling equivalence
(≤_{S})?(≥_{S}).
In particular, the partition into sibling sets together with either of
≤_{L?} or
≤_{L?}
is also sufficient to determine the ordered tree.
The first prescription for ?_{V} can be read as:
the parent of a nonroot node x equals the infimum of the set of all immediate
predecessors of siblings of x, where the words infimum
and predecessors
are meant w.r.t. ≤_{L?}.
Similarly with the second prescription, just use supremum
, successors
and
≤_{L?}.
The relations ≤_{S} and ≤_{H} obviously cannot form a definitory pair. For the simplest example, consider an ordered tree with exactly two nodes – then one cannot tell which of them is the root.
XPath Axis  Relation 

ancestor

<_{V} 
ancestororself

≤_{V} 
child

?_{V} 
descendant

>_{V} 
descendantorself

≥_{V} 
following

<_{H} 
followingsibling

<_{S} 
parent

?_{V} 
preceding

>_{H} 
precedingsibling

>_{S} 
self

id_{X} 
The table on the right shows a correspondence of introduced relations to XPath axes, which are used in structured document systems to access nodes that bear particular ordering relationships to a starting "context" node. For a context node^{[19]}x, its axis named by the specifier in the left column is the set of nodes that equals the image of {x} under the correspondent relation. As of XPath 2.0, the nodes are "returned" in document order, which is the "discordant" linear order ≤_{L?}. A "concordance" would be achieved, if the vertical order ≤_{V} was defined oppositely, with the bottomup direction outwards the root like in set theory in accordance to natural trees.^{[h]}
Below is the list of partial maps that are typically used for ordered tree traversal.^{[20]} Each map is a distinguished functional subrelation of ≤_{L?} or of its opposite.
The traversal maps constitute a partial unary algebra^{[21]}(X, parent, previousSibling, ..., nextNode) that forms a basis for representing trees as linked data structures. At least conceptually, there are parent links, sibling adjacency links, and first / last child links. This also applies to unordered trees in general, which can be observed on the dentry data structure in the Linux VFS.^{[22]}
Similarly to the "VSHL^{±}" system of partial orders, there are pairs of traversal maps that uniquely determine the whole ordered tree structure. Naturally, one such generating structure is (X, ?_{V}, ?_{S}) which can be transcribed as (X, parent, nextSibling) – the structure of parent and nextsibling links. Another important generating structure is (X, firstChild, nextSibling) known as leftchild rightsibling binary tree. This partial algebra establishes a onetoone correspondence between binary trees and ordered trees.
The correspondence to binary trees provides a concise definition of ordered trees as partial algebras.
An ordered tree is a structure where X is a nonempty set of nodes, and lc, rs are partial maps on X called leftchild and rightsibling, respectively. The structure is subject to the following conditions:
The partial order structure (X, ≤_{V}, ≤_{S}) is obtained as follows:
(?_{S})  =  (rs), 
(?_{V})  =  (lc) ? (≤_{S}). 
Ordered trees can be naturally encoded by finite sequences of natural numbers.^{[23]}^{[i]} Denote ?^{?} the set of all finite sequences of natural numbers. Then any nonempty subset W of ?^{?} that is closed under taking prefixes gives rise to an ordered tree: just take the prefix order for ≥_{V} and the lexicographical order for ≤_{L?}. Conversely, for an ordered tree T = (X, ≤_{V}, ≤_{L?}) assign each node x the sequence of sibling indices, i.e. the root is assigned the empty sequence and for every nonroot node x, let w(x) = w(parent(x)) ? [i] where i is the number of preceding siblings of x and ? is the concatenation operator. Put W = {w(x)  x ∈ X}. Then W, equipped with the induced orders ≤_{V} (the inverse of prefix order) and ≤_{L?} (the lexicographical order), is isomorphic to T.
As a possible expansion of the "VSHL^{±}" system, another distinguished relations between nodes can be defined, based on the tree's level structure. First, let us denote by ∼_{E} the equivalence relation defined by x ∼_{E}y if and only if x and y have the same number of ancestors. This yields a partition of the set of nodes into levels L_{0}, L_{1}, ... (, L_{n}) – a coarsement of the partition into sibling sets. Then define relations <_{E}, <_{B?} and <_{B?} by
It can be observed that <_{E} is a strict partial order and <_{B?} and <_{B?} are strict total orders. Moreover, there is a similarity between the "VSL^{±}" and "VEB^{±}" systems: <_{E} is componentwise linear and orthogonal to <_{V}, <_{B?} is linear extension of <_{E} and of >_{V}, and <_{B?} is a linear extension of <_{E} and of <_{V}.
There is a distinction between a tree as an abstract data type and as a concrete data structure, analogous to the distinction between a list and a linked list. As a data type, a tree has a value and children, and the children are themselves trees; the value and children of the tree are interpreted as the value of the root node and the subtrees of the children of the root node. To allow finite trees, one must either allow the list of children to be empty (in which case trees can be required to be nonempty, an "empty tree" instead being represented by a forest of zero trees), or allow trees to be empty, in which case the list of children can be of fixed size (branching factor, especially 2 or "binary"), if desired.
As a data structure, a linked tree is a group of nodes, where each node has a value and a list of references to other nodes (its children). There is also the requirement that no two "downward" references point to the same node. In practice, nodes in a tree commonly include other data as well, such as next/previous references, references to their parent nodes, or nearly anything.
Due to the use of references to trees in the linked tree data structure, trees are often discussed implicitly assuming that they are being represented by references to the root node, as this is often how they are actually implemented. For example, rather than an empty tree, one may have a null reference: a tree is always nonempty, but a reference to a tree may be null.
Recursively, as a data type a tree is defined as a value (of some data type, possibly empty), together with a list of trees (possibly an empty list), the subtrees of its children; symbolically:
t: v [t[1], ..., t[k]]
(A tree t consists of a value v and a list of other trees.)
More elegantly, via mutual recursion, of which a tree is one of the most basic examples, a tree can be defined in terms of forest (a list of trees), where a tree consists of a value and a forest (the subtrees of its children):
f: [t[1], ..., t[k]] t: v f
Note that this definition is in terms of values, and is appropriate in functional languages (it assumes referential transparency); different trees have no connections, as they are simply lists of values.
As a data structure, a tree is defined as a node (the root), which itself consists of a value (of some data type, possibly empty), together with a list of references to other nodes (list possibly empty, references possibly null); symbolically:
n: v [&n[1], ..., &n[k]]
(A node n consists of a value v and a list of references to other nodes.)
This data structure defines a directed graph,^{[j]} and for it to be a tree one must add a condition on its global structure (its topology), namely that at most one reference can point to any given node (a node has at most a single parent), and no node in the tree point to the root. In fact, every node (other than the root) must have exactly one parent, and the root must have no parents.
Indeed, given a list of nodes, and for each node a list of references to its children, one cannot tell if this structure is a tree or not without analyzing its global structure and that it is in fact topologically a tree, as defined below.
As an ADT, the abstract tree type T with values of some type E is defined, using the abstract forest type F (list of trees), by the functions:
with the axioms:
In terms of type theory, a tree is an inductive type defined by the constructors nil (empty forest) and node (tree with root node with given value and children).
Viewed as a whole, a tree data structure is an ordered tree, generally with values attached to each node. Concretely, it is (if required to be nonempty):
together with:
Often trees have a fixed (more properly, bounded) branching factor (outdegree), particularly always having two child nodes (possibly empty, hence at most two nonempty child nodes), hence a "binary tree".
Allowing empty trees makes some definitions simpler, some more complicated: a rooted tree must be nonempty, hence if empty trees are allowed the above definition instead becomes "an empty tree or a rooted tree such that ...". On the other hand, empty trees simplify defining fixed branching factor: with empty trees allowed, a binary tree is a tree such that every node has exactly two children, each of which is a tree (possibly empty). The complete sets of operations on the tree must include fork operation.
A node is a structure which may contain a value or condition, or represent a separate data structure (which could be a tree of its own). Each node in a tree has zero or more child nodes, which are below it in the tree (by convention, trees are drawn growing downwards). A node that has a child is called the child's parent node (or ancestor node, or superior). A node has at most one parent.
An internal node (also known as an inner node, inode for short, or branch node) is any node of a tree that has child nodes. Similarly, an external node (also known as an outer node, leaf node, or terminal node) is any node that does not have child nodes.
The topmost node in a tree is called the root node. Depending on definition, a tree may be required to have a root node (in which case all trees are nonempty), or may be allowed to be empty, in which case it does not necessarily have a root node. Being the topmost node, the root node will not have a parent. It is the node at which algorithms on the tree begin, since as a data structure, one can only pass from parents to children. Note that some algorithms (such as postorder depthfirst search) begin at the root, but first visit leaf nodes (access the value of leaf nodes), only visit the root last (i.e., they first access the children of the root, but only access the value of the root last). All other nodes can be reached from it by following edges or links. (In the formal definition, each such path is also unique.) In diagrams, the root node is conventionally drawn at the top. In some trees, such as heaps, the root node has special properties. Every node in a tree can be seen as the root node of the subtree rooted at that node.
The height of a node is the length of the longest downward path to a leaf from that node. The height of the root is the height of the tree. The depth of a node is the length of the path to its root (i.e., its root path). This is commonly needed in the manipulation of the various selfbalancing trees, AVL Trees in particular. The root node has depth zero, leaf nodes have height zero, and a tree with only a single node (hence both a root and leaf) has depth and height zero. Conventionally, an empty tree (tree with no nodes, if such are allowed) has height 1.
A subtree of a tree T is a tree consisting of a node in T and all of its descendants in T.^{[k]}^{[24]} Nodes thus correspond to subtrees (each node corresponds to the subtree of itself and all its descendants)  the subtree corresponding to the root node is the entire tree, and each node is the root node of the subtree it determines; the subtree corresponding to any other node is called a proper subtree (by analogy to a proper subset).
Trees are often drawn in the plane. Ordered trees can be represented essentially uniquely in the plane, and are hence called plane trees, as follows: if one fixes a conventional order (say, counterclockwise), and arranges the child nodes in that order (first incoming parent edge, then first child edge, etc.), this yields an embedding of the tree in the plane, unique up to ambient isotopy. Conversely, such an embedding determines an ordering of the child nodes.
If one places the root at the top (parents above children, as in a family tree) and places all nodes that are a given distance from the root (in terms of number of edges: the "level" of a tree) on a given horizontal line, one obtains a standard drawing of the tree. Given a binary tree, the first child is on the left (the "left node"), and the second child is on the right (the "right node").
There are many different ways to represent trees; common representations represent the nodes as dynamically allocated records with pointers to their children, their parents, or both, or as items in an array, with relationships between them determined by their positions in the array (e.g., binary heap).
Indeed, a binary tree can be implemented as a list of lists (a list where the values are lists): the head of a list (the value of the first term) is the left child (subtree), while the tail (the list of second and subsequent terms) is the right child (subtree). This can be modified to allow values as well, as in Lisp Sexpressions, where the head (value of first term) is the value of the node, the head of the tail (value of second term) is the left child, and the tail of the tail (list of third and subsequent terms) is the right child.
In general a node in a tree will not have pointers to its parents, but this information can be included (expanding the data structure to also include a pointer to the parent) or stored separately. Alternatively, upward links can be included in the child node data, as in a threaded binary tree.
If edges (to child nodes) are thought of as references, then a tree is a special case of a digraph, and the tree data structure can be generalized to represent directed graphs by removing the constraints that a node may have at most one parent, and that no cycles are allowed. Edges are still abstractly considered as pairs of nodes, however, the terms parent and child are usually replaced by different terminology (for example, source and target). Different implementation strategies exist: a digraph can be represented by the same local data structure as a tree (node with value and list of children), assuming that "list of children" is a list of references, or globally by such structures as adjacency lists.
In graph theory, a tree is a connected acyclic graph; unless stated otherwise, in graph theory trees and graphs are assumed undirected. There is no onetoone correspondence between such trees and trees as data structure. We can take an arbitrary undirected tree, arbitrarily pick one of its vertices as the root, make all its edges directed by making them point away from the root node  producing an arborescence  and assign an order to all the nodes. The result corresponds to a tree data structure. Picking a different root or different ordering produces a different one.
Given a node in a tree, its children define an ordered forest (the union of subtrees given by all the children, or equivalently taking the subtree given by the node itself and erasing the root). Just as subtrees are natural for recursion (as in a depthfirst search), forests are natural for corecursion (as in a breadthfirst search).
Via mutual recursion, a forest can be defined as a list of trees (represented by root nodes), where a node (of a tree) consists of a value and a forest (its children):
f: [n[1], ..., n[k]] n: v f
Stepping through the items of a tree, by means of the connections between parents and children, is called walking the tree, and the action is a 'walk' of the tree. Often, an operation might be performed when a pointer arrives at a particular node. A walk in which each parent node is traversed before its children is called a preorder walk; a walk in which the children are traversed before their respective parents are traversed is called a postorder walk; a walk in which a node's left subtree, then the node itself, and finally its right subtree are traversed is called an inorder traversal. (This last scenario, referring to exactly two subtrees, a left subtree and a right subtree, assumes specifically a binary tree.) A levelorder walk effectively performs a breadthfirst search over the entirety of a tree; nodes are traversed level by level, where the root node is visited first, followed by its direct child nodes and their siblings, followed by its grandchild nodes and their siblings, etc., until all nodes in the tree have been traversed.
journal=
(help)
journal=
(help)
journal=
(help)