This action might not be possible to undo. Are you sure you want to continue?

BooksAudiobooksComicsSheet Music### Categories

### Categories

### Categories

Editors' Picks Books

Hand-picked favorites from

our editors

our editors

Editors' Picks Audiobooks

Hand-picked favorites from

our editors

our editors

Editors' Picks Comics

Hand-picked favorites from

our editors

our editors

Editors' Picks Sheet Music

Hand-picked favorites from

our editors

our editors

Top Books

What's trending, bestsellers,

award-winners & more

award-winners & more

Top Audiobooks

What's trending, bestsellers,

award-winners & more

award-winners & more

Top Comics

What's trending, bestsellers,

award-winners & more

award-winners & more

Top Sheet Music

What's trending, bestsellers,

award-winners & more

award-winners & more

Welcome to Scribd! Start your free trial and access books, documents and more.Find out more

A rooted tree is a tree with a countable number of nodes, in which a particular node is distinguished from the others and called the root:

In some contexts, in which only a rooted tree would make sense, the term tree is often used.

Infinite Tree

A rooted tree is infinite if it contains a countably infinite number of nodes.

Finite Tree

Similarly, a rooted tree is finite if it contains a finite number of nodes.

Parent

Consider a rooted tree T whose root is r T

Let t be a node of T From Paths in Trees are Unique, there is only one path from t to r T Let π:T−{r T

}→T be the mapping defined as: π(t)= the node adjacent to t on the path to r T

Then π(t) is known as the parent (or parent node) of t , and π as the parent function or parent mapping.

Root Node

The root node, or just root, is the one node in a rooted tree which, by definition, has no parent.

Ancestor

An ancestor (or ancestor node) of a node t of a rooted tree T whose root is r T is a node in the path from t to r T . Thus, the root of a rooted tree T is the ancestor of every node of T (including itself).

Proper Ancestor

A proper ancestor of a node t is an ancestor of t which is not t itself.

Children

The children (or child nodes) of a node t in a rooted tree T are the elements of the set:

{s∈T:π(s)=t}

That is, the children of t are all the nodes of T of which t is the parent.

The child of a child node of a node t is a grandchild node of t .

Descendant

A descendant (or descendant node) s of a node t of a rooted tree T whose root is r T is a node such that t is in the path from s to r T . That is, the descendants of t are all the nodes of T of which t is an ancestor.

Proper Descendant

A proper descendant of a node t is a descendant of t which is not t itself.

Sibling

Two children of the same node of a rooted tree are called siblings. That is, siblings are nodes which both have the same parent.

Leaf Node

A leaf node (or a terminal node, or just leaf) of a rooted tree T is a node of T which has no children.

Branch

A subset Γ of a rooted tree T is a branch iff:

The root node r T belongs to Γ ; The parent of each node in Γ−{r T } is in Γ ; Each node in Γ either: o Is a leaf node of T ; o Has exactly one child in Γ .

Hence a node in T with more than one child will be on more than one branch. A leaf node will be on exactly one branch.

The length of a branch is defined as the number of ancestors of the leaf at the end of that branch.

Informally, then, a branch of a rooted tree is the path from the root to a leaf. Note, however, that Γ is infinite iff it has no leaf node at the end.

Sources

**Tree (graph theory)
**

From Wikipedia, the free encyclopedia

Jump to: navigation, search This article includes a list of references, related reading or external links, but its sources remain unclear because it lacks inline citations. Please improve this article by introducing more precise citations. (March 2012)

Trees

A labeled tree with 6 vertices and 5 edges

Vertices

v

Edges

v-1

Chromatic number

2 if v > 1

v t e

In mathematics, more specifically graph theory, a tree is an undirected graph in which any two vertices are connected by exactly one simple path. In other words, any connected graph without cycles is a tree. A forest is a disjoint union of trees. The various kinds of data structures referred to as trees in computer science are equivalent to trees in graph theory, although such data structures are commonly rooted trees, and may have additional ordering of branches.

Contents

[hide]

1 Definitions 2 Example 3 Facts 4 Enumeration o 4.1 Labeled trees o 4.2 Unlabeled trees 5 Types of trees 6 See also 7 References

[edit] Definitions

A tree is an undirected simple graph G that satisfies any of the following equivalent conditions:

G is connected and has no cycles. G has no cycles, and a simple cycle is formed if any edge is added to G. G is connected, but is not connected if any single edge is removed from G.

2. Some authors restrict the phrase to the case where the edges are all directed towards a particular vertex. every vertex except the root has a unique parent. then the label of u is smaller than the label of v). a forest is an undirected cycle-free graph. p. the graph with these vertices that has no edges). G has no simple cycles and has n − 1 edges. say n of them. in other words. n. then the above statements are also equivalent to any of the following conditions: G is connected and has n − 1 edges. As special cases. A polytree or oriented tree is a directed graph with at most one undirected path between any two vertices. G is connected and the 3-vertex complete graph is not a minor of G.. . towards or away from the root. the graph consists of a disjoint union of trees. and the discrete graph on a set of vertices (that is. Rooted trees. all of whose connected components are trees. An ordered tree or plane tree is a rooted tree for which an ordering is specified for the children of each vertex. A rooted tree which is a subgraph of some graph G is a normal tree if the ends of every edge in G are comparable in this tree-order whenever those ends are vertices of the tree (Diestel 2005. The tree-order is the partial ordering on the vertices of a tree with u ≤ v if and only if the unique path from the root to v passes through u. a single tree. see tree data structure. The vertices of a labeled tree on n vertices are typically given the labels 1. the parent of a vertex is the vertex connected to it on the path to the root. In a context where trees are supposed to have a root. A recursive tree is a labeled rooted tree where the vertex labels respect the tree order (i. Equivalently. The term hedge sometimes refers to an ordered sequence of trees. If G has finitely many vertices. A forest is an undirected graph. are a key data structure in computer science. In other words. in which case the edges have a natural orientation. a tree without any designated root is called a free tree. In a rooted tree. or all directed away from a particular vertex (see arborescence). …. an empty graph. Any two vertices in G can be connected by a unique simple path.e. all are examples of forests. A tree is called a rooted tree if one vertex has been designated the root. An irreducible (or series-reduced) tree is a tree in which there is no vertex of degree 2. A labeled tree is a tree in which each vertex is given a unique label. A leaf is a vertex without children. if u < v for two vertices u and v. often with additional structure such as ordering of the neighbors at each vertex. 15). a polytree is a directed acyclic graph for which there are no undirected cycles either. A child of a vertex v is a vertex of which v is the parent. A directed tree is a directed graph which would be a tree if the directions on the edges were ignored.

[edit] Enumeration [edit] Labeled trees Cayley's formula states that there are nn−2 trees on n labeled vertices. The unique simple path connecting the vertices 2 and 6 is 2-4-5-6. the three paths between them have exactly one vertex in common. There exist connected graphs with uncountably many vertices which do not admit a normal spanning tree (Diestel 2005.An n-ary tree is a rooted tree for which each vertex has at most n children.2.. [edit] Example The example tree shown to the right has 6 vertices and 6 − 1 = 5 edges.. is the multinomial coefficient An alternative proof uses Prüfer sequences. Every tree with only countably many vertices is a planar graph. Prop. while 3-ary trees are sometimes called ternary trees. is attained by star graphs.. Every connected graph with only countably many vertices admits a normal spanning tree (Diestel 2005. 8. which is a tree that contains every vertex of G and whose edges are edges of G. .. with n > 1.5. the maximal number. is a terminal vertex if it has precisely one child. 8.. of degrees d1. A terminal vertex of a tree is a vertex of degree 1. if not a leaf itself.2. Every connected graph G admits a spanning tree.. the root. has at least two terminal vertices (leaves). 2-ary trees are sometimes called binary trees.dn respectively. It can be proved by first showing that the number of trees with vertices 1. This minimal number of terminal vertices is characteristic of path graphs.n. the leaves are all terminal vertices.4). In a rooted tree. additionally. [edit] Facts Every tree is a bipartite graph and a median graph. Every finite tree with n vertices.d2.2). For any three vertices in a tree.. Prop.. n − 1.

11. Knuth (1997). 47. 1. 1. No closed formula for the number t(n) of trees with n vertices up to graph isomorphism is known. 6. (sequence A000055 in OEIS). Otter (1948) proved the asymptotic estimate: with C = 0.4. Its diameter is at most 2.4 and Flajolet & Sedgewick (2009).5). 551. Chap.99557658565…. . (Here.3. 23. then the tree is a caterpillar tree. 3. a star graph of order n is a tree of order n with as many leaves as possible. 1301. The first few values of t(n) are: 1.43992401257… and α the same as above (cf. Chap. In other words. then the tree is a lobster. A tree with two terminal vertices (the fewest possible) is a path graph. VII. If all nodes in a tree are within distance one of a central path subgraph. [edit] Types of trees A star graph is a tree which either has order n ≤ 2. 106... which is addressed by the matrix tree theorem. 235. . [edit] Unlabeled trees Counting the number of unlabeled free trees is a harder problem. or consists of a single internal node (and n − 1 leaves). 2. The similar problem of counting all the subtrees regardless of size has been shown to be #P-complete in the general case (Jerrum (1994)).Cayley's formula is the special case of complete graphs in a more general problem of counting spanning trees in an undirected graph.534949606… and α = 2.) This is a consequence of his asymptotic estimate for the number trees with n vertices: means that of unlabeled rooted with D = 0. 3159. If all nodes are within distance two of a central path subgraph. [edit] See also Decision tree Pseudoforest Tree structure Tree data structure Unrooted binary tree . 1. 2.

"Counting trees in a graph is #P-complete". "The Number of Trees". The Art of Computer Programming Volume 1: Fundamental Algorithms. Graph Theory (3rd ed.). Sedgewick. Analytic Combinatorics. ISBN 978-0-521-89806-5 Donald E. Philippe. Flajolet. Second Series 49 (3): 583–599.org/w/index. Robert (2009).wikipedia.1016/0020-0190(94)00085-9. Knuth. Otter. http://diestel-graph-theory. Annals of Mathematics. ISBN 978-3-540-26183-4.[edit] References Wikimedia Commons has media related to: decision diagrams Diestel. JSTOR 1969046. Berlin. doi:10.com/index. Jerrum. Richard (1948). New York: Springer-Verlag. ISSN 0020-0190.php?title=Tree_(graph_theory)&oldid=482495260" View page ratings Rate this page What's this? Trustworthy Objective Complete Well-written I am highly knowledgeable about this topic (optional) Submit ratings Saved successfully Your ratings have not been submitted yet Categories: Trees (graph theory) Hidden categories: Articles lacking in-text citations from March 2012 All articles lacking in-text citations . Information Processing Letters 51 (3): 111–116. Mark (1994). 1997). Reinhard (2005).html.2307/1969046. Retrieved from "http://en. Cambridge University Press. 3rd edition (November 14. Addison-Wesley Professional. doi:10.

. called root and zero or more subtrees T1. There is such a thing as a NULL tree -. Nobody draws the edges as directed edges. . This definition has the edges directed to the root. Trees defined this way are "rooted'' trees. and r is called the parent of each child.1 Definitions for Rooted Trees DEFINITION: A tree is a set of nodes. edges are sometimes called lines. Not all trees are considered to be rooted. Not every author allows this. If not empty. there is a distinguished node r.. perhaps empty..Tk each of whose roots are connected by a directed edge to r.a tree with no nodes. This means there is a direction associated with them. The degree of a tree is the maximum degree of any of its nodes. Nodes are sometimes called vertices or points. it's just understood. They have a distinguished root. Note that the edges are directed edges.T2... DEFINITION: Nodes with the same parent are called siblings. All other nodes are called interior nodes. DEFINITION: The degree of a node is the number of its children. DEFINITION: Nodes with no children are called leaf nodes.31 Rooted Trees 31. The root of each subtree is called a child of r.

node A) has degree 3. this is also shown in the figure. Generic trees are usually shown in a schematic form with the subtrees indicated by triangles. The nodes are labelled for convenience. The details of the subtrees are hidden in the "triangles'" . The tree has degree 3 since the node with maximum degree (the root. The firgure below shows such a representation.The figure shows a rooted tree with edges undirected from child to parent. The more traditional way to draw the tree is with undirected edges.

1) There is a path of length zero from every node to itself. The height and depth of a tree are equal. DEFINITION: A path from node n1 to node nk is a sequence of nodes n1. Every node is both an ancestor and a descendent of itself. nk such that ni is the parent of n i+1 for 1 <= i < k. .. DEFINITION: If there is a path from node ni to node nj. In a tree there is one and only one path from the root to each node... An ancestor (descendent) of a node which is not the node itself is called a proper ancestor (descendent) of the node. DEFINITION: The height of a tree is the height of its root. then node ni is an ancestor of node nj and node nj is a descendent of node ni... DEFINITION: The depth of a node is the length of the path from the root to the node The root is at depth 0. DEFINITION: The depth of a tree is the depth of its deepest leaf. The height of any leaf is 0. The length of this path is the number of edges on the path (namely k . n2. DEFINITION: The height of any node is the longest path from the node to any leaf..

. general_node * child4.1 edges in any tree with n nodes Proof: Each edge connects a node to its parent.31. NextSibling Representation of General Rooted Trees One could imagine representing a general node as a struct: struct general_node { Element_Type element. }. . general_node *next_sibling.3 FirstChild. A better representation is the ``firstchild. nextsibling'' representation. . An obvious problem with this representation is that the number of children would have to be large enough to cover all the expected cases. nextsibling" representation of the rooted tree shown above. Every node except the root has a parent. struct general_node { Element_Type element. The downwardpointing edges are the "first-child" edges.2 Theorems about Trees Theorem There are n . general_node * child3. The horizontal edges are the "next-sibling" edges. }. . general_node * child2. . This would usually mean wasting a lot of pointers. general_node * child1. general_node *first_child. 31. The figure below shows the "firstchild.

// forward declaration template <class Etype> class Binary_Search_Tree. Not every rooted tree with degree two is a binary tree. DEFINITION: A full binary tree is one in which every leaf node is at the same level. DEFINITION: A complete binary tree is a full binary tree except perhaps for the final level which is filled from left to right.32 Binary Trees DEFINITION: A binary tree is a rooted tree in which no node can have more than two children. and the children are distinguished as left and right.there is a "left'' child and a "right'' child. and every interior node has exactly two children. It keeps pointers to its left and right children. The children must be in order -. Here's the definition of a Tree Node for binary trees. .

Tree_Node(elementvalue. Tree_Node().1 If a binary tree (BT) of height h has t leaves. Tree_Node *R = NULL ) : Element( E ). in this case Element will be 0. Element will have the value elementvalue. nodeptr2). Right( R ) { } friend class Binary_Search_Tree<Etype>. Binary Search Tree is made a friend of Tree Node. in this case. Tree_Node(elementvalue. Therefore. Left and Right will be NULL. We prove the latter by induction on h. Left will have the value nodeptr1 and Right will have the value nodeptr2. Left( L ). The constructor takes an element and two pointers as arguments (with default values for all). Element will have the value elementvalue. Left will have the value nodeptr1 and Right will be NULL. The constructor can be called in any of the following ways: 1. t = 1 = 20 . 32.1 Theorems About Binary Trees Theorem 32. then h >= lg t Proof: If h >= lg t. then equivalently t <= 2 h . and both Left and Right children will be NULL. Tree_Node( Etype E = 0. Tree_Node *Right. in this case.template <class Etype> class Tree_Node { protected: Etype Element. 2. Tree_Node(elementvalue). Element will have the value elementvalue. nodeptr1. }. Tree_Node *L = NULL. Tree_Node *Left. Base: h = 0 (single node). 3. < BR> 4. nodeptr1). in this case.

l. Then the left subtree has height h .2 A full binary tree (FBT) of height h has 2h leaf nodes. 0 < h <= H have 2 h leaf nodes. T. the total number of nodes is SUMht=0 2t = 2h+1 .1 + 2h . By IH. namely 2 X 2H = 2 H+1. Without loss of generality. For a FBT. By IH. of height H. Since the leaf nodes of T are partitioned between the two subtrees. We consider two possible cases: Case 1: Root of T has only one child. t = t1 + t2 .1 Therefore.1 < 2h Case 2: Root has two children.IH: Assume true for every BT with height less than h Let T be a BT of height h > 0 and with t leaf nodes. Theorem 32. Since there are 2l nodes at each level.1 t <= 2h Theorem 32. IH: Assume all full binary trees of height h. The height of T' is H + 1 and T' has twice the nuber of leaf nodes as T . let it be the left child.1 Proof: The number of nodes is the sum of the number of nodes on each level. t1 <= 2h1 and t 2 <= 2 h 2 .1 and h 2 <= h . Proof: By induction on h Base: A FBT of height 0 has 1 node (the root). h1 <= h . Let t1 and t2 be the number of leaf nodes and h 1 and h 2 the heights of the two subtrees. create a new FBT T' by adding exactly two children to each leaf of T .1 and it has all the leaf nodes of T. respectively. t <= 2 h . t <= 2h1 + 2h2 t <= 2h . Also. .3 The number of nodes in a full binary tree of height h is 2 h+1 .1 The equation is easily proven by induction.

Therefore. 286.1 + 1 <= n <= 2h+1 . then 2h <= n < 2h+1 Proof: A CBT of height h has at least one and as many as 2h nodes at level h (the final level is not empty. 4766. In any event. .1.3 above) A CBT of height h has at least one more node than a FBT of height h . Rooted Tree A rooted tree is a tree in which a special ("labeled") node is singled out. 2. 2h <= n < 2h+1 Equivalently.1 Therefore. nodes by . pp. This node is called the "root" or (less commonly) "eve" of the tree. (h > 0) .. although the unqualified term "tree" generally refers to a free tree. 1842. the number of nodes n in a CBT is bounded by 2(h .4 If n is the number of nodes in a complete binary tree (CBT) of height h. 2. 1. 115. Rooted trees are equivalent to oriented trees (Knuth 1997. 4. 9. Denote the number of rooted trees with .1) + 1 . level h .must be full by definition of a CBT.1) + 1 . The number of nodes in a FBT of height h . . A tree which is not rooted is sometimes called a free tree. are 1. 719. The numbers of rooted trees on nodes for .1 is 2(h . A rooted tree in which the root node has vertex degree 1 is known as a planted tree.1.Theorem 32. 48.. then the generating function is (Sloane's A000081). we can say h <= lg n < h + 1... and may be full). (By Theorem 32. 385-399). 20.1.

Knuth 1997. Odlyzko 1995.(1) (2) This power series satisfies (3) (4) where is the generating function for unrooted trees. As shown by Otter (1948). then an asymptotic series for is given by . p. where is given by the unique positive root of (9) If is the number of nonisomorphic rooted trees on nodes. (7) (8) (Sloane's A051491. 396). where the second sum is over all which divide (Finch 2003). A generating function for involving the sequence itself as can be written using a product (5) The number of rooted trees can also be calculated from the recurrence relation (6) with and .

A. 2003. Philadelphia." Wilf. R.inria. 1956." In Handbook of Combinatorics. G. 1948. "The Number of Trees. MA: Addison-Wesley. Mathematics by Experiment: Plausible Reasoning in the 21st Century. Grötschel. Finch. "Information on Rooted Trees. pp. Sequences A000081/M1180 and A051491 in "The On-Line Encyclopedia of Integer Sequences. 1995. 295-316. F. S. J.(10) where the constants can be computed in terms of partial derivatives of the function (11) (Plotkin and Rosenthal 1994. Math. 49. pp. N.ca/~cos/inf/tree/RootedTree. J. Finch 2003). 3rd ed. http://algo." Amer. Graham. D. Otter. R. 2 (Ed. 689-697. Plotkin. W. A 56.6 in Mathematical Constants. The Art of Computer Programming. 1989. D. England: Cambridge University Press. L.enum. 2003. E. Ordered Tree. "Two Asymptotic Series. pp. Math. H. A. and Bailey. Cambridge. Reading. "On Picture-Writing. Planted Tree. 1063-1229. and Palmer. 2003.edu/~odlyzko/doc/asymptotic. Knuth. A. Odlyzko. Combinatorial Algorithms: An Update. Vol.pdf. Lovász). New York: Academic Press. 583-599. PA: SIAM.1 in Graphical Enumeration. R." J." http://www. p. Weakly Binary Tree REFERENCES: Borwein. Harary. http://www. MA: Addison-Wesley. Finch. Austral. 51-54. 1997. MA: MIT Press. S.umn. M. Reading. and Rosenthal. Cambridge. Ruskey. 1994. 1973. M. Rooted Graph. Wellesley. H.html. J. 2nd ed. Combinatorial Algorithms for Computers and Calculators. Math. 187-190 and 232. 1: Fundamental Algorithms. F. Ser. Vol. Monthly 63. Graph Theory. 131-143. E. and Wilf. S. Harary. Soc. "How to Obtain an Asymptotic Expansion of a Sequence from an Analytic Identity Satisfied by Its Generating Function." December 10." Ann. M. pp. 22. MA: A K Peters." §5. F. SEE ALSO: Free Tree. New York: Academic Press. Information on Rooted Trees . Pólya. J.theory.fr/bsolve/. "Otter's Tree Enumeration Constants. 1978.csc. Tree.dtc." §3. Nijenhuis. Sloane. "Rooted Trees. and L. Red-Black Tree.uvic. M. "Asymptotic Enumeration Methods. 1994.

2. . This is called the parent array. In the figure above the number of ordered trees in the equivalence class of each rooted tree is 1. 1. 2. the sequence obtained is 0121156685. respectively. Another way of encoding an ordered tree is to record the level (distance from the root) at which its nodes occur in preorder. A rooted tree may be regarded as an equivalence class of ordered trees. Two ordered trees are equivalent if one may be transformed into the other by re-ordering subtrees. 3. 1. 1. 2. 1. One natural way of encoding an ordered tree is to record the parent of each node in preorder. In the figure on the left each node points to its parent.A rooted tree may be defined as a free tree in which some node has been distinguished as the root. Taking 0 to be the parent of the root. the 5th Catalan number and the number of ordered trees on 5 nodes. This is called the level sequence. Roots are shown in blue. The sum of these numbers is 14. In the tree shown on the right the numbers give a preorder labelling and by recording the level number of those nodes we obtain the sequence 0121123342. Above we show a list of all 9 rooted trees on 5 vertices.

Binary Rooted Trees An important subclass of the rooted trees are the those in which every node has either two children or is a leaf (has no children).3.e.. 12486. 32973.. The list of all trees with 5 nodes shown at the top of this page shows the canonic tree from each equivalence class.The two trees shown above are the same when regarded as unlabelled rooted trees. 115. 48. since one may be obtained from the other by reordering subtrees. The second tree illustrated is canonic (the one with parent array 0123432181). For n = 1. we transform an ordered tree into a canonic tree by recursively pushing subtrees with greater height to the left. biggest when regarded as a number) and call it canonic. 4766. 87811. 4. Intuitively. 9. Sloane's database of integer sequences. we generate all rooted trees. 1. 719. 2. This is sequence A000081(M1180) in Neil J... Among all ordered trees that are equivalent we choose the one whose parent array is lexicographically largest (i. By generating canonic ordered trees. 286. 20. These trees are output by the program by setting m = 2 ..15 the number of rooted trees is 1. 1842.2.

8.. 1. 1. 4.20 nodes. 10683. 23. respectively. 4850. 260.3. 45.. The number of binary rooted trees with n = 1. The asymptotic number of binary rooted trees has been studied by Otter and others. 207. 643.. 4.15 internal nodes is 1. Rooted trees where each node has at most 4 children are counted for n = 1.. 17.2. 19241. 983. 511624.2. 9881527. This is sequence A001190(M0790) in Neil J. 2. 11. Rooted trees where each node has at most 3 children are sometimes called quartic planted trees. respectively. 89. 2. 507. 1.(giving the list shown above). 2. 46. 3666930.. 7639. respectively. by the numbers 1.. 1366424.3. Sloane's database of integer sequences. For n = 1. there are 1. 27790.. 192548.3. as shown below. 3057. 4138.. Sloane's database of integer sequences. 106. 1624. 2179. This is sequence A000598(M1146) in Neil J.. 39..2. This is sequence A036718 in Neil J.15 internal nodes. 98. 1238. 48865 of them. You may click here for further information. Counts of rooted trees classified by height n 1 2 1 21 31 41 51 1 2 4 1 3 8 18 38 76 1 4 13 36 93 225 1 5 19 61 180 1 6 26 94 1 7 34 1 8 1 3 4 5 6 7 8 9 10 11 12 13 14 15 61 6 7 1 10 8 1 14 9 1 21 10 1 29 147 . 72917.. and then by adding n+1 leaves to the resulting trees. 211. 9. 19. Sloane's database of integer sequences.. 10905. 6. 451. 3.

An upper bound on the number of children of a node can be specified (as m). Number of Taxa 3 4 5 6 7 8 Number of unrooted trees 1 3 15 105 945 10395 Number of rooted trees 3 15 105 945 10395 135135 .*3*1=(2n-5)!/[2n-3*(n-3)!] Number of rooted trees for n taxa Nr=(2n-3)*(2n-5)*(2n-7)*.11 1 41 277 528 498 12 1 55 509 1198 1323 308 941 136 43 9 53 1 10 1 487 188 13 1 76 924 2666 3405 2744 1615 728 251 64 11 1 14 1 100 1648 5815 8557 7722 5079 2593 1043 326 76 12 1 15 1 134 2912 12517 21103 21166 15349 8706 3961 1445 414 89 13 1 The sum of the first two columns is the number of numerical partitions of n+1.. The sum of the first four columns is Sloane's A001384(M1172). The sum of the first three columns is Sloane's A001383(M1107)...*3*1=(2n-3)!/[2n-2*(n-2)!] Note that the number of unrooted trees for n sequences is equal for the number of rooted trees for (n-1) sequences. Number of Rooted and Unrooted Trees Number of unrooted trees for n taxa Nu=(2n-5)*(2n-7)*. The Advantages of Forward Thinking in Generating Rooted and Free Trees.. COS options In COS the user can output the parent array (standard representation) or the level sequence (alternate representation). 10th Annual ACM-SIAM Symposium on Discrete Algorithms (SODA). (1999) S939-940. The algorithm used in COS is from Gang Li and Frank Ruskey. Upper and lower bounds on the height can also be specified (as lb and ub).

Number of Taxa 9 10 20 30 40 50 60 70 80 Number of unrooted trees 135135 2027025 2. .18E+137 Number of rooted trees 2027025 34459425 8. The above tree is unbalanced and not sorted.84E+074 5. search Not to be confused with B-tree.95E+038 1. the free encyclopedia Jump to: navigation.01E+094 5.20E+021 4.01E+057 2. A simple binary tree of size 9 and depth 3.22E+020 8. with a root node whose value is 2.43E+139 For comparison the universe contains only about 1089 protons and has an age of about 5*1017 seconds or 5*1029 picoseconds.69E+036 1.00E+115 2. Binary tree From Wikipedia.75E+076 5.31E+055 2.85E+117 3.86E+096 6.

1 Succinct encodings o 9. Nodes with children are parent nodes. if it exists.2. Contents [hide] 1 Definitions for rooted trees 2 Types of binary trees 3 Properties of binary trees 4 Common operations o 4.1 Node with zero or one children 4. Binary trees are used to implement binary search trees and binary heaps.3.2.1 Insertion 4.1.2 Deletion 4. .3 Breadth-first order 5 Type theory 6 Definition in graph theory 7 Combinatorics 8 Methods for storing binary trees o 8. and child nodes may contain references to their parents. Outside the tree. a binary tree is a tree data structure in which each node has at most two child nodes.2 Depth-first order 4.2 Node with two children o 4.2 Encoding general trees as binary trees 10 See also 11 Notes 12 References 13 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).In computer science.1. usually distinguished as "left" and "right".3. and post-order traversal 4. in-order.1 Pre-order.3 Iteration 4.3.1 External nodes 4. there is often a reference to the "root" node (the ancestor of all nodes).2 Internal nodes o 4.1 Nodes and references o 8.2 Arrays 9 Encodings o 9. 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.

The root is the only node in the tree with In-degree = 0.[1] (This is ambiguously also called a complete binary tree. The depth of a node n is the length of the path from the root to the node. Siblings are nodes that share the same parent node. where is the number of nodes on the balanced tree. A node p is an ancestor of a node q if it exists on the path from the root to node q. There is at most one root node in a rooted tree. A perfect binary tree is a full binary tree in which all leaves are at the same depth or same level. except possibly the last. Example 3: balanced tree with 5 nodes. Sometimes a full tree is ambiguously defined as a perfect tree. (depth of tree is 2 nodes). 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. The root node of a tree is the node with no parents. Binary trees that are balanced according to this definition have a predictable depth (how many nodes are traversed from the root to a leaf. In-degree of a node is the number of edges arriving at that node. Example 1: (depth = 0). and in which every parent has two children. in which every node has two children. The set of all nodes at a given depth is sometimes called a level of the tree. A leaf node has no children. These paths corresponding by an order preserving bijection to the points of the Cantor set. depth)..) A complete binary tree is a binary tree in which every level. and all nodes are as far left as possible.[3] although in general it is a binary tree where no leaf is much farther away from the root than any other leaf.[2] An infinite complete binary tree is a tree with a countably infinite number of levels. Example 2: balanced tree with 3 nodes. All the leaf nodes have Out-degree = 0. (depth=1). Out-degree of a node is the number of edges leaving that node. This depth is equal to the integer part of balanced tree with 1 node. A (rooted) tree with only one node (the root) has a depth of zero. .. A balanced binary tree is commonly defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. The set of all nodes is countably infinite. The size of a node is the number of descendants it has including itself. (Different balancing schemes allow different definitions of "much farther"[4]). 2.. so that there are 2d nodes at level d. The node q is then termed as a descendant of p. or (through the example of the Stern–Brocot tree) to the set of positive irrational numbers. but the set of all infinite paths from the root is uncountable: it has the cardinality of the continuum. The depth (or height) of a tree is the length of the path from the root to the deepest node in the tree. is completely filled. [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 root node is at depth zero. root counting as node 0 and subsequent as 1. A rooted complete binary tree can be identified with a free magma. .

A degenerate tree is a tree where for each parent node. there is only one associated child node. The number of nodes in a perfect binary tree can also be found using this formula: where is the number of leaf nodes in the tree.[5] Proof: Let n = the total number of nodes B = number of branches n0. while others simply return useful information about the tree. Some are mutator operations. The number n–L of internal nodes (non-leaf nodes) in a Complete Binary Tree of n nodes is ⌊ ⌋. The number of null links (absent children of nodes) in a complete binary tree of n nodes is (n+1). For any non-empty binary tree with n0 leaf nodes and n2 nodes of degree 2. n0 = n2 + 1. the tree will behave like a linked list data structure. Note that this terminology often varies in the literature. and two children respectively. B = n . especially with respect to the meaning "complete" and "full". a single child. n1. n2 represent the number of nodes with no children. The number of leaf nodes in a perfect binary tree can be found using this formula: where is the depth of the tree. . [edit] Properties of binary trees The number of nodes in a perfect binary tree can be found using this formula: and at most where is the depth of the tree.1 (since all nodes except the root node come from a single branch) B = n1 + 2*n2 n = n1+ 2*n2 + 1 n = n0 + n1 + n2 n1+ 2*n2 + 1 = n0 + n1 + n2 ==> n0 = n2 + 1 [edit] Common operations There are a variety of different operations that can be performed on binary trees. The number of nodes in a complete binary tree is at least where is the depth of the tree. This means that in a performance measurement.

A assigns the new node as one of its children and the new node assigns node A as its parent.) A assigns its child to the new node and the new node assigns its parent to A.[edit] Insertion Nodes can be inserted into binary trees in between two other nodes or added after an external node. Then the new node assigns its child to B and B assigns its parent as the new node. [edit] Internal nodes The process of inserting a node into a binary tree Insertion on internal nodes is slightly more complex than on external nodes. Say that the internal node is node A and that node B is the child of A. [edit] Deletion Deletion is the process whereby a node is removed from the tree.[6] [edit] Node with zero or one children The process of deleting an internal node in a binary tree . In binary trees. To add a new node after node A. Only certain nodes in a binary tree can be removed unambiguously. a node that is inserted is specified as to which child it is. (If the insertion is to insert a right child. [edit] External nodes Say that the external node being added on to is node A. then B is the right child of A. and similarly with a left child insertion.

Left child. including binary search trees. See breadth-first search for more information. Also called a level-order traversal. a node with two children cannot be deleted unambiguously. Right child Post-Order: Left Child. Right child. There are several common orders in which the nodes can be visited.[6] However. in-order. set the parent of A's child to A's parent and set the child of A's parent to A's child. If a node has no children (external node). there is no need to remember all the nodes we have visited. [edit] Node with two children In a binary tree. which always attempts to visit the node closest to the root that it has not already visited. root. in-order. See depth-first search for more information. root In-Order: Left child. If it has one child. we always attempt to visit the node farthest from the root that we can. because a tree cannot contain cycles. [edit] Type theory In type theory. and each has useful properties that are exploited in algorithms based on binary trees: Pre-Order: Root first. right child. [edit] Breadth-first order Contrasting with depth-first order is breadth-first order. Unlike a depth-first search on graphs. a binary tree with nodes of type A is defined inductively as TA = μα. deletion is accomplished by setting the child of A's parent to null. a process called iteration or enumeration. [edit] Depth-first order In depth-first order. Pre-order is a special case of this. and post-order traversal Main article: Tree traversal Pre-order.Say that the node to delete is node A. [edit] Definition in graph theory . 1 + A × α × α. one wishes to visit each of the nodes in a tree and examine the value there. and post-order traversal visit each node in a tree by recursively visiting each node in the left and right subtrees of the root. in certain binary trees these nodes can be deleted. but with the caveat that it must be a child of a node we have already visited. [edit] Pre-order. [edit] Iteration Often.

then interchanging them will produce a tree distinct from the original one). Another way of defining binary trees is a recursive definition on directed graphs. there is equivalent rooted binary tree in graph theory. and the addition of redundant parentheses (around an already parenthesized expression or around the full expression) is disallowed (or at least not counted as producing a new possibility). This also does not establish the order of children. There is a unique binary tree of size 0 (consisting of a single leaf). and any other binary tree is characterized by the pair of its left and right children. the other nodes are leaf nodes and there are n + 1 of them. adding a vertex. If we drop the connectedness requirement. the full tree has size i + j + 1. With the root thus chosen. and adding an edge directed from the new vertex to the root of each binary tree. [edit] Combinatorics In combinatorics one considers the problem of counting the number of full binary trees of a given size. A binary tree is either: A single vertex. Here the trees have no values attached to their nodes (this would just multiply the number of possible trees by an easily determined factor). For instance for n = 3 one has to parenthesize a string like . 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. The number of such binary trees of size n is equal to the number of ways of fully parenthesizing a string of n + 1 symbols (representing leaves) separated by n binary operators (representing internal nodes). and up to two children. each vertex will have a uniquely defined parent. The size of the tree is taken to be the number n of internal nodes (those with two children). It can be shown that in any binary tree of two or more nodes. which is possible in five ways: The correspondence to binary trees should be obvious. so as to determine the argument subexpressions of each operator. so far there is insufficient information to distinguish a left or right child. but does fix a specific root node. there are exactly two more nodes of degree one than there are of degree three. however. however the left and right child of any node are distinguished (if they are different trees. 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. A graph formed by taking two binary trees. but there can be any number of nodes of degree two. Therefore the number of binary trees of size n has the following . we call such a structure a forest.For each binary tree data structure. and trees are distinguished only by their structure. if these have sizes i and j respectively. allowing multiple connected components in the graph.

recursive description . and if the tree is a complete binary tree. In this compact arrangement. this method wastes no space. left child. then the dotted-pair expression for that proper list is a fully parenthesized expression (with NIL as symbol and '. and right child. which consist only of parentheses in such a way that they are properly balanced. A bijective correspondence can nevertheless be defined as follows: enclose the Dyck word in a extra pair of parentheses. if a node has an index i. or to a special sentinel node. for any positive integer n. If a node has fewer than two children. a tree node is often a tagged union of two types of nodes. so that the result can be interpreted as a Lisp list expression (with the empty list () as only occurring atom). this number is therefore also the Catalan number . So there are also five Dyck words of length 10: . whose lengths 2i and 2j satisfy i + j + 1 = n). [edit] Methods for storing binary trees Binary trees can be constructed from programming language primitives in several ways. and follows that is the Catalan number of index n. It The above parenthesized strings should not be confused with the set of words of length 2n in the Dyck language. In languages with tagged unions such as ML. and the other of which is a "leaf" node. The ability to represent binary trees as strings of symbols and parentheses implies that binary trees can represent the elements of a free magma on a singleton set. its children are found at indices (for the left child) and (for . The number of such strings satisfies the same recursive description (each Dyck word of length 2n is determined by the Dyck subword enclosed by the initial '(' and its matching ')' together with the Dyck subword remaining after that closing parenthesis. [edit] Arrays Binary trees can also be stored in breadth-first order as an implicit data structure in arrays. 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. These Dyck words do not correspond in an obvious way to binary trees. Sometimes it also contains a reference to its unique parent.' as operator) describing the corresponding binary tree (which is in fact the internal representation of the proper list). [edit] Nodes and references In a language with records and references. which contains no data and functions much like the null value in a language with pointers. one of which is a 3-tuple of data. some of the child pointers may be set to a special null value.

thus we need at least about bits to encode it. [1] If the tree contains data. we can simply simultaneously store it in a consecutive array in preorder. we can convert the output back to the original tree like this: function DecodeSuccinct(bitstring structure. One simple representation which meets this bound is to visit the nodes of the tree in preorder. array data) { if n = nil then append 0 to structure. EncodeSuccinct(n. where is the number of (internal) nodes. it is expensive to grow and wastes space proportional to 2h . 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. while its parent (if any) is found at index (assuming the root has index zero). we don't even have to store its length. data). bitstring structure. structure. the th Catalan number (assuming we view trees with identical structure as identical). else append 1 to structure. EncodeSuccinct(n. This method of storage is often used for binary heaps.n for a tree of depth h with n nodes.the right). particularly during a preorder traversal. To show that no information is lost. } The string structure has only bits in the end. append n.data to data.right. However. A succinct binary tree therefore would occupy only 2 bits per node. The number of different binary trees on nodes is . This function accomplishes this: function EncodeSuccinct(node n. [edit] Encodings [edit] Succinct encodings A succinct data structure is one which takes the absolute minimum possible space. outputting "1" for an internal node and "0" for a leaf. as established by information theoretical lower bounds. No space is wasted because nodes are added in breadth-first order. This method benefits from more compact storage and better locality of reference.left.data . structure. this is about . data). For large .

.right = DecodeSuccinct(structure. 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. 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 --.G}. data) n. we only need to represent the general tree in left child-sibling way. [edit] Encoding general trees as binary trees There is a one-to-one mapping between general ordered trees and binary trees. This binary tree representation of a general order tree is sometimes also referred to as a left child-right sibling binary tree (LCRS tree).F. and the node only has a pointer to the beginning or head of this list. The above program only works for a full binary tree. One way of thinking about this is that each node's children are in a linked list. Each node N in the ordered tree corresponds to a node N' in the binary tree. or a Filial-Heir chain.left = DecodeSuccinct(structure.E.e. chained together with their right fields.n. To convert a general ordered tree to binary tree. each node is either a leaf or has at least 2 children). It can be converted into the binary tree on the right. The result of this representation will be automatically binary tree. or a doubly chained tree.D. A has the 6 children {B. which in particular is used by Lisp to represent general ordered trees as binary trees. through its left field.C. the next node in order among the children of the parent of N.that is. in the tree on the left.(i. if viewed from a different perspective. For example.

6. 1997.2 (pp. entry for data structure in Dictionary of Algorithms and Data Structures. 1990 ISBN 0-13-199746-7 ^ Paul E.).S. 318–348). Nguyen (2003).rice. Section 2. 2. 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: (((N O) I J) C D ((P) (Q)) F (M)) which would be implemented in memory as the binary tree on the right. Chapman and Hall. 4. Addison-Wesley.3.3. ^ Mehta. Fundamental Algorithms.gov/dads/HTML/completeBinaryTree. U. Prentice Hall.html. al Data Structures Using C. rice. without any letters on those nodes that have a left child. "Binary Tree Structure". Retrieved December 28.nist. [edit] References Donald Knuth. http://www. . Tenenbaum.3. [edit] See also 2-3 tree 2-3-4 tree AA tree B-tree Binary space partitioning Elastic binary tree Huffman tree Kraft's inequality Random binary tree Recursion (computer science) Red-black tree Rope (computer science) Self-balancing binary search tree Strahler number Threaded binary Tree of primitive Pythagorean triples Alternative methods of generating the tree # Unrooted binary tree [edit] Notes 1.nist. ISBN 1584884355.edu/comp212/03spring/lectures/22/. Sartaj Sahni (2004).The binary tree can be thought of as the original tree tilted sideways. Online version Accessed 2010-12-19. ^ "complete binary tree". http://www. http://www.edu. ^ Aaron M. ab ^ Dung X. Handbook of Data Structures and Applications.clear. NIST. ^ "perfect binary tree". 15 December 2004. ISBN 0-201-89683-4.gov/dads/HTML/perfectBinaryTree. Third Edition. 3. The art of computer programming vol 1.1–2. Black (ed. Dinesh. 2010. et. NIST. especially subsections 2. 5. National Institute of Standards and Technology.html.

Course Technology. or a balanced binary search tree on array [hide] v t e Trees in computer science Binary trees Binary search tree (BST) Cartesian tree Top tree T-tree AA tree AVL tree LLRB tree Red–black tree Scapegoat tree Splay tree Treap B+ tree B*-tree Bx-tree UB-tree 2-3 tree 2-3-4 tree (a. 2005. Jerome L Paul. 113–166). Sequential and Distributed.b)-tree Dancing tree Htree Suffix tree Radix tree Ternary search tree Self-balancing binary search trees B-trees Tries . (pp. ISBN 0-534-42057-5. Kenneth A Berman. [edit] External links flash actionscript 3 opensource implementation of binary tree — opensource library [2] — GameDev. Algorithms: Parallel.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. Chapter 4.

wikipedia.org/w/index. X-fast trie Y-fast trie Quadtree Octree k-d tree Implicit k-d tree vp-tree Exponential tree Fusion tree Interval tree PQ tree Range tree SPQR tree Van Emde Boas tree R-tree R+ tree R* tree X-tree M-tree Segment tree Hilbert R-tree Priority R-tree Heap Hash tree Finger tree Metric tree Cover tree BK-tree Doubly-chained tree iDistance Link-cut tree Fenwick tree Binary space partitioning (BSP) trees Non-binary trees Spatial data partitioning trees Other trees Retrieved from "http://en.php?title=Binary_tree&oldid=484739303" View page ratings Rate this page What's this? Trustworthy .

Objective Complete Well-written I am highly knowledgeable about this topic (optional) Submit ratings Saved successfully Your ratings have not been submitted yet Categories: Binary trees Data structures Personal tools Log in / create account Namespaces Article Talk Variants Views Actions Search Read Edit View history .

Search Special:Search Navigation Main page Contents Featured content Current events Random article Donate to Wikipedia Interaction Toolbox Help About Wikipedia Community portal Recent changes Contact Wikipedia What links here Related changes Upload file Special pages Permanent link Cite this page Rate this page Print/export Create a book Download as PDF Printable version Languages العربية Български Català Česky Deutsch Español Esperanto Français 한국어 हिन्दी .

Contact us Privacy policy About Wikipedia Disclaimers Mobile view Binary tree From Wikipedia. additional terms may apply. Wikipedia® is a registered trademark of the Wikimedia Foundation. the free encyclopedia Jump to: navigation.. . a non-profit organization. Text is available under the Creative Commons Attribution-ShareAlike License. Inc. Bahasa Indonesia Íslenska Italiano עברית 日本語 Polski Português Română Русский Slovenčina Slovenščina Српски / Srpski Suomi Svenska Українська 中文 This page was last modified on 30 March 2012 at 19:10. search Not to be confused with B-tree. See Terms of use for details.

Outside the tree. 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. and post-order traversal 4. a binary tree is a tree data structure in which each node has at most two child nodes. usually distinguished as "left" and "right". and child nodes may contain references to their parents. Nodes with children are parent nodes. In computer science.2 Node with two children o 4.2.3.A simple binary tree of size 9 and depth 3.2.1 Node with zero or one children 4. The above tree is unbalanced and not sorted.2 Internal nodes o 4. there is often a reference to the "root" node (the ancestor of all nodes).1 Insertion 4.2 Depth-first order 4.3.1. Contents [hide] 1 Definitions for rooted trees 2 Types of binary trees 3 Properties of binary trees 4 Common operations o 4. if it exists.3 Iteration 4.3. Binary trees are used to implement binary search trees and binary heaps.1.1 Pre-order. in-order.1 External nodes 4. with a root node whose value is 2.3 Breadth-first order 5 Type theory 6 Definition in graph theory 7 Combinatorics 8 Methods for storing binary trees .2 Deletion 4.

The root node is at depth zero. Out-degree of a node is the number of edges leaving that node. The set of all nodes is countably infinite.2 Arrays 9 Encodings o 9. 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. [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. 8. and all nodes are as far left as possible. In-degree of a node is the number of edges arriving at that node. but the set of all infinite paths from the root is uncountable: it has the cardinality of the continuum.[2] An infinite complete binary tree is a tree with a countably infinite number of levels. These paths corresponding by an order preserving bijection to the . The depth of a node n is the length of the path from the root to the node. All the leaf nodes have Out-degree = 0. The node q is then termed as a descendant of p. in which every node has two children. There is at most one root node in a rooted tree.[1] (This is ambiguously also called a complete binary tree. The size of a node is the number of descendants it has including itself. so that there are 2d nodes at level d. The root node of a tree is the node with no parents. Sometimes a full tree is ambiguously defined as a perfect tree.) A complete binary tree is a binary tree in which every level. The depth (or height) of a tree is the length of the path from the root to the deepest node in the tree. The set of all nodes at a given depth is sometimes called a level of the tree. A perfect binary tree is a full binary tree in which all leaves are at the same depth or same level. A (rooted) tree with only one node (the root) has a depth of zero. except possibly the last.2 Encoding general trees as binary trees 10 See also 11 Notes 12 References 13 External links o o [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). Siblings are nodes that share the same parent node. The root is the only node in the tree with In-degree = 0. is completely filled.1 Succinct encodings o 9. A leaf node has no children.1 Nodes and references 8. A node p is an ancestor of a node q if it exists on the path from the root to node q. and in which every parent has two children.

a single child. A degenerate tree is a tree where for each parent node. the tree will behave like a linked list data structure. (Different balancing schemes allow different definitions of "much farther"[4]). The number of nodes in a perfect binary tree can also be found using this formula: where is the number of leaf nodes in the tree. there is only one associated child node. Example 3: balanced tree with 5 nodes. Binary trees that are balanced according to this definition have a predictable depth (how many nodes are traversed from the root to a leaf. (depth=1).[3] although in general it is a binary tree where no leaf is much farther away from the root than any other leaf. This means that in a performance measurement.1 (since all nodes except the root node come from a single branch) . root counting as node 0 and subsequent as 1. especially with respect to the meaning "complete" and "full".. Example 1: (depth = 0). B = n . n0 = n2 + 1. The number of null links (absent children of nodes) in a complete binary tree of n nodes is (n+1). where is the number of nodes on the balanced tree. The number n–L of internal nodes (non-leaf nodes) in a Complete Binary Tree of n nodes is ⌊ ⌋. Example 2: balanced tree with 3 nodes.. This depth is equal to the integer part of balanced tree with 1 node. (depth of tree is 2 nodes). The number of nodes in a complete binary tree is at least where is the depth of the tree. [edit] Properties of binary trees The number of nodes in a perfect binary tree can be found using this formula: and at most where is the depth of the tree. depth). 2. For any non-empty binary tree with n0 leaf nodes and n2 nodes of degree 2.[5] Proof: Let n = the total number of nodes B = number of branches n0. A balanced binary tree is commonly defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Note that this terminology often varies in the literature. points of the Cantor set. n2 represent the number of nodes with no children.. and two children respectively. The number of leaf nodes in a perfect binary tree can be found using this formula: where is the depth of the tree. n1. or (through the example of the Stern–Brocot tree) to the set of positive irrational numbers. A rooted complete binary tree can be identified with a free magma. .

) A assigns its child to the new node and the new node assigns its parent to A.B = n1 + 2*n2 n = n1+ 2*n2 + 1 n = n0 + n1 + n2 n1+ 2*n2 + 1 = n0 + n1 + n2 ==> n0 = n2 + 1 [edit] Common operations There are a variety of different operations that can be performed on binary trees. To add a new node after node A. while others simply return useful information about the tree. Only certain nodes in a binary tree can be removed unambiguously. (If the insertion is to insert a right child. Then the new node assigns its child to B and B assigns its parent as the new node. A assigns the new node as one of its children and the new node assigns node A as its parent. [edit] Deletion Deletion is the process whereby a node is removed from the tree. [edit] Insertion Nodes can be inserted into binary trees in between two other nodes or added after an external node. [edit] External nodes Say that the external node being added on to is node A. a node that is inserted is specified as to which child it is. and similarly with a left child insertion.[6] . Say that the internal node is node A and that node B is the child of A. then B is the right child of A. Some are mutator operations. [edit] Internal nodes The process of inserting a node into a binary tree Insertion on internal nodes is slightly more complex than on external nodes. In binary trees.

but with the caveat that it must be a child of a node we have already visited. Left child. [edit] Iteration Often. we always attempt to visit the node farthest from the root that we can. and post-order traversal Main article: Tree traversal Pre-order. one wishes to visit each of the nodes in a tree and examine the value there. there is no need to remember all the nodes we have visited. If a node has no children (external node). [edit] Pre-order. set the parent of A's child to A's parent and set the child of A's parent to A's child. and post-order traversal visit each node in a tree by recursively visiting each node in the left and right subtrees of the root. including binary search trees. in certain binary trees these nodes can be deleted. deletion is accomplished by setting the child of A's parent to null. a process called iteration or enumeration. in-order. a node with two children cannot be deleted unambiguously. Right child Post-Order: Left Child. root In-Order: Left child. There are several common orders in which the nodes can be visited. right child. Right child. Pre-order is a special case of this. [edit] Depth-first order In depth-first order.[edit] Node with zero or one children The process of deleting an internal node in a binary tree Say that the node to delete is node A. and each has useful properties that are exploited in algorithms based on binary trees: Pre-Order: Root first. [edit] Node with two children In a binary tree.[6] However. Unlike a depth-first search on graphs. If it has one child. See depth-first search for more information. because a tree cannot contain cycles. . root. in-order.

If we drop the connectedness requirement. [edit] Combinatorics In combinatorics one considers the problem of counting the number of full binary trees of a given size. but there can be any number of nodes of degree two. This also does not establish the order of children. and up to two children. then interchanging them will produce a tree distinct from the original one). and adding an edge directed from the new vertex to the root of each binary tree. It can be shown that in any binary tree of two or more nodes. there are exactly two more nodes of degree one than there are of degree three. 1 + A × α × α. however. the other nodes are leaf nodes and there are n + 1 of them. 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. we call such a structure a forest. A graph formed by taking two binary trees. [edit] Definition in graph theory For each binary tree data structure. but does fix a specific root node. Also called a level-order traversal. The number of such binary trees of size n is equal to the number of ways of fully parenthesizing a string of n + 1 symbols (representing leaves) separated .[edit] Breadth-first order Contrasting with depth-first order is breadth-first order. With the root thus chosen. a binary tree with nodes of type A is defined inductively as TA = μα. See breadth-first search for more information. A binary tree is either: A single vertex. [edit] Type theory In type theory. adding a vertex. and trees are distinguished only by their structure. 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. The size of the tree is taken to be the number n of internal nodes (those with two children). there is equivalent rooted binary tree in graph theory. Another way of defining binary trees is a recursive definition on directed graphs. however the left and right child of any node are distinguished (if they are different trees. allowing multiple connected components in the graph. Here the trees have no values attached to their nodes (this would just multiply the number of possible trees by an easily determined factor). so far there is insufficient information to distinguish a left or right child. each vertex will have a uniquely defined parent. which always attempts to visit the node closest to the root that it has not already visited.

then the dotted-pair expression for that proper list is a fully parenthesized expression (with NIL as symbol and '. It The above parenthesized strings should not be confused with the set of words of length 2n in the Dyck language. and any other binary tree is characterized by the pair of its left and right children. the full tree has size i + j + 1.by n binary operators (representing internal nodes). So there are also five Dyck words of length 10: . A bijective correspondence can nevertheless be defined as follows: enclose the Dyck word in a extra pair of parentheses. 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. Therefore the number of binary trees of size n has the following recursive description . The ability to represent binary trees as strings of symbols and parentheses implies that binary trees can represent the elements of a free magma on a singleton set. so as to determine the argument subexpressions of each operator. this number is therefore also the Catalan number . whose lengths 2i and 2j satisfy i + j + 1 = n). if these have sizes i and j respectively. and the addition of redundant parentheses (around an already parenthesized expression or around the full expression) is disallowed (or at least not counted as producing a new possibility). [edit] Methods for storing binary trees Binary trees can be constructed from programming language primitives in several ways. The number of such strings satisfies the same recursive description (each Dyck word of length 2n is determined by the Dyck subword enclosed by the initial '(' and its matching ')' together with the Dyck subword remaining after that closing parenthesis. so that the result can be interpreted as a Lisp list expression (with the empty list () as only occurring atom). [edit] Nodes and references In a language with records and references. which is possible in five ways: The correspondence to binary trees should be obvious. . For instance for n = 3 one has to parenthesize a string like . These Dyck words do not correspond in an obvious way to binary trees.' as operator) describing the corresponding binary tree (which is in fact the internal representation of the proper list). There is a unique binary tree of size 0 (consisting of a single leaf). which consist only of parentheses in such a way that they are properly balanced. and follows that is the Catalan number of index n. for any positive integer n.

we can simply simultaneously store it in a consecutive array in preorder. and the other of which is a "leaf" node. thus we need at least about bits to encode it. the th Catalan number (assuming we view trees with identical structure as identical). which contains no data and functions much like the null value in a language with pointers. it is expensive to grow and wastes space proportional to 2h . For large . In languages with tagged unions such as ML. One simple representation which meets this bound is to visit the nodes of the tree in preorder. as established by information theoretical lower bounds. This function accomplishes this: function EncodeSuccinct(node n. [edit] Encodings [edit] Succinct encodings A succinct data structure is one which takes the absolute minimum possible space. and right child.Sometimes it also contains a reference to its unique parent. This method of storage is often used for binary heaps. some of the child pointers may be set to a special null value. [1] If the tree contains data. this method wastes no space. its children are found at indices (for the left child) and (for the right). or to a special sentinel node. left child. one of which is a 3-tuple of data.n for a tree of depth h with n nodes. The number of different binary trees on nodes is . A succinct binary tree therefore would occupy only 2 bits per node. [edit] Arrays Binary trees can also be stored in breadth-first order as an implicit data structure in arrays. particularly during a preorder traversal. and if the tree is a complete binary tree. This method benefits from more compact storage and better locality of reference. while its parent (if any) is found at index (assuming the root has index zero). if a node has an index i. However. a tree node is often a tagged union of two types of nodes. If a node has fewer than two children. this is about . In this compact arrangement. array data) { if n = nil then append 0 to structure. else . No space is wasted because nodes are added in breadth-first order. outputting "1" for an internal node and "0" for a leaf. bitstring structure.

data) n. each node is either a leaf or has at least 2 children). Each node N in the ordered tree corresponds to a node N' in the binary tree. in the tree on the left. . The above program only works for a full binary tree. This binary tree representation of a general order tree is sometimes also referred to as a left child-right sibling binary tree (LCRS tree). A has the 6 children {B. data).data to data. data). [edit] Encoding general trees as binary trees There is a one-to-one mapping between general ordered trees and binary trees. 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. EncodeSuccinct(n.C.left.(i. chained together with their right fields. One way of thinking about this is that each node's children are in a linked list. structure. if viewed from a different perspective.e. which in particular is used by Lisp to represent general ordered trees as binary trees. For example.append 1 to structure.right. we only need to represent the general tree in left child-sibling way. the next node in order among the children of the parent of N. we don't even have to store its length. or a Filial-Heir chain. through its left field. and the right child of N' is the node corresponding to N 's next sibling --.right = DecodeSuccinct(structure. the left child of N' is the node corresponding to the first child of N.data n. } The string structure has only bits in the end. and the node only has a pointer to the beginning or head of this list. structure.that is. EncodeSuccinct(n. It can be converted into the binary tree on the right. we can convert the output back to the original tree like this: function DecodeSuccinct(bitstring structure. 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. or a doubly chained tree. append n.left = DecodeSuccinct(structure.E.G}. where is the number of (internal) nodes.D. To show that no information is lost. To convert a general ordered tree to binary tree.F. The result of this representation will be automatically binary tree.

NIST. http://www. with the black left edges representing first child and the blue right edges representing next sibling.html.nist. ^ "perfect binary tree". without any letters on those nodes that have a left child.gov/dads/HTML/perfectBinaryTree. [edit] See also 2-3 tree 2-3-4 tree AA tree B-tree Binary space partitioning Elastic binary tree Huffman tree Kraft's inequality Random binary tree Recursion (computer science) Red-black tree Rope (computer science) Self-balancing binary search tree Strahler number Threaded binary Tree of primitive Pythagorean triples Alternative methods of generating the tree # Unrooted binary tree [edit] Notes 1.The binary tree can be thought of as the original tree tilted sideways. . 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 right.

ISBN 0-534-42057-5.3. Chapter 4. Kenneth A Berman. al Data Structures Using C. et. Sequential and Distributed.gov/dads/HTML/completeBinaryTree.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.). Nguyen (2003). http://www. [edit] References Donald Knuth. Algorithms: Parallel. Chapman and Hall. 5.edu/comp212/03-spring/lectures/22/. 2010.edu. "Binary Tree Structure". ISBN 0-201-89683-4. ^ Aaron M. Tenenbaum. The art of computer programming vol 1. 2005. 318–348). 1997. Course Technology. or a balanced binary search tree on array [hide] v t e Trees in computer science Binary trees Binary search tree (BST) Cartesian tree Top tree T-tree AA tree AVL tree LLRB tree Red–black tree Scapegoat tree Splay tree Selfbalancing binary search trees . Sartaj Sahni (2004). Dinesh. U.html. Jerome L Paul. Section 2.clear. Black (ed. Handbook of Data Structures and Applications. 6.1–2. Online version Accessed 2010-12-19. Retrieved December 28.rice.2 (pp. ^ Paul E. Prentice Hall.S. [edit] External links flash actionscript 3 opensource implementation of binary tree — opensource library [2] — GameDev. (pp. 113–166). NIST. ISBN 1584884355. ^ "complete binary tree". Third Edition. 15 December 2004. 3. especially subsections 2. ^ a b Dung X. Fundamental Algorithms. National Institute of Standards and Technology. entry for data structure in Dictionary of Algorithms and Data Structures. ^ Mehta.3. http://www.2. Addison-Wesley. 1990 ISBN 0-13-199746-7 4.nist.3. rice.

Treap B+ tree B*-tree Bx-tree UB-tree 2-3 tree 2-3-4 tree (a.b)-tree Dancing tree Htree Suffix tree Radix tree Ternary search tree X-fast trie Y-fast trie Quadtree Octree k-d tree Implicit k-d tree vp-tree Exponential tree Fusion tree Interval tree PQ tree Range tree SPQR tree Van Emde Boas tree R-tree R+ tree R* tree X-tree M-tree Segment tree Hilbert R-tree Priority R-tree Heap Hash tree Finger tree Metric tree B-trees Tries Binary space partitioning (BSP) trees Non-binary trees Spatial data partitioning trees Other trees .

Cover tree BK-tree Doubly-chained tree iDistance Link-cut tree Fenwick tree Retrieved from "http://en.wikipedia.php?title=Binary_tree&oldid=484739303" View page ratings Rate this page What's this? Trustworthy Objective Complete Well-written I am highly knowledgeable about this topic (optional) Submit ratings Saved successfully Your ratings have not been submitted yet Categories: Binary trees Data structures Personal tools Log in / create account .org/w/index.

Namespaces Article Talk Variants Views Actions Search Search Special:Search Read Edit View history Navigation Main page Contents Featured content Current events Random article Donate to Wikipedia Interaction Toolbox Help About Wikipedia Community portal Recent changes Contact Wikipedia What links here Related changes Upload file Special pages Permanent link Cite this page Rate this page Print/export .

Create a book Download as PDF Printable version 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 30 March 2012 at 19:10. additional terms may apply. a non-profit organization. Wikipedia® is a registered trademark of the Wikimedia Foundation. Text is available under the Creative Commons Attribution-ShareAlike License. Inc. See Terms of use for details. Contact us Privacy policy About Wikipedia Disclaimers Mobile view ..

.

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

We've moved you to where you read on your other device.

Get the full title to continue

Get the full title to continue listening from where you left off, or restart the preview.

scribd