package edu.stuy.util;

import java.util.TreeMap;

public class HighScores {
    protected ScoreTree _scores;

    public HighScores() {
        _scores = new ScoreTree();
    }

    public void addScore(String name, long score) {
        _scores.add(new HighScore(name, score));
    }

    public HighScore[] getScoresSorted() {
        return _scores.getScoresSorted();
    }

    private class ScoreTree {
        private HighScore _val;
        private ScoreTree _left, _right;
        private int _size;

        public ScoreTree(HighScore score) {
            _val = score;
            _size = 1;
        }
        public ScoreTree() {
            this(null);
            _size = 0;
        }

        public void add(HighScore score) {
            _size++;
            if (_val == null) _val = score;
            else {
                if (score.compareTo(_val) > 0) {
                    if (_right == null) _right = new ScoreTree(score);
                    else _right.add(score);
                } else {
                    if (_left == null) _left = new ScoreTree(score);
                    else _left.add(score);
                }
            }
        }

        /*
         * Returns an array with the scores, sorted from lowest to highest
         */
        public HighScore[] getScoresSorted() {
            HighScore[] sortedScores = new HighScore[_size];
            int index = 0;
            if (_left != null) {
                HighScore[] leftScores = _left.getScoresSorted();
                for (int j = 0; j < leftScores.length; j++) {
                    sortedScores[index] = leftScores[j];
                    index++;
                }
            }
            if (_val != null) {
                sortedScores[index] = _val;
                index++;
            }
            if (_right != null) {
                HighScore[] rightScores = _right.getScoresSorted();
                for (int j = 0; j < rightScores.length; j++) {
                    sortedScores[index] = rightScores[j];
                    index++;
                }
            }
            return sortedScores;
        }
    }

}

@@ -42,16 +42,132 @@
 TreeMap;
+%0Aimport java.io.BufferedReader;%0Aimport java.io.FileReader;%0Aimport java.io.BufferedWriter;%0Aimport java.io.FileWriter;
 %0A%0Apublic
@@ -484,24 +484,1317 @@
 d();%0A    %7D%0A%0A
+    public void saveScores(String filename) %7B%0A        try %7B%0A            BufferedWriter w = new BufferedWriter(new FileWriter(filename));%0A            HighScore%5B%5D scores = getScoresSorted();%0A            for (int i = 0; i %3C scores.length; i++) %7B%0A                w.write(scores%5Bi%5D.getName().replace(%22:%22, %22%5C%5C:%22)); //Escape colons%0A                w.write(%22 : %22); // separator%0A                w.write(%22%22 + scores%5Bi%5D.getScore());%0A                w.newLine();%0A            %7D%0A            w.close();%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A        %7D%0A    %7D%0A%0A    public void saveScores() %7B%0A        saveScores(%22scores.txt%22);%0A    %7D%0A%0A    public void loadScores(String filename) %7B%0A        try %7B%0A            BufferedReader b = new BufferedReader(new FileReader(filename));%0A            while (b.ready()) %7B%0A                String%5B%5D data = b.readLine().split(%22 : %22);%0A                String name = data%5B0%5D.replace(%22%5C%5C:%22, %22:%22); // We'll escape their colons whilst saving, so unescape them whilst loading%0A                long score = Long.parseLong(data%5B1%5D);%0A                addScore(name, score);%0A            %7D%0A            b.close();%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A        %7D%0A    %7D%0A    %0A    public void loadScores() %7B%0A        loadScores(%22scores.txt%22);%0A    %7D%0A%0A
     private 

Add File loading/saving to HighScores