views:

200

answers:

5

Looking at the implementation on Wikipedia, it would seem that a standard BST (non self balancing) never re-arranges itself during inserts, and so the very first item added will always be the root. Is this correct? If so, doesn't that mean that there is the potential for a BST to often have much worse than O(logN)?

Using this as a reference for a recursive insert:

 /* Inserts the node pointed to by "newNode" into the subtree rooted at "treeNode" */
 void InsertNode(Node* &treeNode, Node *newNode)
 {
     if (treeNode == NULL)
       treeNode = newNode;
     else if (newNode->key < treeNode->key)
       InsertNode(treeNode->left, newNode);
     else
       InsertNode(treeNode->right, newNode);
 }
A: 

Yes, the order of insertion can have negative effects on the shape/balance of the resulting tree. As you pointed out, the resulting worst case for a tree is worse than O(log(N)) and could end up looking simply like a linked-list .

Pete
A: 

Yes, which is why self-balancing BSTs exist.

Pesto
A: 

Some good info in this SO answer.

JP Alioto
Thanks. But I just want to confirm my reading of this, the very first node added to a BST will always from that point on be the head, correct?
ApplePieIsGood
Yes, it will be a one node tree with just a root. Then the second number you add will be a child to either the left or right dependng on whether it's bigger than the root and etc.
JP Alioto
Yeah I knew that if I added one it would be the root, but I was just looking to confirm that no matter how many more I added, that first one would remain the root. So it does seem to be the case.
ApplePieIsGood
A: 

Yes, unbalanced BSTs can be bad. In fact if you add sorted data to a BST, you can quickly degenerate your tree to single linked list performance, which has an insert of O(n), assuming an insert is at the end.

A standard BST will do decently well on average if you are dealing with random data. You're worst enemy is sorted, or reverse sorted data.

This is why you'll usually want to use a balanced BST(just pick one from your language's library).

Kekoa
+1  A: 

Yes, it will always be on the root node simply because:

  • that's the only place you can put it in an empty tree; and
  • you're not moving it.

Of course, you can delete it which will result in another node becoming the root but that doesn't move the original first node elsewhere in the tree.

The degenerate case for an unbalanced binary tree is a linked list, which has a search time complexity of O(n).

paxdiablo
O(1/2n) for a list.
Blank Xavier
@BlankXavier, this is a misunderstanding of BigO notation. There is no O(n/2) since the O(n) is a relative measure of complexity based on n and the multiplication by half doesn't change that relation at all. O(n), O(n/2) and O(n*1e200) are all the same.
paxdiablo