You are on page 1of 10

Binary tree

From Wikipedia, the free encyclopedia

Not to be confused with B-tree.

A simple binary tree of size 9 and height 3, with a root node whose value is 2. The above tree is balanced but not sorted.

In computer science, a binary tree is a tree data structure in which each node has at most two child nodes, usually distinguished as "left" and "right". Nodes with children are parent nodes, and

child nodes may contain references to their parents. Outside the tree, there is often a reference to the "root" node (the ancestor of all nodes), if it exists. Any node in the data structure can be

reached by starting at root node and repeatedly following references to either the left or right child.

Binary trees are commonly used to implement binary search trees and binary heaps.



• 1 Definitions for rooted trees

• 2 Types of binary trees

• 3 Properties of binary trees

• 4 Proof

• 5 Type theory

• 6 Definition in graph theory

• 7 Combinatorics

• 8 Methods for storing binary trees

o 8.1 Nodes and references

o 8.2 Ahnentafel list

• 9 Methods of iterating over binary trees

o 9.1 Pre-order, in-order, and post-order traversal

o 9.2 Depth-first order

o 9.3 Breadth-first order

• 10 Encodings

eg:depth of tree with level =3. o 10. There is at most one root node in a rooted tree. Example 1: balanced . root counting as node 0 and subsequent as 1.  In-degree of a node is the number of edges arriving at that node.  A leaf node has no children.  The height of a tree is the length of the path from the root to the deepest node in the tree.  The root node of a tree is the node with no parents. The cardinal number of the set of all paths is . 2.  The size of a node is the number of descendants it has including itself. The root node is at depth zero (or one [1]). The node q is then termed a descendant of p. The set of all nodes at a given depth is sometimes called a level of the tree. is completely filled. A (rooted) tree with only one node (the root) has a height of zero (or one[2]). the unit interval on the real line (of cardinality ) is the continuous image of the Cantor set. level+1=4 [edit]Types of binary trees  A rooted binary tree is a tree with a root node in which every node has at most two children. The cardinal number of the set of all nodes is ..2 Encoding general trees as binary trees • 11 See also • 12 Notes • 13 References • 14 External links [edit]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).[4]  An infinite complete binary tree is a tree with levels.1 Succinct encodings o 10.  The depth of a node n is the length of the path from the root to the node. .  A node p is an ancestor of a node q if it exists on the path from q to the root. this tree is sometimes called the Cantor space.then. and all nodes are as far left as possible. Balanced trees have a predictable depth (how many nodes are traversed from the root to a leaf.  Out-degree of a node is the number of edges leaving that node.  The root is the only node in the tree with In-degree = 0..  Siblings are nodes that share the same parent node. depth). except possibly the last.[3] (This is ambiguously also called a complete binary tree.  A perfect binary tree is a full binary tree in which all leaves are at the same depth or same level.)  A complete binary tree is a binary tree in which every level. The infinite complete binary tree essentially describes the structure of the Cantor set. where for each level d the number of existing nodes at level d is equal to 2d.  A full binary tree (sometimes proper binary tree or 2-tree or strictly binary tree) is a tree in which every node other than the leaves has two children..  A balanced binary tree is where the depth of all the leaves differs by at most 1. This depth is equal to the integer part of log2(n) where n is the number of nodes on the balanced tree. size of tree is.

1 + A × α × α. tree with 1 node.1 B = n1 + 2*n2 n = n1+ 2*n2 + 1 n = n0 + n1 + n2 n1+ 2*n2 + 1 = n0 + n1 + n2 ==> n0 = n2 + 1 [edit]Type theory In type theory.  A rooted complete binary tree can be identified with a free magma.[5]  n = n0 + n1 + n2 + n4 + n3 + n5 + . and two children respectively. [edit]Definition in graph theory For each binary tree data structure. log2(5) = 2.32 (depth of tree is 2 nodes).. Example 3: balanced tree with 5 nodes.  The number of nodes n in a perfect binary tree can also be found using this formula: n = 2L − 1 where L is the number of leaf nodes in the tree.. log2(3) = 1. n0 = n2 + 1. n1.. B = n .1. n2 represent the nodes with no children.  A degenerate tree is a tree where for each parent node. NOT include n0 [edit]Proof proof: n0=n2+1 Let n = the total number of nodes B = the branches in T n0. + nB-1 + nB  B = n . .  The number of nodes n in a complete binary tree is minimum: n = 2h and maximum: n = 2h + 1 − 1 where h is the height of the tree.  The number of leaf node in a Complete Binary Tree of n-node is UpperBound(n / 2).59 (depth=1). n = 1 + 1*n1 + 2*n2 + 3*n3 + 4*n4 + . This means that in a performance measurement. the tree will behave like a linked list data structure. a binary tree with nodes of type A is defined inductively as TA = μα. there is only one associated child node. Note that this terminology often varies in the literature. + B*nB. Example 2: balanced tree with 3 nodes. especially with respect to the meaning "complete" and "full".  For any non-empty binary tree with n0 leaf nodes and n2 nodes of degree 2. there is equivalent rooted binary tree in graph theory. single child.. [edit]Properties of binary trees  The number of nodes n in a perfect binary tree can be found using this formula: n = 2h + 1 − 1 where h is the height of the tree. log2(1) = 0 (depth = 0).  The number of leaf nodes n in a perfect binary tree can be found using this formula: n = 2h where h is the height of the tree.  The number of NULL links in a Complete Binary Tree of n-node is (n+1)..

surrounded by parenthesis. some of the child pointers may be set to a special null value.Graph theorists use the following definition: A binary tree is a connected acyclic graph such that the degree of each vertex is no more than three. [edit]Methods for storing binary trees Binary trees can be constructed from programming language primitives in several ways. (0 (a 0)). allowing multiple connected components in the graph. one of which is a 3-tuple of data. and the other of which is a "leaf" node. each vertex will have a uniquely defined parent. The set of all possible strings consisting entirely of balanced parentheses is known as the Dyck language. For example. but does fix a specific root node. there are exactly two more nodes of degree one than there are of degree three. and up to two children. together with the natural operation of attaching trees to one-another. Sometimes it also contains a reference to its unique parent. (0 (0 a)). (a b) denotes the binary tree whose left subtree is a and whose right subtree is b. With the root thus chosen. the operators to obtain the left and right subtrees are sometimes referred to as car and cdr. In languages with tagged unions such as ML. a tree node is often a tagged union of two types of nodes. The ability to represent binary trees as strings of symbols and parentheses implies that binary trees can represent the elements of a magma. [edit]Nodes and references In a language with records and references. and right child. Conversely. This also does not establish the order of children. the set of all possible binary trees.  A graph formed by taking two binary trees. [edit]Combinatorics The groupings of pairs of nodes in a tree can be represented as pairs of letters. It can be shown that in any binary tree of two or more nodes. however. Thus. adding a vertex. 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. the total number of ways in which these nodes can be arranged into a binary tree is given by the Catalan number Cn. Another way of defining binary trees is a recursive definition on directed graphs. ((0 a) 0). Given a string representing a binary tree. and C3 = 5 declares that ((a 0) 0). which contains no data and functions much like the null value in a language with pointers. left child. but there can be any number of nodes of degree two. C2 = 2 declares that (a 0) and (0 a)are the only binary trees possible that have two nodes. A binary tree is either:  A single vertex. Given n nodes. If we drop the connectedness requirement. Here 0 represents a subtree that is not present. the free magma. so far there is insufficient information to distinguish a left or right child. and (a b) are the only five binary trees possible that have 3 nodes. forms a magma. If a node has fewer than two children. or to a specialsentinel node. A rooted binary tree is such a graph that has one of its vertices of degree no more than two singled out as the root. [edit]Ahnentafel list . and adding an edge directed from the new vertex to the root of each binary tree. we call such a structure a forest. Strings of balanced pairs of parenthesis may therefore be used to denote binary trees in general.

