Subversion Repositories programming

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
41 irasnyd 1
// Written by Ira Snyder
2
// Due Date: 11-24-2004
3
// Project #4
4
 
5
import java.io.*;
6
import java.util.*;
7
 
8
class AVLTree {
9
    private int key, height;
10
    private AVLTree left, right;
11
 
12
    public static final AVLTree NIL = new AVLTree();
13
 
14
    public AVLTree( int key ) {
15
        this.key = key;
16
        left = right = NIL;
17
    }
18
 
19
    public boolean add( int key ) {
20
        int oldSize = size();
21
        grow(key);
22
        return size() > oldSize;
23
    }
24
 
25
    public AVLTree grow( int key ) {
26
        if( this == NIL ) return new AVLTree(key);
27
        if( key == this.key ) return this; //prevent key dupes
28
        if( key < this.key ) left = left.grow(key);
29
        else right = right.grow(key);
30
 
31
        rebalance();
32
 
33
        height = 1 + Math.max(left.height,right.height);
34
        return this;
35
    }
36
 
37
    public int size() {
38
        if( this == NIL ) return 0;
39
        return 1 + left.size() + right.size();
40
    }
41
 
42 irasnyd 42
    //I like the BinaryTree toString better than the author's toString()
43
    //so I used it here instead. I left the author's code for completeness.
44
    /*
41 irasnyd 45
    public String toString() {
46
        if( this == NIL ) return "";
47
        return left + " " + key + " " + right;
48
    }
42 irasnyd 49
    */
50
 
51
    public String toString() {
52
 
53
        if( this == NIL ) return "()";
54
 
55
        String sLeft = sLeft = left.toString();
56
        String sRight = sRight = right.toString();
57
        String answer = new String();
58
 
59
        //assemble the string to return
60
        answer = "(";
61
        if( !sLeft.equals("()") ) { answer += sLeft + ","; }
62
        answer += key;
63
        if( !sRight.equals("()") ) { answer += "," + sRight; }
64
        answer += ")";
65
 
66
        //return the assembled string
67
        return answer;
68
    }
69
 
41 irasnyd 70
    private AVLTree() { //construct the empty tree
71
        left = right = this;
72
        height = -1;
73
    }
74
 
75
    private AVLTree( int key, AVLTree left, AVLTree right ) {
76
        this.key = key;
77
        this.left = left;
78
        this.right = right;
79
        height = 1 + Math.max(left.height,right.height);
80
    }
81
 
82
    private void rebalance() {
83
        if( right.height > (left.height + 1) ) {
84
            if( right.left.height > right.right.height ) right.rotateRight();
85
            rotateLeft();
86
        }
87
        else if( left.height > (right.height + 1) ) {
88
            if( left.right.height > left.left.height ) left.rotateLeft();
89
            rotateRight();
90
        }
91
    }
92
 
93
    private void rotateLeft() {
94
        left = new AVLTree(key,left,right.left);
95
        key = right.key;
96
        right = right.right;
97
    }
98
 
99
    private void rotateRight() {
100
        right = new AVLTree(key,left.right,right);
101
        key = left.key;
102
        left = left.left;
103
    }
104
 
42 irasnyd 105
    // new functions from Prog. Probs PG 411
106
    public int getHeight() { return height; }
107
 
108
    public AVLTree getLeft() { 
109
        if( this == NIL ) { return NIL; }
110
        return left;
111
    }
112
 
113
    public AVLTree getRight() { 
114
        if( this == NIL ) { return NIL; }
115
        return right;
116
    }
117
 
118
    public int getRoot() {
119
        return key;
120
    }
121
 
122
    public boolean contains( int x ) { 
123
        if( this == NIL ) { return false; }
124
        if( key == x ) { return true; }
125
 
126
        return left.contains(x) || right.contains(x);
127
    }
128
 
129
    // am guessing this returns the tree with x == key as the root
130
    public AVLTree get( int x ) {
131
        if( this == NIL ) { return NIL; }
132
        if( x == key ) { return this; }
133
 
134
        if( x < key ) { return left.get(x); }
135
        return right.get(x);
136
    }
137
 
138
    public boolean equals( Object object ) { 
139
        if( !(object instanceof AVLTree) ) { return false; }
140
 
141
        AVLTree temp = (AVLTree)object; //make it typed
142
 
143
        boolean l=false, r=false;
144
 
145
        //check the left
146
        if( left == NIL && temp.left == NIL ) l = true;
147
        else l = left.equals(temp.left);
148
 
149
        //check the right
150
        if( right == NIL && temp.right == NIL ) r = true;
151
        else r = right.equals(temp.right);
152
 
153
        return (temp.key == key) && l && r;
154
    }
155
 
156
    public AVLTree( int[] a ) { 
157
        this(a[0]); //create a new tree
158
 
159
        //call add() for every element in the array
160
        for( int i=1; i < a.length; i++ ) add(a[i]);
161
    }
162
 
163
    /*
164
    public boolean remove( int key ) { 
165
        if( this == NIL ) { return false; }
166
        if( key < this.key ) { return left.remove(key); }
167
        if( key > this.key ) { return right.remove(key); }
168
 
169
        if( left == NIL && right == NIL ) { this = NIL; return true; }
170
        if( left == NIL ) { 
171
            this = new AVLTree(right.key,right.left,right.right);
172
            return true;
173
        }
174
        if( right == NIL ) {
175
            this = new AVLTree(left.key,left.left,left.right);
176
            return true;
177
        }
178
        this = new AVLTree( deleteMinimum(right) );
179
        rebalance(); //fix the balance
180
        return true;
181
    }
182
 
183
    private int deleteMinimum( AVLTree tree ) {
184
        int tempKey=0;
185
 
186
        if( left == NIL ) { 
187
            tempKey = key;
188
            this = new AVLTree(right.key,right.left,right.right);
189
        }
190
        if( left.left == NIL && left.right == NIL ) {
191
            tempKey = key;
192
            this = new AVLTree(left.key,left.left,right.right);
193
        }
194
        return deleteMinimum(left);
195
    }
196
    */
197
 
41 irasnyd 198
} //end AVLTree class
199