3 ms·
No, I probably would use the BTreeMap in std instead, assuming I want a search tree. Which is even better, because the primary reason you use binary trees in C
by ATsch 5y ago
No, I probably would use the BTreeMap in std instead, assuming I want a search tree. Which is even better, because the primary reason you use binary trees in C is that they are easy to make intrusive. But if I want a priority queue instead there's also BinaryHeap. If I wanted to store data on the branch nodes I might use intrusive_collections. Or maybe if I was dealing with a more complex graph I'd pull in a library like petgraph. If all that fails I could still arena-allocate the nodes to get around the lifetime issues. If I really absolutely wanted to, which I don't see happening any time soon, personally. I'd need a more concrete example there.
- Jensson 5y agoI said binary tree, not "Map interface implemented using an ordered binary tree". They are not the same thing.
- ATsch 5y agoI am aware, as you might be able to gather by reading past the first sentence of my comment. Of course if you'd just said what you needed the binary tree for, I could have given you a better answer. If you don't have a use case in mind, I'd suggest just trying to actually use Rust for something instead of spending time trying to construct problems where Rust would be bad at the solution.
- Jensson 5y agoYou can't even explain how doing something as simple as a binary tree with parent pointer is possible in rust, so my assumption is that it isn't possible and I'd have to make work arounds. I can make work arounds just fine in C++, you don't have to teach me those, but I don't see why I'd learn a language where I'd be forced to make those work arounds rather than use the version that fits my problem best. It might be possible that you can write nice versions etc, if so I'd like to see them, but it seems everyone is hell bent on just saying "You shouldn't do it!".
- volta83 5y ago> You can't even explain how doing something as simple as a binary tree with parent pointer is possible in rust, so my assumption is that it isn't possible A beginner learns this in the first week: struct Node<'a, 'b> { left: Option<&'a Node>, right: Option<&'b Node> } Many people don't know how to read or write, but you don't hear them every day claiming that "therefore it must be impossible", yet here we are. > but I don't see why I'd learn a language where I'd be forced to make those work arounds rather than use the version that fits my problem best. > but it seems everyone is hell bent on just saying "You shouldn't do it!". The claim was and still is "there are infinitely more effective - as in faster - ways to learn Rust than by starting with how to write doubly-linked lists". This whole thread is you failing at 3rd grader logic hard and concluding: - therefore it is impossible to write doubly-linked lists in Rust - therefore doubly-linked list in Rust are efficient - therefore it is impossible to learn Rust - ...and many other things... I've taught Rust to a 9th year old, but I don't think I could have taught Rust to a 2 year old, so arguably, I think you are right, in that probably _FOR YOU_ it is impossible to learn Rust, you wouldn't be able to grok how to write doubly-linked lists in Rust properly, you wouldn't be able to master Rust to write efficient code in it, etc. That's ok, don't be frustrated by it, everybody is different. Just try to stop projecting your limitations to other people. Particularly in this forum, or when talking with Linux kernel developers, were most people are smart experienced programmers that would learn Rust well in a day and master it in a week.