Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. //The idea behind this function is to scan the tree "in order" recursively
  2. public void recKeysToArray(IWAVLNode currNode, int[] arr) {
  3. if(!currNode.isRealNode()) //If currNode is a virtual node, do nothing
  4. return;
  5. if(((WAVLNode)currNode).getRank() == 0) { //If currNode is a leaf
  6. arr[currIndex] = currNode.getKey();
  7. currIndex++;
  8. }
  9. else {
  10. recKeysToArray(currNode.getLeft(), arr);
  11. arr[currIndex] = currNode.getKey();
  12. currIndex++;
  13. recKeysToArray(currNode.getRight(), arr);
  14. }
  15. }
  16. /**
  17. * public int[] keysToArray()
  18. *
  19. * Returns a sorted array which contains all keys in the tree,
  20. * or an empty array if the tree is empty.
  21. */
  22. public int[] keysToArray()
  23. {
  24. this.currIndex = 0;
  25. int[] arr = new int[this.root.getSubtreeSize()]; //num of keys = num of nodes in the tree
  26. recKeysToArray(this.root, arr);
  27. return arr;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement