Binary Tree
Binary Tree
SUBMISSION DATE
23- -02-
-2012
SUBMITTED TO :
SUBMITTED BY :
PAWAN VASKAR SECTION K3R01 ROLL NO- RK3R01 B-30
MAM PARUL
PRAKRAM SHARMA
CONTENT
BINARY TREE DEFINATION OF ROOTED BINARY TREE
BINARY TREE
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
simple binary tree of size 9 and depth 3, with a root node whose value is 2. The above
Directed edge refers to the link from the parent to the child (the arrows in the picture of the tree). The root node of a tree is the node with no parents. There is at most one root node in a rooted tree. A leaf node has no children. The depth of a node n is the length of the path from the root to the node. The set of all nodes at a given depth is sometimes called a level of the tree. The root node is at depth zero. The depth of a tree is the length of the path from the root to the deepest node in the tree. A (rooted) tree with only one node (the root) has a depth of zero. Siblings are nodes that share the same parent node. A node p is an ancestor of a node q if it exists on the path from the root to node q. The node q is then termed as a descendant of p. The size of a node is the number of descendants it has including itself. In-degree of a node is the number of edges arriving at that node. Out-degree of a node is the number of edges leaving that node. The root is the only node in the tree with In-degree = 0. All the leaf nodes have Out-degree = 0.
A rooted binary tree is a tree with a root node in which every node has at most two children. A full binary tree (sometimes proper binary tree or 2tree or strictly binary tree) is a tree in which every node other than the leaves has two children. Sometimes a full tree is ambiguously defined as a perfect tree. A perfect binary tree is a full binary tree in which all leaves are at the same depth or same level, and in which every parent has two children. (This is ambiguously also called a complete binary tree.) A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. An infinite complete binary tree is a tree with a countably infinite number of levels, in which every node has two children, so that there are 2d nodes at level d. The set of all nodes is countably infinite, but the set of all infinite paths from the root is uncountable: it has the cardinality of the continuum. These paths corresponding by an order preserving bijection to the points of the Cantor set, or (through the example of the SternBrocot tree) to the set of positive irrational numbers. 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, although in general it is a binary tree where no leaf is much farther away from the root than any other leaf. (Different balancing schemes allow different
definitions of "much farther". Binary trees that are balanced according to this definition have a predictable depth (how many nodes are traversed from the root to a leaf, root counting as node 0 and subsequent as 1, 2, ..., depth). This depth is equal to the integer part of log2(n) where n is the number of nodes on the balanced tree. Example 1: balanced tree with 1 node, log2(1) = 0 (depth = 0). Example 2: balanced tree with 3 nodes, log2(3) = 1.59 (depth=1). Example 3: balanced tree with 5 nodes, log2(5) = 2.32 (depth of tree is 2 nodes). 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. This means that in a performance measurement, the tree will behave like a linked list data structure.
Note that this terminology often varies in the literature, especially with respect to the meaning "complete" and "full".
RED-BLACK TREE
Red-black trees are a fairly simple and very efficient data structure for maintaining a balanced binary tree. The idea is to strengthen the representation invariant so a tree has height logarithmic in n. To help enforce the invariant, we color each node of the tree either red orblack. Where it matters, we consider the color of an empty tree to be black. type color = Red | Black type 'a rbtree = Node of color * 'a * 'a rbtree * 'a rbtree | Leaf Here are the new conditions we add to the binary search tree representation invariant: 1. There are no two adjacent red nodes along any path. 2. Every path from the root to a leaf has the same number of black nodes. This number is called the black height (BH) of the tree. If a tree satisfies these two conditions, it must also be the case that every subtree of the tree also satisfies the conditions. If a subtree violated either of the conditions, the whole tree would also. Additionally, we require that the root of the tree be colored black. This can always be enforced by simply setting its color to black; doing this does not cause any other invariants to be violated.With these
invariants, the longest possible path from the root to an empty node would alternately contain red and black nodes; therefore it is at most twice as long as the shortest possible path, which only contains black nodes. If n is the number of nodes in the tree, the longest path cannot have a length greater than twice the length of the paths in a perfect binary tree: 2 log n, which is O(log n). Therefore, the tree has height O(log n) and the operations are all asymptotically logarithmic in the number of nodes. Another way to see this is to think about just the black nodes in the tree. Suppose we snip all the red nodes out of the trees and reconnecting each black node to its closest black ancestor. Then we have a tree whose leaves are all at depth BH, and whose branching factor ranges between 2 and 4. Such a tree must contain at least (2BH) nodes, and so must the whole tree when we add the red nodes back in. If n is (2BH), then black height BH is O(log n). But invariant 1 says that the longest path is at most h = 2BH. So h is O(log n) too.