"STL" Associative container _ two fork Tree

Source: Internet
Author: User
Tags data structures diff

"STL" commonly used container summary, with review nature of learning more efficient;

Review of "STL" associative container

Before reviewing the relevant containers, first recall the underlying basic data structures;

"Tree": the common tree has two fork tree, search binary tree, Rb_tree, AVL _tree, Havermann tree;

: The following only to do features of the review, code and specific details have the front of a dedicated blog, followed by a link;

The ordinary two-fork tree is a structure of the left and right subtree, there is no special place, only the time complexity of the search is Logn;

Search the characteristics of the binary tree is Zuozi node than the value of the middle node, the node of the right subtree is larger than the middle node, about the search for two-fork Tree interview questions have a lot of, and finally listed some problems as a consolidation;

Avl_tree: Highly balanced two-pronged tree, the absolute value of the height difference of the left and right subtree is not more than 1; by rotation to achieve the goal of balance, which is divided into single rotating double rotation, which is the key to balance the binary tree, the interview will not let you write this code, but the process of rotation must understand, Once face to which net when let me hand to draw AVL tree rotation, tu, some Meng force.

On the face of the ALV tree, there is a classic question: to determine whether an AVL tree is balanced;

Rb_tree: Red-black tree is a binary search tree node in the structure of the addition of color attributes, through the restriction of color to balance the binary tree, to minimize the two-fork tree height;

Some features of the red-black tree:
1. The root node is a black node
2. There are no contiguous two red nodes
3. The longest path is twice times 4 of the shortest path.
All the child nodes of the leaf node are black nodes
5. For each node, The same number of black nodes are included on the simple path from this node to all of its descendant leaf nodes

Here is a more detailed article about the red-black tree that I wrote before:
Red and black Trees

In STL, the red-black tree as the bottom container has map, set, Multiset, Multimap; But in fact, in the C + + standard library, that is, C++11 joined the unordered_map, and unordered _set,set_symmetric_difference;

"Set": all elements will be automatically sorted by the pattern element key value, set only key value, in fact, because the bottom is red-black trees, the bottom is the value and key into the same, while set does not allow duplicate key, set does not allow the iterator to change the key value, which will destroy the structure of the tree;

STL set provides the relevant algorithm, incidentally, face cvte, there is a programming problem: to find 2 strings of the difference set;

The first time I thought of set, stupid is not familiar with the relevant algorithm of the set interface, resulting in the pseudocode written very frustrated; The following is the title of the example review set;

Set provides the correlation algorithm: Set_union (and set), Set_difference (difference set), Set_intersection (intersection) and so on;
#include <iostream>
#include <set>
#include <vector>
#include <algorithm>

using namespace std;

int main ()
{
    char arr1[] = "DASDFASF2";
    Char arr2[] = "DSWDADSS3";
    set<char> S1 (arr1, arr1+9);
    Set<char> S2 (arr2, arr2+9);

    Vector<char> diff (Ten);
    Set_symmetric_difference (S1.begin (), S1.end (), S2.begin (), S2.end (), Diff.begin ());
    for (int i = 0; i < diff.size (); ++i)

    {
        cout<<diff[i]<< "";
    }
    cout<<endl;

    System ("pause");
    return 0;
}

Set_symmetric_difference: symmetric difference set, i.e. (S1-S2) U (S2-S1); This is the complete two-string difference set;

Of course, I did not use this algorithm, because at that time did not know the algorithm, so I can only build a wheel;

 #include <iostream> #include <set> #include <vector> using namespace std;
    int main () {char* arr1 = "DASDFASF2";
    char* arr2 = "DSWDADSS3";
    set<char> S1 (arr1, arr1+9);

    Set<char> S2 (arr2, arr2+9);

    Vector<char> diff; while ('!= ' *arr1 | | '!= ' *arr2 {while ('!= ' *arr1) {pair<set<char>::iterator, bool> ret =
            S2.insert (*ARR1);
            if (Ret.second = = True) {Diff.push_back (*ARR1);
        } ++arr1;
            while ('!= ' *arr2) {pair<set<char>::iterator, bool> ret = S1.insert (*ARR2);
            if (Ret.second = = True) {Diff.push_back (*ARR2);
        } ++ARR2;
    for (int i = 0; i < diff.size (); ++i) {cout<<diff[i]<< "";

    } cout<<endl;
    System ("pause");
return 0; }

It is worth mentioning that the set insert returns a pair-type structure with two members first and second;

"Map": Map and set the bottom of the data structure are red and black trees, the biggest difference is that the map of the complete use of the KEY_VALUE structure, the map can be modified by the iterator to modify the value of the map but can not modify the key value, the same reason as set, will destroy the structure of the tree;

A new feature of map compared to set is that it overloads the ' [] '; You can find or insert a new element directly by [];

For example:

#include <iostream>
#include <map>

using namespace std;

int main ()
{
    Map<char, int> m;
    M[' a '] = 1;
    M[' B '] = 2;
    M[' C '] = 3;

    Map<char, Int>::iterator it = M.begin ();
    while (it!= m.end ())
    {
        cout<<it->first<< ":" <<it->second<<endl;
        it++;
    }
    return 0;
}

"Multiset and Multimap": Set and map with repeatable key values, also for reuse, using the same red-black tree structure, and the set and map invoke the insert_unique when inserting elements, and multiset and Multim The AP calls the Insert_equal;

Two fork trees often meet questions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.