All Topics  
Binary tree

 

   Email Print
   Bookmark   Link






 

Binary tree



 
 
In computer science
Computer science

Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems....
, a binary tree is a tree data structure
Tree (data structure)

In computer science, a tree is a widely-used data structure that emulates a hierarchical tree structure with a set of linked Vertex_. It is an acyclic connected graph where each node has a set of zero or more children nodes, and at most one parent node....
 in which each node has at most two children. Typically the child nodes are called left and right. Binary trees are commonly used to implement binary search tree
Binary search tree

In computer science, a binary search tree is a binary tree data structurewhich has the following properties:* Each node has a distinct value....
s and binary heap
Binary heap

A binary heap is a heap data structure created using a binary tree. It can be seen as a binary tree with two additional constraints:*The shape property: the tree is a complete binary tree; that is, all levels of the tree, except possibly the last one are fully filled, and, if the last level of the tree is not complete, the node...
s.




- Note that this terminology often varies in the literature, especially with respect to the meaning "complete" and "full".

lass="link1" onMouseover='showByLink("m212297",this)' onMouseout='hide("m212297")'href="http://www.absoluteastronomy.com/topics/Graph_theory">Graph theorists
Graph theory

In mathematics and computer science, graph theory is the study of graph : mathematical structures used to model pairwise relations between objects from a certain collection....
  use the following definition: A binary tree is a connected acyclic graph such that the degree of each vertex
Vertex (graph theory)

In graph theory, a vertex or node is the fundamental unit out of which graphs are formed: an undirected graph consists of a set of vertices and a set of edges , while a directed graph consists of a set of vertices and a set of arcs ....
 is no more than 3.






Discussion
Ask a question about 'Binary tree'
Start a new discussion about 'Binary tree'
Answer questions from other users
Full Discussion Forum



Encyclopedia


In computer science
Computer science

Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems....
, a binary tree is a tree data structure
Tree (data structure)

In computer science, a tree is a widely-used data structure that emulates a hierarchical tree structure with a set of linked Vertex_. It is an acyclic connected graph where each node has a set of zero or more children nodes, and at most one parent node....
 in which each node has at most two children. Typically the child nodes are called left and right. Binary trees are commonly used to implement binary search tree
Binary search tree

In computer science, a binary search tree is a binary tree data structurewhich has the following properties:* Each node has a distinct value....
s and binary heap
Binary heap

A binary heap is a heap data structure created using a binary tree. It can be seen as a binary tree with two additional constraints:*The shape property: the tree is a complete binary tree; that is, all levels of the tree, except possibly the last one are fully filled, and, if the last level of the tree is not complete, the node...
s.

Binary Tree

Definitions for rooted trees

  • A directed edge refers to the link from the parent to the child (the arrows in the picture of the tree).
  • The root node of a tree is the node
    Node (computer science)

    A node is an abstract basic unit used to build linked data structures such as tree data structure, linked lists, and computer-based representations of graph ....
     with no parents. There is at most one root node in a rooted tree.
  • A leaf node
    Leaf node

    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....
     has no children.
  • The depth of a node n is the length of the path from the root to the node. The set of all nodes at a given depth is sometimes called a level of the tree. The root node is at depth zero.
  • The height of a tree is the length of the path from the root to the deepest node in the tree. A (rooted) tree with only a node (the root) has a height of zero.
  • Siblings are nodes that share the same parent node.
  • If a path exists from node p to node q, where node p is closer to the root node than q, then p is an ancestor of q and q is a descendant of p.
  • The size of a node is the number of descendants it has including itself.
  • In-degree of a node is the number of edges arriving at that node.
  • Out-degree of a node is the number of edges leaving that node.
  • Root is the only node in the tree with In-degree = 0


Types of binary trees

  • A rooted binary tree is a rooted tree in which every node has at most two children.
  • A full binary tree (sometimes proper binary tree or 2-tree) is a tree in which every node other than the leaves has two children.
  • A perfect binary tree is a full binary tree in which all leaves are at the same depth or same Level. (This is ambiguously also called a complete binary tree.)
  • A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
  • An infinite complete binary tree is a tree with
    Aleph number

    In the branch of mathematics known as set theory, the aleph numbers are a sequence of numbers used to represent the cardinality of infinite sets....
     levels, where for each level d the number of existing nodes at level d is equal to 2d. The cardinal number of the set of all nodes is . The cardinal number of the set of all paths is .
  • A balanced binary tree is where the depth of all the leaves differs by at most 1. Balanced trees have a predictable depth (how many nodes are traversed from the root to a leaf, root counting as node 0 and subsequent as 1, 2, ..., depth). This depth is equal to the integer part of where is the number of nodes on the balanced tree. Example 1: balanced tree with 1 node, (depth = 0). Example 2: balanced tree with 3 nodes, (depth=1). Example 3: balanced tree with 5 nodes, (depth of tree is 2 nodes).
  • A rooted complete binary tree can be identified with a free magma.
  • An almost complete binary tree is a tree in which each node that has a right child also has a left child. Having a left child does not require a node to have a right child. Stated alternately, an almost complete binary tree is a tree where for a right child, there is always a left child, but for a left child there may not be a right child.
  • A degenerate tree is a tree where for each parent node, there is only one associated child node. This means that in a performance measurement, the tree will behave like a linked list data structure.
  • The number of nodes ' in a perfect binary tree can be found using this formula: ' where ' is the height of the tree.
  • The number of leaf nodes ' in a perfect binary tree can be found using this formula: ' where ' is the height of the tree.
  • The number of nodes ' in a complete binary tree is minimum: ' and maximum: ' where ' is the height of the tree.
  • The number of NULL links in a Complete Binary Tree of n-node is (n+1).
  • The number of leaf node in a Complete Binary Tree of n-node is .


- Note that this terminology often varies in the literature, especially with respect to the meaning "complete" and "full".

Definition in graph theory

Graph theorists
Graph theory

In mathematics and computer science, graph theory is the study of graph : mathematical structures used to model pairwise relations between objects from a certain collection....
  use the following definition: A binary tree is a connected acyclic graph such that the degree of each vertex
Vertex (graph theory)

In graph theory, a vertex or node is the fundamental unit out of which graphs are formed: an undirected graph consists of a set of vertices and a set of edges , while a directed graph consists of a set of vertices and a set of arcs ....
 is no more than 3. It can be shown that in any binary tree, there are exactly two more nodes of degree one than there are of degree three, but there can be any number of nodes of degree two. A rooted binary tree is such a graph that has one of its vertices of degree no more than 2 singled out as the root.

With the root thus chosen, each vertex will have a uniquely defined parent, and up to two children; however, so far there is insufficient information to distinguish a left or right child. If we drop the connectedness requirement, allowing multiple connected components
Connected component (graph theory)

In graph theory, a connected component of an undirected graph is a subgraph in which any two vertices are connected graph to each other by path , and to which no more vertices or edges can be added while preserving its connectivity....
 in the graph, we call such a structure a forest.

Another way of defining binary trees is a recursive definition on directed graphs. A binary tree is either:
  • A single vertex.
  • A graph formed by taking two binary trees, adding a vertex, and adding an edge directed from the new vertex to the root of each binary tree.
This also does not establish the order of children, but does fix a specific root node.

Combinatorics

The groupings of pairs of nodes in a tree can be represented as pairs of letters, surrounded by parenthesis. Thus, (a b) denotes the binary tree whose left subtree is a and whose right subtree is b. Strings of balanced pairs of parenthesis may therefore be used to denote binary trees in general. The set of all possible strings consisting entirely of balanced parentheses is known as the Dyck language
Dyck language

In the theory of formal languages of computer science, mathematics, and linguistics, the Dyck language is the language consisting of those balanced string #Formal theory of parentheses [ and ]....
.

Given n nodes, the total number of ways in which these nodes can be arranged into a binary tree is given by the Catalan number
Catalan number

In combinatorics, the Catalan numbers form a sequence of natural numbers that occur in various counting problems, often involvingrecursion defined objects....
 . For example, declares that (a 0) and (0 a) are the only binary trees possible that have two nodes, and declares that ((a 0) 0), (0 a) 0), (0 (a 0)), (0 (0 a)), and (a b) are the only five binary trees possible that have 3 nodes. Here 0 represents a subtree that is not present.

The ability to represent binary trees as strings of symbols and parentheses implies that binary trees can represent the elements of a magma
Magma (algebra)

In abstract algebra, a magma is a basic kind of algebraic structure. Specifically, a magma consists of a Set M equipped with a single binary operation M × M ? M....
. Conversely, the set of all possible binary trees, together with the natural operation of attaching trees to one-another, forms a magma, the free magma.

Given a string representing a binary tree, the operators to obtain the left and right subtrees are sometimes referred to as car and cdr
Car and cdr

Introduced in the Lisp programming language, car and cdr are primitive operations upon linked lists composed of cons cells. A cons cell is composed of two pointers; the car operation extracts the first pointer, and the cdr operation extracts the second....
.

Methods for storing binary trees

Binary trees can be constructed from programming language
Programming language

A programming language is a machine-readable artificial language designed to express computations that can be performed by a machine, particularly a computer....
 primitives in several ways. In a language with records
Record (computer science)

In computer science, a record type or struct is a type whose values are records, i.e. aggregates of several items of possibly different types....
 and reference
Reference (computer science)

In computer science, a reference is an object containing information about how to locate and access the particular data item, as opposed to containing the data itself....
s, binary trees are typically constructed by having a tree node structure which contains some data and references to its left child and its right child. Sometimes it also contains a reference to its unique parent. If a node has fewer than two children, some of the child pointers may be set to a special null value, or to a special sentinel node
Sentinel node

A sentinel node is a programming idiom used to speed up some operations on linked lists and Tree . It refers to a special type of object that represents the end of a data structure....
.

Binary trees can also be stored as an implicit data structure
Implicit data structure

In computer science, an implicit data structure is a data structure that uses very little memory besides the actual data elements. It is called "implicit" because most of the structure of the elements is expressed implicitly by their order....
 in array
Array

In computer science, an array is a data structure consisting of a group of element s that are accessed by index . In most programming languages each element has the same data type and the array occupies a contiguous area of computer memory....
s, and if the tree is a complete binary tree, this method wastes no space. In this compact arrangement, if a node has an index i, its children are found at indices (for the left child) and (for the right), while its parent (if any) is found at index ' (assuming the root has index zero). This method benefits from more compact storage and better locality of reference
Locality of reference

In computer science, locality of reference, also known as the principle of locality, is the phenomenon of the same value or related computer storage locations being frequently accessed....
, particularly during a preorder traversal. However, it is expensive to grow and wastes space proportional to 2h - n for a tree of height h with n nodes.

Binary Tree in Array


In languages with tagged union
Tagged union

In computer science, a tagged union, also called a variant type, variant record, discriminated union, or disjoint union, is a data structure used to hold a value that could take on several different, but fixed types....
s such as ML
ML programming language

ML is a general-purpose functional programming language developed by Robin Milner and others in the late 1970s at the University of Edinburgh, whose syntax is inspired by ISWIM....
, a tree node is often a tagged union of two types of nodes, one of which is a 3-tuple of data, left child, and right child, and the other of which is a "leaf" node, which contains no data and functions much like the null value in a language with pointers.

Methods of iterating over binary trees

Often, one wishes to visit each of the nodes in a tree and examine the value there. There are several common orders in which the nodes can be visited, and each has useful properties that are exploited in algorithms based on binary trees.

Pre-order, in-order, and post-order traversal.


Pre-order, in-order, and post-order traversal visit each node in a tree by recursively visiting each node in the left and right subtrees of the root. If the root node is visited before its subtrees, this is pre-order; if after, post-order; if between, in-order. In-order traversal is useful in binary search tree
Binary search tree

In computer science, a binary search tree is a binary tree data structurewhich has the following properties:* Each node has a distinct value....
s, where this traversal visits the nodes in increasing order.

Depth-first order

In depth-first order, we always attempt to visit the node farthest from the root that we can, but with the caveat that it must be a child of a node we have already visited. Unlike a depth-first search on graphs, there is no need to remember all the nodes we have visited, because a tree cannot contain cycles. Pre-order is a special case of this. See depth-first search
Depth-first search

Depth-first search is an algorithm for traversing or searching a tree data structure, tree structure, or graph . One starts at the root and explores as far as possible along each branch before backtracking....
 for more information.

Breadth-first order

Contrasting with depth-first order is breadth-first order, which always attempts to visit the node closest to the root that it has not already visited. See Breadth-first search
Breadth-first search

In graph theory, breadth-first search is a graph search algorithm that begins at the root node and explores all the neighboring nodes. Then for each of those nearest nodes, it explores their unexplored neighbor nodes, and so on, until it finds the goal....
 for more information. Also called a level-order traversal.

Encodings


Succinct encodings

A succinct data structure
Succinct data structure

In computer science, a succinct data structure for a given data type is a representation of the underlying combinatorial object that uses an amount of space ?close? to the information theoretic lower bound together with efficient algorithms for navigation, search, insertion and deletion operations....
 is one which takes the absolute minimum possible space, as established by information theoretical
Information theory

Information theory is a branch of applied mathematics and electrical engineering involving the quantification of information. Historically, information theory was developed by Claude E....
 lower bounds. The number of different binary trees on nodes is , the th Catalan number
Catalan number

In combinatorics, the Catalan numbers form a sequence of natural numbers that occur in various counting problems, often involvingrecursion defined objects....
 (assuming we view trees with identical structure as identical). For large , this is about ; thus we need at least about bits to encode it. A succinct binary tree therefore would occupy only 2 bits per node.

One simple representation which meets this bound is to visit the nodes of the tree in preorder, outputting "1" for an internal node and "0" for a leaf. If the tree contains data, we can simply simultaneously store it in a consecutive array in preorder. This function accomplishes this:

function EncodeSuccinct(node n, bitstring structure, array data)

The string structure has only bits in the end, where is the number of (internal) nodes; we don't even have to store its length. To show that no information is lost, we can convert the output back to the original tree like this:

function DecodeSuccinct(bitstring structure, array data)

More sophisticated succinct representations allow not only compact storage of trees but even useful operations on those trees directly while they're still in their succinct form.

Encoding n-ary trees as binary trees

There is a one-to-one mapping between general ordered trees and binary trees, which in particular is used by Lisp
Lisp programming language

Lisp is a family of computer programming languages with a long history and a distinctive, fully parenthesized syntax. Originally specified in 1958, Lisp is the second-oldest high-level programming language in widespread use today; only Fortran is older....
 to represent general ordered trees as binary trees. Each node N in the ordered tree corresponds to a node N' in the binary tree; the left child of N' is the node corresponding to the first child of N, and the right child of N' is the node corresponding to N 's next sibling --- that is, the next node in order among the children of the parent of N. This binary tree representation of a general order tree, is sometimes also referred to as a First-Child/Next-Sibling binary tree, or a Doubly-Chained Tree
Doubly-chained tree

In computer science, a Doubly-chained tree is a Tree in which each node has two Pointer . Typically, each node has one pointer pointing to its child node and one pointing at the node to its right....
, or a Filial-Heir chain.

One way of thinking about this is that each node's children are in a linked list
Linked list

In computer science, a linked list is one of the fundamental data structures, and can be used to implement other data structures. It consists of a sequence of node s, each containing arbitrary data Field s and one or two reference s pointing to the next and/or previous nodes....
, chained together with their right fields, and the node only has a pointer to the beginning or head of this list, through its left field.

For example, in the tree on the left, A has the 6 children . It can be converted into the binary tree on the right.



The binary tree can be thought of as the original tree tilted sideways, with the black left edges representing first child and the blue right edges representing next sibling. The leaves of the tree on the left would be written in Lisp as:

I J) C D ((P) (Q)) F (M))

which would be implemented in memory as the binary tree on the right, without any letters on those nodes that have a left child

See also

  • 2-3 tree
    2-3 tree

    A 2-3 tree in computer science is a type of data structure, a B-tree where every Node with children has either two children and one data element or three children and two data elements ....
  • 2-3-4 tree
    2-3-4 tree

    A 2-3-4 tree , in computer science, is a self-balancing data structure that is commonly used to implement associative array. 2-3-4 trees are B-trees of order 4; like B-trees in general, they can search, insert and delete in Big-O notation time....
  • AA tree
    AA tree

    An AA tree in computer science is a form of Self-balancing binary search tree used for storing and retrieving ordered data efficiently. AA trees are named for Arne Andersson , their inventor....
  • B-tree
    B-tree

    In computer science, a B-tree is a tree data structure that keeps data sorted and allows searches, insertions, and deletions in logarithmic Amortized analysis....
  • Binary search tree
    Binary search tree

    In computer science, a binary search tree is a binary tree data structurewhich has the following properties:* Each node has a distinct value....
  • Binary space partitioning
    Binary space partitioning

    Binary space partitioning is a method for recursively subdividing a Euclidean space into convex sets by hyperplanes. This subdivision gives rise to a representation of the scene by means of a Tree known as a BSP tree....
  • Elastic binary tree
  • Kraft's inequality
    Kraft's inequality

    In coding theory, Kraft's inequality gives a necessary and sufficient condition for the existence of a Variable-length code#Uniquely decodable codes for a given set of codeword lengths....
  • Recursion (computer science)
    Recursion (computer science)

    Recursion is a way of thinking about and solving problems. In fact, Recursion_ is one of the central ideas of computer science. Solving a problem using recursion means the solution depends on solutions to smaller instances of the same problem....
  • Threaded binary tree
    Threaded binary tree

    A threaded binary tree may be defined as follows:A binary tree is threaded by making all right child pointers, that would normally be null, point to the inorder successor of the node, and all left child pointers, that would normally be null, point to the inorder predecessor of the node."...
  • binary tree of preimages in Julia set ( IIM/J )
  • Strahler number
    Strahler number

    The Strahler number, or Horton-Strahler number of a binary tree is defined as follows:An empty tree has Strahler number 0.If the binary tree T has subtrees and , the Strahler number S of T is defined by:...


External links

  • — opensource library