If I have been given a certain number of numbers (which I store in a balanced binary search tree for ease) , Then I want to answer a question, what is the smallest number ith between me [A, B], what is the need to inform a fast algorithm for doing that task?
Technically I can cross the tree from Route Search for A (or if one does not exist, then immediately a number from that number), compared to B. (or compared to B. In addition to searching for a small number), and while doing so I can keep a counter for it, to determine when I will be Ith number but it does not seem optimal.
O (log n), which I can do at the height of the tree Rwbumic'm used to set the sets?
Thank you
You can certainly do this, if you are in the nodes To create a traversal o (logon)
, we need to know each node to know, how many nodes are the right sub-projection (you can maintain this information and However, O (log (n)) are adding / removing the tree from time to time.
def search (currentNode, k) if k == 0 then currentNode else if currentNode.rightBranchSize & gt ; = K then we remove 1 from k because the current node is no longer returned (currentNode.right, k - 1) else // We reduce k as the current node and its full rights are subdivisions And return search (currentNode.parent, k - currentNode.rightBranchSize - 1) End End
It is believed that the code should be quite self explanatory.
We Start > = A
to be the first node with the number and search for the key element (key-based)
- Schnaader in your comment, it would be easier to cross the tree if
k
is small. - Most extensive library (such as STL) will not allow you tree trees (they do not provide any type of
node
left with pointers) and Right subtrees). Therefore, although the algorithm is simple, it can be difficult to implement. - This takes only a slight change to consider from your question to
B
.
Comments
Post a Comment