My purpose is to first locate(search) a certain node in the tree(AVL or balanced binary tree), and then count the number of nodes which are under it. The whole operation works in O(logn) times. Is it achievable?
Asked
Active
Viewed 138 times
-1
-
2When asking a question, it is suggestable to add any code implementation you have already made, result analysis, error logs, etc.. Thanks. – Marco Tizzano Apr 29 '21 at 14:18
1 Answers
0
Yes it is, for that you need an implementation of AVL that keeps for each node the number of nodes that it has in its sub-tree. Now, you only need to locate the desired node, and then look in its 'size' field to know how many nodes are in its sub-tree.

e.ad
- 380
- 2
- 10