Hello all, I would once again like to ask for advice. I've created a binary tree that adds and deletes random elements. I read in my Data Structures text, regarding the below deleteByCopying() function, which can lead to an unbalanced tree, that:
'a simple improvement can make the algorightm symmetrical. The algorithm can alternately delete the predecessor of the key in node from the left subtree and delete its successor form the right subtree.'
What does this mean in regards to:
I can't figure out why it's talking about deleting predecessors and successors. The sucessor from the right subtree sounds like node->right, the right subchild, but: successor from the left subtree? Successor sounds like it's parent, prev, and not like node->left. Can anyone help?Code:template<class T> void deleteByCopying(BinSearchNode<T>*& node) { BinSearchNode<T> *previous, *tmp = node; if(node->right == 0) node = node->left; else if(node->left == 0) node = node->right; else { tmp = node->left; previous = node; while(tmp->right != 0) { previous = tmp; tmp = tmp->right; } node->key = tmp->key; if(previous == node) previous->left = tmp->left; else previous->right = tmp->left; } delete tmp; }
Thanks,
-Patrick



LinkBack URL
About LinkBacks



air, modified my function and included the <utility> header, and included pair class def., but the compiler does not like my use of enqueue() for pair (do I have to overload it?), and doesn't accept my declaration of pair in the getIPL function. As usual, thanks for any help, and what you've done so far!