Next, we will talk about the trees in the data structure. The most important thing to understand is the binary tree, the balanced binary tree, and the red-black tree. We will discuss these trees today.
Binary sorting tree
Concept: the binary sorting tree is also called the Binary Search Tree, the binary search tree, And the binary search tree. A binary tree is a special binary tree. The binary tree specifies that the keywords of each node are greater than those of all nodes on the left Tree, at the same time, it is less than or equal to the keywords of all nodes on the right subtree. Therefore, from the root node of the binary sorting tree to the left until the bottom, the minimum value can be obtained; from the root node to the right, until the bottom, the maximum value can be obtained. The special properties of the binary sorting tree make it very convenient to search for elements. Each query can remove half of the elements, so the search time is O (logn ).
Binary sorting treeAlgorithm:
(1) binary sorting tree search algorithm
Typedef Struct Node
{
Int Data;// Node Value
Struct Node * parent; // Parent node
Struct Node * lchild; // Left Node
Struct Node * rchild; // Right Node
} Treenode;
Treenode * search (treenode * root, Int Value)
{
If (Root! = NULL)
{
If (Root-> DATA = value)
{
Return Root;
}
Else
{
If (Value <root-> data)
{
Search (root-> lchild, value ); // If the search value is smaller than the node value, recursive left subtree
}
Else
{
Search (root-> rchild, value ); // If the search value is greater than the node value, recursive right subtree
}
}
}
Return Root;
}
(2) binary sorting Tree Insertion Algorithm
The Insertion Algorithm of the binary sorting tree is very simple: Starting from the root node, when the key value is large, it goes to the left, and when the key value is small, it goes to the right. It is the insertion point.
(3) algorithm for deleting the binary sorting tree
The deletion algorithm of the binary sorting tree is a little complicated: There are two subnodes to be deleted, or there are only one or no subnodes, which will be processed differently.
<1> there are two subnodes: Find the smallest node in the right subtree, replace the current node value with its value, and then delete the node with the smallest value, that is to say, if there are two subnodes, the minimum one is used to replace the current node with the keyword greater than the current node.
<2> it is much simpler to have only one node or no sub-node. If there is a sub-node, move the sub-node up and delete the current node.
Balanced Binary Tree
Concept: a balanced binary tree is strictly defined as a balanced binary tree. If T is a non-empty binary tree, its left and right subtree are TL and TR, so that HL and HR are respectively the depth of left and right subtree. if and only when :( 1) TL and TR are balanced binary trees; (2) | HL-HR | when ≤ 1, t is a balanced binary tree.
Balancing the rotation of a binary tree: After deleting some nodes, the balance of the tree is damaged. At this time, we need to adjust the tree to maintain the balance of the tree. At this point, the method used to adjust the balance of the tree is the rotation of the tree.
AVL Tree: blog GardenArticleSpeaking of: http://blog.csdn.net/do2jiang/article/details/5529770
Red/black tree
Concept: red-black tree is a self-balancing binary tree. However, it should be noted that the red-black tree is not a balanced binary tree. On the contrary, the red-black tree relaxed some requirements of the balanced binary tree due to a certain degree of "imbalance ", the performance of the red/black tree is improved. It is widely used. For example, the memory storage structure of STL (Standard Template Library) map and set container is the red/black tree.
If a node has no child node, it is called a leaf node because it is on the edge of the tree. In the red/black tree, the leaves are assumed null or empty. Because the red/black tree is also a binary sorting tree, the value of each node must be greater than or equal to all nodes in its left subtree, and less than or equal to all nodes in its right subtree. This ensures that the red/black tree can quickly find the specified value in the tree.
The binary sorting tree meets the following conditions:
(1) Each node is either "red" or "Black.
(2) All leaf nodes are empty and "Black.
(3) If a node is red, its two subnodes are black.
(4) Each path from any node to its leaf node contains the same number of "Black" nodes.
(5) The root node is always black.
With the above rules, we can ensure the balance of the entire tree, that is, to ensure that the search time is O (logn ).
The following is a red-black tree:
Related Articles:
• C/C ++ notes (1)
• C/C ++ notes (2)