Thread overview
rbtree to array?
3 days ago
Pete Padil
3 days ago
Pete Padil
3 days ago

Was wondering if there is a way to get the data inside an rbtree as a simple array or a std.array, besides manually copying? Apologies if the answer is obvious and I missed it.

3 days ago

On Tuesday, 6 May 2025 at 04:57:46 UTC, Pete Padil wrote:

>

Was wondering if there is a way to get the data inside an rbtree as a simple array or a std.array, besides manually copying? Apologies if the answer is obvious and I missed it.

Rbtree has a range interface of d. Its memory layout is similar to a linked list, where the data is not contiguous. So, you have to copy the elements manually. But it is always a nice practice to use library functions, doing it for you.

    import std.container.rbtree;
    import std.array;

    auto rbt = redBlackTree(3, 1, 4, 2, 5);
    auto arr = rbt[].array;

    writeln(arr);
3 days ago

On Tuesday, 6 May 2025 at 06:50:36 UTC, Ferhat Kurtulmuş wrote:

>

Rbtree has a range interface of d. Its memory layout is similar to a linked list, where the data is not contiguous. So, you have to copy the elements manually. But it is always a nice practice to use library functions, doing it for you.

    import std.container.rbtree;
    import std.array;

    auto rbt = redBlackTree(3, 1, 4, 2, 5);
    auto arr = rbt[].array;

    writeln(arr);

Excellent - thank you!