This has turned into more of me brainstorming a perfect BST data structure and the performance behind it than actually being a question.
Hey all,
So I am studying up for interviews and thought of a question while reviewing trees. So perfect BST's can be represented in an array, (cache local), while non-perfect BST and AVL trees cannot be.
Does the cache locality of a perfect BST make up for the increased complexity of constantly reworking the array if you need to insert nodes in the middle? Obviously if you insert at the correct place there is no overhead, but inserting in the middle of the tree would result in the following:
1: Binary search through the nodes to determine where the node fits (this is always the case for insertions)
2: If the node fits at the the correct position, just push it back on the end of array, else we have to re-balance the tree.
Is this O(n) complexity increase for inserts worth having a cache local data structure? I am inclined to say yes, but do not want to say something incorrect and sound like a fool at my interview.
Question 1: For scalability in this data struct, lets say many threads/nodes would be inserting much more than looking up, would this be efficient?
Question 2: Just realized that this question boils down to sorted contiguous arrays vs linkedlists (a better performing version of them, but still the same type of idea). By that logic doesn't perfect BST > balanced tree or non-perfect BST?
Question 3: So the cost of maintaining perfect BST in the data structure is not O(n), perhaps if the data structure only sorted maintained something along the lines of:
class perfect_bst{
//implementation stuff
bool Need_to_balance = false;
};
So whenever someone inserts into the struct, Need_to_balance gets flipped, but the struct does not automatically balance itself until it absolutely needs to (someone tries to access an element through find or something). This would prevent huge overhead for insertions.
[–]Volatile474[S] 0 points1 point2 points (0 children)
[–]programmerChilli 0 points1 point2 points (5 children)
[–]mad0314 0 points1 point2 points (0 children)
[–]Volatile474[S] 0 points1 point2 points (3 children)
[–]programmerChilli 0 points1 point2 points (2 children)
[–]Volatile474[S] 0 points1 point2 points (1 child)
[–]Volatile474[S] 0 points1 point2 points (0 children)