I'm trying to implement a self balancing binary tree for practice. One of the obvious internal operations is a tree rotation. However, a tree rotation needs to take account the possibility that one of the child nodes is the same as the parent node; for example, if I have left <= middle < right
, and my current node is
0
/ \
0 1
Then I can't rotate clockwise. However, if I were to allow equal values to be found on either side, then I wouldn't have to worry about that.
So far I haven't found any problems...
- Insertions could still be fast. It could still follow
left <= middle < right
and balance as needed, or figure out the optimal side when inserting equal values - Searches would be unaffected, assuming you just need to find a single occurences (no need to split if the values are equal, because you've already found it)
- Deleting a single node should be fine because its just a search
- Deleting all occurences of a value would require the aforementioned splitting - if I want to delete all occurences of A, I'd have to search both sides of the node if I encountered A, whereas if I kept it strictly
left <= middle < right
(or vise versa) I'd only have to search one side. But it doesn't seem like it'd degenerate - I think it also lets me always keep the height on the left side no more than +-1 from the heigh on the right side.
I'm wondering if:
- There are any cases where any operations on the tree would degenerate
- This violates any "principles of a BST"
- There's anything else I missed and should consider
I'm doing this in Haskell, if it makes a difference