root after  In-Order: Left child. in-order. See depth-first search for more information. Also called a level-order traversal. We can't insert a new node into array implemented binary tree with ease. if between.Binary trees can also be stored as an implicit data structure in arrays. each node(root. if after. If the root node is visited before its subtrees. In the case of array. and if the tree is a complete binary tree. [edit]Pre-order.left. [edit]Encodings [edit]Succinct encodings . which always attempts to visit the node closest to the root that it has not already visited. one wishes to visit each of the nodes in a tree and examine the value there. Unlike a depth-first search on graphs. root. there is no need to remember all the nodes we have visited. In-order traversal is useful in binary search trees. and post-order traversal visit each node in a tree by recursively visiting each node in the left and right subtrees of the root. but with the caveat that it must be a child of a node we have already visited. while its parent (if any) is found at index (assuming the root has index zero). [edit]Breadth-first order Contrasting with depth-first order is breadth-first order. There are several common orders in which the nodes can be visited. But In linked list representation we can find the relationship between parent and children. in-order. its children are found at indices 2i + 1(for the left child) and 2i + 2(for the right). However.right) is simply placed in the index and there is no connection mentioned about the relationship between parents and children. This method benefits from more compact storage and better locality of reference. and post-order traversal Main article: Tree traversal Pre-order. [edit]Methods of iterating over binary trees Often. where this traversal visits the nodes in increasing order. This method is used in languages like FORTRAN which doesn't have dynamic memory allocation. if a node has an index i. it is expensive to grow and wastes space proportional to 2h -n for a tree of height h with n nodes. because a tree cannot contain cycles. right child. In array representation the nodes are accessed by calculating the index. Pre-order is a special case of this. A binary tree can also be represented in the form of array as well as adjacency linked list. this is pre-order. See Breadth-first search for more information. particularly during a preorder traversal. this method wastes no space. post-order. In this compact arrangement. we always attempt to visit the node farthest from the root that we can. in-order. [edit]Depth-first order In depth-first order. but this is easily done when using a binary tree implemented as linked list. children after  Post-Order: Children first. and each has useful properties that are exploited in algorithms based on binary trees:  Pre-Order: Root first.

left = DecodeSuccinct(structure. To convert a general ordered tree to binary tree. we can convert the output back to the original tree like this: function DecodeSuccinct(bitstring structure. through its left field. we don't even have to store its length. [1] If the tree contains data. The number of different binary trees on nnodes is Cn. For large n. and the node only has a pointer to the beginning or head of this list. One simple representation which meets this bound is to visit the nodes of the tree in preorder. we can simply simultaneously store it in a consecutive array in preorder.that is. array data) { if n = nil then append 0 to structure.left. [edit]Encoding general trees as binary trees There is a one-to-one mapping between general ordered trees and binary trees. if viewed from a different perspective. where n is the number of (internal) nodes. . structure. bitstring structure. The result of this representation will be automatically binary tree. or a Filial-Heir chain. EncodeSuccinct(n. we only need to represent the general tree in left child-sibling way. the nth Catalan number (assuming we view trees with identical structure as identical).data to data. else append 1 to structure. and the right child of N' is the node corresponding to N 's next sibling --. the next node in order among the children of the parent of N. structure. One way of thinking about this is that each node's children are in a linked list. which in particular is used by Lisp to represent general ordered trees as binary trees. This function accomplishes this: function EncodeSuccinct(node n. A succinct binary tree therefore would occupy only 2 bits per node. data). or a Doubly-chained tree. To show that no information is lost.right = DecodeSuccinct(structure. Each node N in the ordered tree corresponds to a node N' in the binary tree. EncodeSuccinct(n. this is about 4n. This binary tree representation of a general order tree is sometimes also referred to as a Left child-right sibling binary tree (LCRS tree). append n.A succinct data structure is one which takes the absolute minimum possible space. array data) { remove first bit of structure and put it in b if b = 1 then create a new node n remove first element of data and put it in n. data) return n else return nil } 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.right. } The string structure has only 2n + 1 bits in the end. thus we need at least about log24n = 2n bits to encode it. data) n. the left child of N' is the node corresponding to the first child of N. as established by information theoretical lower bounds. data). outputting "1" for an internal node and "0" for a leaf. chained together with their right fields.

For example.F. [edit]See also  2-3 tree  2-3-4 tree  AA tree  B-tree  Binary space partitioning  Elastic binary tree  Kraft's inequality  Random binary tree  Recursion (computer science)  Self-balancing binary search tree  Strahler number  Threaded binary  Unrooted binary tree [edit]Notes 1. in the tree on the left.D. without any letters on those nodes that have a left child. The binary tree can be thought of as the original tree tilted sideways.E. A has the 6 children {B.laynetworks. It can be converted into the binary tree on the right. The leaves of the tree on the left would be written in Lisp as: (((N O) I J) C D ((P) (Q)) F (M)) which would be implemented in memory as the binary tree on the}.htm . with the black left edges representing first child and the blue right edges representing next sibling. ^ http://www.

1–2. Algorithms: Parallel. ^ "complete binary tree". [edit]References  Donald Knuth. ISBN 0-534-42057-5. Section 2.b)-tree · Dancing tree · Htree · B -tree Tries Suffix tree · Radix tree · Ternary search tree Binary space partitioning (BSP) trees Quadtree · Octree · kd-tree (implicit) · VP-tree Non-binary trees Exponential tree · Fusion tree · Interval tree · PQ tree · Range tree · SPQR tree Spatial data partitioning trees R-tree · R+ tree · R* tree · X-tree · M-tree · Segment tree · Fenwick tree · Hilbert R-tree Other trees Heap · Hash tree · Finger tree · Metric tree · Cover tree · BK-tree · Doubly-chained tree · iDistance Categories: Binary trees | Data structures • New features • Log in / create account • Article • Discussion • Read • Edit • View history . 2005. ^ "perfect binary tree". Fundamental Algorithms. 2. The art of computer programming vol 1. or a balanced binary search tree on array [hide] v d e • • Trees in computer science Binary trees Binary search tree (BST) · Van Emde Boas tree · Cartesian tree · Top Tree · T-tree Self-balancing binary search trees Red-black tree · AVL tree · AA tree · Splay tree · Scapegoat tree · Treap B-trees x B+ tree · B*-tree · UB-tree · 2-3 tree · 2-3-4 tree · (a.2 (pp. Chapter 4. (pp. 1997. ^ Mehta.3. ^ http://www. Course Technology.Chapman and Hall. Third Edition.htm 3. NIST. Addison-Wesley.laynetworks. 5.3. NIST. Sequential and Distributed. Dinesh. [edit]External links  flash actionscript 3 opensource implementation of binary tree — opensource library  [2] — GameDev. 113–166). Jerome L Paul. ISBN 1584884355. especially subsections  Kenneth A Berman. Handbook of Data Structures and Applications. ISBN 0-201-89683-4. Sartaj Sahni (2004).3. 4. 318–348).net's article about binary trees  [3] — Binary Tree Proof by Induction  Balanced binary search tree on array How to create bottom-up an Ahnentafel list.

Wikipedia® is a registered trademark of the Wikimedia Foundation.• Main page • Contents • Featured content • Current events • Random article • Donate Interaction • About Wikipedia • Community portal • Recent changes • Contact Wikipedia • Help Toolbox Print/export Languages • Български • Català • Česky • Deutsch • Español • Esperanto • Français • 한국어 • Bahasa Indonesia • Íslenska • Italiano • ‫עברית‬ • 日本語 • Polski • Português • Română • Русский • Slovenčina • Slovenščina • Српски / Srpski • Suomi • Svenska • Українська • 中文 • This page was last modified on 18 October 2010 at 16:59. See Terms of Use for details.. a non-profit organization. • Contact us • Privacy policy • About Wikipedia • Disclaimers . • Text is available under the Creative Commons Attribution-ShareAlike License. Inc. additional terms may apply.

• • .