Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about merge. #9

Open
rdebroiz opened this issue Feb 27, 2021 · 1 comment
Open

Question about merge. #9

rdebroiz opened this issue Feb 27, 2021 · 1 comment
Labels
bug Something isn't working

Comments

@rdebroiz
Copy link

I try to merge two tree together but I'm not sure i properly understood what it says in the doc.

for instance with the siblings of the first level of two trees used as ranges for the merge function,
i expect something like:

a               a               a
└── a      +    └── b       =   ├── a
    └── a           └── b       │   └── a
                                └── b
                                    └── b

but the result is

┌── a
│   └── a
│       └── a
└── b
    └── b

code to demonstrate:

#include <trie.h>

using namespace std;

void print_branches(const tree<char>& tree) {
    tree<char>::leaf_iterator it = tree.begin_leaf();
    while(tree.is_valid(it)) {
        string branch;
        tree_node_<char>* node = it.node;
        while(node != tree.head && node != nullptr) {
            branch += ">-";
            branch.push_back(node->data);
            node = node->parent;
        }
        reverse(branch.begin(), branch.end());
        ++it;
        cout << branch << endl;
    }
}

int main(int argc, const char** argv) {
    tree<char> tree1, tree2;
    tree<char>::sibling_iterator it1, it2;
    it1 = tree1.insert(tree1.begin(), 'a');
    it1 = tree1.append_child(it1, 'a');
    it1 = tree1.append_child(it1, 'a');
    it2 = tree2.insert(tree2.begin(), 'a');
    it2 = tree2.append_child(it2, 'b');
    it2 = tree2.append_child(it2, 'b');

    it1 = tree1.begin();
    it2 = tree2.begin();
    tree1.merge(it1, it1.end(), it2, it2.end());
    print_branches(tree1);
}
    

output:

a->a->a->
b->b->

insead of

a->a->a->
a->b->b->

Am i correctly using the merge function in conjunction with the sibling_iterator?

@xuerenjie124
Copy link

Hi, I also get that result,do you find the correct way to use that function?

@kpeeters kpeeters added the bug Something isn't working label Feb 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants