digitalmars.D.learn - rbtree to array?
- Pete Padil (3/3) May 05 Was wondering if there is a way to get the data inside an rbtree
- Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= (12/15) May 05 Rbtree has a range interface of d. Its memory layout is similar
- Pete Padil (2/13) May 06 Excellent - thank you!
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.
May 05
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. ```d import std.container.rbtree; import std.array; auto rbt = redBlackTree(3, 1, 4, 2, 5); auto arr = rbt[].array; writeln(arr); ```
May 05
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. ```d import std.container.rbtree; import std.array; auto rbt = redBlackTree(3, 1, 4, 2, 5); auto arr = rbt[].array; writeln(arr); ```Excellent - thank you!
May 06