Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. O(n)
  2. countAllInRange(7,10)
  3.  
  4. Function int countAllInRange(k1,k2, tree){
  5. If (tree == null)
  6. Return 0;
  7. Else
  8. If(tree.node.value >= k1 && tree.node.value <= k2)
  9. Return 1+ countAllInRange(k1,k2, tree->left)
  10. + countAllInRange(k1,k2, tree->right)
  11. Else
  12. If(tree.node.value < k1)
  13. Return countAllInRange(k1,k2, tree->right)
  14. If(tree.node.value > k2)
  15. Return countAllInRange(k1,k2, tree->left)
  16.  
  17.  
  18.  
  19. Return countAllInRange(k1,k2, tree->left)
  20. + countAllInRange(k1,k2, tree->right)
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27. Foreach(node in tree)
  28. If(node.value >= k1 && node.value <= k2)
  29. countNodesInRange++;
  30.  
  31. return countNodesInRange
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement