#CPP binary search trees.
42 messages Β· Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity
Well I guess you will have something like this ```cpp
#include <memory>
template<class T>
struct Node
{
T Data;
std::unique_ptr<Node> left = nullptr;
std::unique_ptr<Node> right = nullptr;
};
In which case there is no need to use any recursive destructors because unique_ptr will clean up everything automatically for you
So when a parent node goes out of scope it will clean up it's left and right nodes as well
test this with 100k tree nodes? You might see some problems π
What?
std::unique_ptr has memory overhead only if you provide it with some non-trivial deleter. And has time overhead only during constructor (if it has to copy the provided deleter and/or null-initialize the pointer) and during destructor (to destroy the owned object).
conclusion: 100k nodes w/ smart pointers would be just as fine as 100k nodes with raw pointers, unless you continuously create and destroy std::unique_ptr.
I am not talking about run time cost. The run time cost is trivial. There is a much bigger problem with trees with large depths using this implementation
Letting the destructor of the std:;unique_ptr is perfectly logically fine but there is a hidden caveat
but let say supposedly was depth was large
then you will have a stack overflow. There is a reason why leetcode does not crete binary trees over 2k size with large depths
You can test this yourself. Create a binary tree that not balanced of at least of a depth of just 1k and then see it crash during the destruction. My guess is that if you create a tree of 100k in real production code, its not exactly really balanced or complete. It can easily have large depth.
but none of the nodes are allocated on the stack?
Bar maybe the root node
No the calls to the destructor is recursive by default. It generates a stack call. Yes of course freeing the memory is done on the heap but you still need to call the destructor which is a call on the stack. It generates a call that is done recursively.
http://CppCon.org
β
Presentation Slides, PDFs, Source Code and other presenter materials are available at: https://github.com/cppcon/cppcon2016
β
Lifetime safety means writing code that, by construction, is guaranteed to eliminate two things: (a) use of null/dangling pointers (including pointerlike things such as references, iterators, views, an...
Herp sutter talks about this at 18:19
This would only really matter in non-trivial destructors tho
Since any trivial destructors would just be optimised away
yeah in production code, T can easily be non trivial type
wait no
it still a problem
if T is trivial type it still a problme
Yeah true sorry i wasn't thinking about the problem quite right i get what you mean
He just asked about recurssive so I gave recurrsive
@static cave yeah Recursive is not the way to go. Your best bet is delete the destructor, and instead have a manual deleter. Either let the memory automatically leak and restore it the OS(though there could be problems if you dont specify what it should be doing) or if you need clean up then use a iterative solution. Unless this is a homework assignment in which case do anything you want
test this with a tree 1m tree nodes? no problem. here's the thing, log(N) is small. your stack can handle 20 calls.
this assumes tree is balanced - not necessarily true for a BST
I already created a tree with 1 million nodes. And its pretty complete binary tree. The default maximum size is 1MB stack size. You still need to recurisvely visit every node thats how DFS works. So your space is still O(N)
in fact it stops at the 9960 stack element
you need to visit every node either way. but since it's recursive, you will only have, at max, the longest path from root to leaf of stackframes at any point in time. assuming balance, that is log(N)
i've already done this in C with an AVL with recursive destruction with 1 << 20 nodes. there is no stack overflow.
No its O(n) because you are generating a stack call for each visit. It's a DFS traversal.
and the stackframe gets popped when the destruction of the current node is complete. herb literally says it's log(N) if balanced and N in an unbalanced tree in the video you linked
DFS traversal means it will reach N nodes. so fucking what? hopefully you'll reach N nodes however you traverse, otherwise you have a leak. it doesn't take O(N) stack
No at 20:44 it says O(n)
and prior to that he says log(N)
yeah if your tree was really balanced then its Log(n) but I dont have a blaanced tree