Click here to Skip to main content
15,885,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a b tree and I am given a key and I need to return the number of nodes that are greater than the key. I tried many ways but I can't get this problem I think the confusing part is how to go back to check the next children. So far I just implemented the search but not sure how to go back up to the root.

What I have tried:

def _nums_larger(self, k, node):
      if node is None:
        return 0
      count = 0
      for key in node.keys:
        if key > k:
          count +=  1
      if node.is_leaf:
        return -1
      for i, key in enumerate(node.keys):
        if key > k:
          count += self._nums_larger(k,node.children[i])
      self._nums_larger(k, node.children[-1])
      return count
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900