My professor showed the following problem in class and mentioned that the answer is O(1) while mine was quit different, I hope to get some help knowing of what mistakes did I made.
Question:
Calculate the Amortized Time Complexity for F method in AVL tree, when we start from the minimal node and each time we call F over the last found member.
Description of F: when we are at specific node F continues just like inorder traversal starting from the current one until the next one in inorder traversal for the next call.
What I did:
First I took an random series of m calls to F.
I said for the first one we need O(log n) - to find the most minimal node then for the next node we need to do inorder again but continues one more step so O(log n)+1 an so on until I scan m elements.
Which gets me to:
To calculate Amortized Time we do T(m)/m then I get:
Which isn't O(1) for sure.


The algorithm doesn't start by searching for any node, but instead is already passed a node and will start from that node. E.g. pseudocode for
Fwould look like this:The above code basically does an in-order traversal of a tree starting from a node until the next node is reached.
Amortized runtime:
Pick an arbitrary tree and
m. Letr_0be the lowest common ancestor of all nodes visited byF. Now definer_(n + 1)as the lowest common ancestor of all nodes in the right subtree ofr_nthat will be returned byF. This recursion bottoms out forr_u, which will be them-th node in in-order traversal. Anyr_nwill be returned byFin some iteration, so all nodes in the left subtree ofr_nwill be returned byFas well.All nodes that will be visited by
Fare either also returned byFor are nodes on the path fromr_0tor_u. Sincer_0is an ancestor ofr_1andr_1is an ancestor ofr_2, etc., the path fromr_0tor_ucan be at most as long as the right subtree is high. The height of the tree is limited bylog_phi(m + 2), so in total at mostnodes will be visited during
miterations ofF. All nodes visited byFform a subtree, so there are at most2 * (m + log_phi(m + 2))edges that will be traversed by the algorithm, leading to an amortized runtime-complexity of(The above bounds are in reality considerably tighter, but for the calculation presented here completely sufficient)