Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //The idea behind this function is to scan the tree "in order" recursively
- public void recKeysToArray(IWAVLNode currNode, int[] arr) {
- if(!currNode.isRealNode()) //If currNode is a virtual node, do nothing
- return;
- if(((WAVLNode)currNode).getRank() == 0) { //If currNode is a leaf
- arr[currIndex] = currNode.getKey();
- currIndex++;
- }
- else {
- recKeysToArray(currNode.getLeft(), arr);
- arr[currIndex] = currNode.getKey();
- currIndex++;
- recKeysToArray(currNode.getRight(), arr);
- }
- }
- /**
- * public int[] keysToArray()
- *
- * Returns a sorted array which contains all keys in the tree,
- * or an empty array if the tree is empty.
- */
- public int[] keysToArray()
- {
- this.currIndex = 0;
- int[] arr = new int[this.root.getSubtreeSize()]; //num of keys = num of nodes in the tree
- recKeysToArray(this.root, arr);
- return arr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement