c++ - Count the number of nodes in an AVL tree in a given range -


i'm required write c++ function that, given range (a,b], returns number of nodes in avl tree in given range, in log(n) time complexity. can add more fields tree's nodes if need so.

i should point out a,b not appear in tree. example, if tree's nodes are: 1,2,5,7,9,10, running function using parameters (3,9] should return 3.

which algorithm should use achieve this?

this famous problem - dynamic order statistcs tree augmentation.

you need augment nodes when @ child pointer, know how many children in child's subtree @ time o(1). it's easy see can done without affecting complexity.

once have that, can answer query (between , that, inclusive/exclusive - possibilities) performing 2 traversals node roots. exact traversals depend on details (check functions lower_bound , upper_bound in c++ example).


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -