Vous êtes sur la page 1sur 5

Tag: tree data structure

About
In computer science, a tree is a widely-used data structure that emulates a tree structure with a set
of linked nodes.

Nodes
A node may contain a value or a condition or represents a separate data structure or 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 grow down, not up as they do in nature). 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. 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).

Root nodes
The topmost node in a tree is called the root node. Being the topmost node, the root node will not
have parents. It is the node at which operations on the tree commonly begin (although some
algorithms begin with the leaf nodes and work up ending at the root). 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, it is typically 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.

Leaf nodes
Nodes at the bottommost level of the tree are called leaf nodes. Since they are at the bottommost
level, they do not have any children.

Internal nodes
An internal node or inner node is any node of a tree that has child nodes and is thus not a leaf
node.

Subtrees
A subtree is a portion of a tree data structure that can be viewed as a complete tree in itself. Any
node in a tree T, together with all the nodes below it, comprise a subtree of T. The subtree
corresponding to the root node is the entire tree; the subtree corresponding to any other node is
called a proper subtree (in analogy to the term proper subset).

From en.wikipedia.org/wiki/Tree_data_structure

PHP Recursive str_replace: replaceTree


Working with trees When working with tree data structures you often need to craft them in different
ways. PHP offers a lot of functions to change the shape of arrays, but often they only go 1 level deep.
Trees can count an almost infinite number of levels. Hence we need recursive replacements for our
beloved array & string functions.

• share:
• Sep 05 2008
• 3 Comments

PHP Recursive ksort: ksortTree


Working with trees When working with tree data structures you often need to craft them in different
ways. PHP offers a lot of functions to change the shape of arrays, but often they only go 1 level deep.
Trees can count an almost infinite number of levels. Hence we need recursive replacements for our
beloved array functions.

• share:
• Sep 05 2008
• 7 Comments

Convert anything to Tree Structures in PHP


I recently faced a programming challenge that almost broke my brain. I needed to create a function
that could explode any single-dimensional array into a full blown tree structure, based on the
delimiters found in it's keys. Tricky part was size of the tree could be infinite. I called the function:
explodeTree. And maybe it's best to first look at an example.

Leaf node
From Wikipedia, the free encyclopedia
This article needs additional citations for verification.
Please help improve this article by adding reliable references. Unsourced material may be challenged and removed.
(September 2010)

9, 14, 19, 67 and 76 are leaf nodes.

In computer science, a leaf node or external node is a node of a tree data structure that has zero child nodes.
Often, leaf nodes are the nodes farthest from the root node. In the graph theory tree, a leaf node is a vertex of
degree 1 other than the root (except when the tree has only one vertex; then the root, too, is a leaf). Every tree
has at least one leaf.

A non-leaf node is called an internal node. Some trees only store data in internal nodes, though this affects the
dynamics of storing data in the tree. For example, with empty leaves, one can store an empty tree with a single
leaf node. However with leaves that can store data, it is impossible to store an empty tree unless one stores
some kind of marker data in the leaf that signifies that the leaf is to be empty (and thus the tree to be empty as
well).

Conversely, some trees only store data in the leaf nodes, and use the internal nodes to hold other metadata,
such as the range of values in the subtree rooted at that node. This type of tree is useful for range queries.

Another example of this is a parse tree. In this type of structure, the root node represents the starting symbol of
a grammar, and all internal nodes represent derivations of non-terminals, which continue downward until
concrete symbols are established. The leafs are the actual lexical tokens of the sentence.[1]

Tree (data structure)


In computer science, a tree is a widely-used data structure that emulates a tree structure with a set of
linked nodes. It is an acyclic and connected graph. Most of the literatures also include the constraint that
a graph's edges must be undirected to be a tree. In addition to these three constraints, some literature
indicates that a graph's edges should be un-weighted to be a tree.

Contents

• 1 Nodes
o 1.1 Root nodes
o 1.2 Leaf nodes
o 1.3 Internal nodes
• 2 Subtrees
• 3 Tree ordering
• 4 Tree representations
o 4.1 Trees as graphs
• 5 Traversal methods
• 6 Common operations
• 7 Common uses
• 8 See also
o 8.1 Popular tree data structures
 8.1.1 Self balancing binary search trees
 8.1.2 Other trees
• 9 References

• 10 External links
Nodes
A node may contain a value or a condition or represent a separate data structure or 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
grow down, not up as they do in nature). 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. 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).

Root nodes
The topmost node in a tree is called the root node. Being the topmost node, the root node will not have
parents. It is the node at which operations on the tree commonly begin (although some algorithms begin
with the leaf nodes and work up ending at the root). 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, it is typically 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.

Leaf nodes
Nodes at the bottommost level of the tree are called leaf nodes. Since they are at the bottommost level,
they do not have any children.

Internal nodes
An internal node or inner node is any node of a tree that has child nodes and is thus not a leaf node.

Subtrees
A subtree is a portion of a tree data structure that can be viewed as a complete tree in itself. Any node
in a tree T, together with all the nodes below it, comprise a subtree of T. The subtree corresponding to
the root node is the entire tree; the subtree corresponding to any other node is called a proper
subtree (in analogy to the term proper subset).

Tree ordering
There are two basic types of trees. In an recursive tree or unordered tree, a tree is a tree in a purely
structural sense — that is to say, given a node, there is no order for the children of that node. A tree on
which an order is imposed — for example, by assigning different natural numbers to each edge leading
to a node's children — is called an edge-labeled tree or an ordered tree with data structures built
upon them being called ordered tree data structures.

Ordered trees are by far the most common form of tree data structure. Binary search trees are one kind
of ordered tree.

Tree representations
There are many different ways to represent trees; common representations represent the nodes as
records allocated on the heap(not to be confused with the heap data structure) 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).

Trees as graphs
In graph theory, a tree is a connected acyclic graph. A rooted tree is such a graph with a vertex singled
out as the root. In this case, any two vertices connected by an edge inherit a parent-child relationship.
An acyclic graph with multiple connected components or a set of rooted trees is sometimes called
a forest.
Traversal methods
Main article: Tree traversal

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 pre-order walk; a walk in which the children are traversed before their respective
parents are traversed is called a post-order walk.