long story short, given node p, I want to find the next smallest value (after p) in a balanced binary tree.
- So given node p, if it has a left child, the next smallest would be the right most of its left child (traverse right from left child until it can't anymore).
- If left child does not have a right child, then left child is the smallest.
- If p does not have a left child, from p we will traverse up the tree until we find a node that is the right child of a parent node, then that parent node will be the next smallest.
- if we go up all the way and found no nodes that's the right child of a parent, then there's no smaller values.
I think that's it. AM I missing anything? The run time of this I think is roughly log(N). If I want to find T largest values in a binary search tree, it should be Tlog(N).
Here's the longer version of the problem for those who are interested. I have an array of numbers that's changing with every iteration. With every iteration it's losing a number and gaining a number. You can think of replacing a number with another. I know which 2 numbers that will be. I need to find the sum of T largest numbers in the array with each iteration. so my strategy was to use a binary search tree(red-black) for faster insert, then find + delete, and finally find T largest(I traverse all the way to largest and call findNextSmallest T - 1 times). whole thing should be (2 + T)log(N)
- is my approach to finding next smallest correct?
- is there a better way of doing this than what I have?