Subversion Repositories programming

Rev

Rev 48 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
47 irasnyd 1
// Written by Ira Snyder
2
// Due Date: 11-24-2004
3
// Project #4
4
// Helped and was Helped By: Allen Oliver
5
// Got information from: 
6
//     http://www.cs.dartmouth.edu/~chepner/cs15/notes/08_bst.html
7
 
8
import java.io.*;
9
import java.util.*;
10
 
11
class BSTree {
12
    private int key, height;
13
    private Object data;
14
    private BSTree left, right;
15
 
16
    //create an empty tree to be used instead of checking
17
    //for nulls all over the place
18
    public static final BSTree NIL = new BSTree();
19
 
20
    //constructor for the basic BSTree
21
    public BSTree( int key, Object data ) {
22
        this.key = key;
23
        this.data = data;
24
        left = right = NIL;
25
    }
26
 
27
    //method to insert a new key-data pair into the tree
28
    public boolean add( int key, Object data ) {
29
        int oldSize = size();
30
        grow(key,data);
31
        return size() > oldSize;
32
    }
33
 
34
    //provide name mapping for add
35
    public boolean insert( int key, Object data ) {
36
        return add(key,data);
37
    }
38
 
39
    //method to add an element to a tree
40
    private BSTree grow( int key, Object data ) {
41
        if( this == NIL ) return new BSTree(key,data);
42
 
43
        //prevent key dupes, update data
44
        if( key == this.key ) { this.data = data; return this; } 
45
        if( key < this.key ) left = left.grow(key,data);
46
        else right = right.grow(key,data);
47
 
48
        height = 1 + Math.max(left.height,right.height);
49
        return this;
50
    }
51
 
52
    //method to return the size of the tree
53
    public int size() {
54
        if( this == NIL ) return 0;
55
        return 1 + left.size() + right.size();
56
    }
57
 
58
    //I like the BinaryTree toString better than the author's toString()
59
    //so I used it here instead. I left the author's code for completeness.
60
    /*
61
    public String toString() {
62
        if( this == NIL ) return "";
63
        return left + " " + key + " " + right;
64
    }
65
    */
66
 
67
    //method to print the tree as a String
68
    public String toString() {
69
 
70
        if( this == NIL ) return "()";
71
 
72
        String sLeft = sLeft = left.toString();
73
        String sRight = sRight = right.toString();
74
        String answer = new String();
75
 
76
        //assemble the string to return
77
        answer = "(";
78
        if( !sLeft.equals("()") ) { answer += sLeft + ","; }
79
        answer += key;
80
        if( !sRight.equals("()") ) { answer += "," + sRight; }
81
        answer += ")";
82
 
83
        //return the assembled string
84
        return answer;
85
    }
86
 
87
    //constructor to make the empty tree
88
    private BSTree() {
89
        left = right = this;
90
        height = -1;
91
    }
92
 
93
    //constructor to make a new BSTree given a key-data pair, and left
94
    //and right subtrees
95
    private BSTree( int key, Object data, BSTree left, BSTree right ) {
96
        this.key = key;
97
        this.data = data;
98
        this.left = left;
99
        this.right = right;
100
        height = 1 + Math.max(left.height,right.height);
101
    }
102
 
103
    //method to return the height of the tree
104
    public int getHeight() { return height; }
105
 
106
    //method to return the height of the subtree with the
107
    //given key
108
    public int getHeight( int key ) {
109
        if( this == NIL ) { return -1; }
110
        if( key == this.key ) { return getHeight(); }
111
        if( key < this.key ) { return left.getHeight(key); }
112
        return right.getHeight(key);
113
    }
114
 
115
    //method to return the left subtree
116
    public BSTree getLeft() { 
117
        if( this == NIL ) { return NIL; }
118
        return left;
119
    }
120
 
121
    //method to return the left subtree of the subtree with the
122
    //given key
123
    public BSTree getLeft( int key ) {
124
        if( this == NIL ) { return null; } //key not in tree
125
        if( key == this.key ) { return getLeft(); }
126
        if( key < this.key ) { return left.getLeft(key); }
127
        return right.getLeft(key);
128
    }
129
 
130
    //method to return the right subtree
131
    public BSTree getRight() {
132
        if( this == NIL ) { return NIL; }
133
        return right;
134
    }
135
 
136
    //method to return the right subtree of the subtree with the
137
    //given key
138
    public BSTree getRight( int key ) {
139
        if( this == NIL ) { return null; } //key not in tree
140
        if( key == this.key ) { return getRight(); }
141
        if( key < this.key ) { return left.getRight(key); }
142
        return right.getRight(key);
143
    }
144
 
145
    //method to return the key of the root
146
    public int getRoot() { return key; }
147
 
148
    //method to return the key of the root
149
    public int getKey() { return key; }
150
 
151
    //method to return the data of the root
152
    public Object getData() { return data; }
153
 
154
    //method to see if the tree contains a key
155
    public boolean contains( int x ) { 
156
        if( this == NIL ) { return false; }
157
        if( key == x ) { return true; }
158
 
159
        return left.contains(x) || right.contains(x);
160
    }
161
 
162
    //return the data associated with a given key
163
    public Object get( int key ) {
164
        if( this == NIL ) { return NIL; }
165
        if( key == this.key ) { return data; }
166
 
167
        if( key < this.key ) { return left.get(key); }
168
        return right.get(key);
169
    }
170
 
171
    //check if two trees are equals
172
    public boolean equals( Object object ) { 
173
        if( !(object instanceof BSTree) ) { return false; }
174
 
175
        BSTree temp = (BSTree)object; //make it typed
176
 
177
        boolean l=false, r=false;
178
 
179
        //check the left
180
        if( left == NIL && temp.left == NIL ) l = true;
181
        else l = left.equals(temp.left);
182
 
183
        //check the right
184
        if( right == NIL && temp.right == NIL ) r = true;
185
        else r = right.equals(temp.right);
186
 
187
        return (temp.key == key) && l && r;
188
    }
189
 
190
    //method to create a new tree from an array of keys,
191
    //and an array of data
192
    public BSTree( int[] keys, Object[] data ) { 
193
        //this(keys[0],data[0]); //create a new tree
194
 
195
        if( keys.length != data.length ) { throw new IllegalArgumentException(); }
196
 
197
        this.key = keys[0];
198
        this.data = data[0];
199
        left = right = NIL;
200
 
201
        //call add() for every element in the array
202
        for( int i=1; i < keys.length; i++ ) add(keys[i],data[i]);
203
    }
204
 
205
    //method to remove a key-data pair from the tree
206
    public boolean remove( int key ) {
207
        if( !contains(key) ) { return false; }
208
        removeHelper(this,key);
209
        if( contains(key) ) { return false; }
210
        return true;
211
    }
212
 
213
    //a helper method for remove, which performs the actual recursion
214
    private BSTree removeHelper( BSTree tree, int key ) {
215
        if( tree == NIL ) { return NIL; }
216
        else if( key < tree.key ) { tree.left = removeHelper(tree.left,key); }
217
        else if( key > tree.key ) { tree.right = removeHelper(tree.right,key); }
218
        else if( tree.left != NIL && tree.right != NIL ) { 
219
            tree = deleteMinimum(tree.right); 
220
        }
221
        else if( tree.left != NIL ) { tree = tree.left; }
222
        else { tree = tree.right; }
223
 
224
        return tree;
225
    }
226
 
227
    //method to remove the minimum element in a tree
228
    private BSTree deleteMinimum( BSTree tree ) {
229
        if( tree == NIL ) { return NIL; }
230
        if( tree.left == NIL && tree.right == NIL ) { return NIL; }
231
        if( tree.left == NIL ) { 
232
            return new BSTree(tree.right.key,tree.right.data,
233
                               tree.right.left,tree.right.right); 
234
        }
235
        return new BSTree(tree.key,tree.data,deleteMinimum(tree.left),tree.right);
236
    }
237
 
238
    //method to print a tree InOrder
239
    public static void printInOrder( BSTree tree ) {
240
        Queue queue = new Queue();
241
        queue.enqueue(tree);
242
 
243
        while( !queue.isEmpty() ) {
244
            BSTree temp = (BSTree)queue.dequeue();
245
            System.out.print(temp.getKey());
246
            if( temp.getLeft() != NIL ) { queue.enqueue(temp.getLeft()); }
247
            if( temp.getRight() != NIL ) { queue.enqueue(temp.getRight()); }
248
        }
249
    }
250
 
251
    //method to print a tree in PreOrder
252
    public static void printPreOrder( BSTree tree ) {
253
        if( tree == NIL ) return;
254
 
255
        System.out.print(tree.getKey());
256
        BSTree.printPreOrder( tree.getLeft() );
257
        BSTree.printPreOrder( tree.getRight() );
258
    }
259
 
260
} //end BSTree class
261