The basic concept of tree-type structure
For a large number of input data, the linear access time of the list is too slow, not suitable for use. This article discusses another important data structure----tree, most of the time can guarantee the operation of the average time to run the complexity of O (Logn), the first part of the tree to look at some preliminary knowledge.
First look at the shape of the tree structure, representing the general shape of the tree structure:
The tree is a collection of nodes that summarizes some of the basic concepts of the tree:
1. Nodes: The data elements in the tree are called nodes.
2, Root: The topmost node is called the root, a tree has only one root and developed from the root, from another point of view, each node can be considered the root of its sub-tree
3, Father: node of the upper node, in the node K's father is e, node L's father is G
4, brothers: With the same father's knot is called the brothers, figure F, G, H Mutual Brothers
5. Node Degree: The number of subtree owned by the node is called the degree of node, such as the degree of Node B is 3
6, leaves: The degree of 0 of the node, also known as the Terminal node, the figure D, K, F, L, H, I, J are all leaves.
7, Branch node: The degree is not 0 of the node, also known as non-terminal nodes or internal nodes, the figure of Root, A, B, C, E, g are branch nodes
8, the level of the node: from the root node to a tree nodes on the path of the branch tree is called the level of the node, the root of the hierarchy is 1, the remaining nodes are the level of their father node +1
9, the depth of the tree: the maximum number of nodes in the tree, the depth of the tree in the graph is 4
Two-fork Tree
Above is the general form of the tree, look at the binary tree below. Two A fork tree is a tree in which each node cannot have more than two subtrees .
Shows a binary tree:
One of the properties of a binary tree is an average of two fork tree depth and the number of nodes n is much smaller , this property is very important, especially for the special type of two-fork tree is the binary search tree, the depth of the average is O (Logn), which will greatly reduce the time complexity of the search.
Of course, the binary tree in the case of poor use of the situation is a serious problem, namely:
The depth of the tree is N-1, which is absolutely not allowed, and this tree is also known as an unbalanced tree . In the next section of the two-fork look-up tree description, you will tell the two-tree with self-balancing conditions to become a balance tree.
Binary search Tree
One important application of binary trees is the use of them in their lookups. Assuming that each node in the tree stores one item of data, the binary tree becomes a binary lookup tree in that the property of all the items in its left subtree is less than x for each node x in the tree, and that the value of all items in its right subtree is greater than X, which means that all elements of the tree can be sorted in a consistent manner .
Such as:
This is a binary lookup tree. But if I change this:
This is not a binary lookup tree, because one of the nodes in the left subtree of the root node is 11.
AVL Tree
As already mentioned before, in the generation of two fork tree/two fork search tree is very easy to unbalance, the worst case is one-sided (only Zuozi/right subtree), which will lead to the tree retrieval efficiency greatly reduced, so in order to maintain the balance of the binary tree, Daniel proposed a variety of implementation algorithms, such as: AVL tree, Node size balance tree (SBT), stretch tree, tree heap (treap), red-black tree, and so on.
Here the AVL tree---- A two-fork search tree with equilibrium conditions that must be kept easy, and it ensures that the depth of the tree must be O (Logn).
For an AVL tree, its balance must meet one of the following characteristics:
1. Empty tree
2, the Saozi right subtree depth of each node is the most difference 1
Can look at:
In the picture, the tree on the left is the AVL tree, and the tree on the right is not. Because obviously, from the root node 7, the right subtree depth is 1, and the left subtree 7-->2-->4-->5 (3) This path depth is 3, does not satisfy each node's Saozi right subtree depth of the most difference 1 conditions.
It can be proved that the height of an AVL tree is 1.44 * log (N + 2)-1.328, but the actual height is only slightly greater than Logn.
Red and black Trees
Red and black trees are another variant of the AVL tree. The red and black tree, as its name implies, is a balanced binary tree of red or black, which maintains the balance of the two-fork tree by its color constraints. For a typical two-fork tree:
For a valid red-black tree, we must add the following rules:
1. Each node can only be red or black
2, the root node is black
3, each leaf is black
4, if a node is red, then its two sub-nodes are black, that is, a path can not appear adjacent to the two red nodes
5. All paths from any node to each of its leaves contain the same number of black nodes
These constraints force the key properties of the red-black tree: The longest possible path from the root to the leaf is no more than twice times longer than the shortest possible path . The result is that the tree is generally balanced, because inserting, deleting, and finding a worthy worst-case time is required to be proportional to the height of the tree, which allows the red and black trees to be efficient only in the worst case scenario.
No more specific will not say, you can see http://www.cnblogs.com/yangecnu/p/Introduce-Red-Black-Tree.html, the Red and black tree explanation is very well written.
By the way, TreeMap and TreeSet are typical implementations of red and black trees.
Tree-type structure