Skip to main content
Code Review

Timeline for Binary tree deepcopy using the Morris traversal in C

Current License: CC BY-SA 4.0

3 events
when toggle format what by license comment
Mar 26, 2024 at 22:39 comment added J_H You’re saying that doing Two Things is "hard". I completely agree. Better to do a Single Thing well. It’s easier to design, easier to test. Recommend you get around to writing a simple deepcopy() for binary tree, and then the whole business of "yikes, I trashed caller’s only copy of tree!" magically disappears. // As far as malloc fail goes, just be honest with the caller. Immediately return NULL and confess that the traversal tree is toast. Alternatively, do all allocations during first traversal, then on second traversal make mutations.
Mar 26, 2024 at 21:42 comment added Slav As I said in the OP - this is an example code I hacked together to make this post. I only care about the Morris traversal function. Everything else you see will not be present in the final implementation. Regarding malloc - in the real implementation this will be replaced by a call to a function for the specific port, which must somehow allocate memory to me, so I have no control over it. But good point about the malloc failure - recorevering from it will b e tricky though since the Morris traversal modifies the leaf trees, creating temporary loops. How I overcome this and clean up?
Mar 26, 2024 at 21:15 history answered Madagascar CC BY-SA 4.0

AltStyle によって変換されたページ (->オリジナル) /