binary search tree的java实现

 1/**
 2 * ADT binary search tree
 3 */

 4package binarySearchTree;
 5
 6import java.io.Serializable;
 7import java.util.Iterator;
 8
 9/**
10 * @author 
11 * @version 1.0
12 */

13public interface IBinarySearchTree extends Serializable
14{
15    /**
16     * Get the size of binary search tree
17     * 
18     * @return an int of binary search tree's size
19     */

20    public int size();
21    
22    /**
23     * Method to clear the binary search tree
24     */

25    public void clear();
26    
27    /**
28     * Check to know if the binary search tree is empty or not
29     * 
30     * @return true if the binary searchg tree is empty
31     */

32    public boolean isEmpty();
33    
34    /**
35     * Method adding an object into the binary search tree
36     * 
37     * @param o the object is to be add in the binary search tree
38     */

39    @SuppressWarnings("unchecked")
40    public void add(Comparable o);
41    
42    /**
43     * Remove an object in the binary search tree
44     * 
45     * @param o the object is to be remove from binary search tree
46     * @return an object that is removed
47     */

48    @SuppressWarnings("unchecked")
49    public Object remove(Comparable o);
50    
51    /**
52     * Find an object in the binary search tree
53     * 
54     * @param o a specific object is wanted to be find in binary search tree
55     * @return an object if that object exist in the tree, return null if not
56     */

57    @SuppressWarnings("unchecked")
58    public Object find(Comparable o);
59    
60    /**
61     * Return the iterator of the binary search tree
62     * 
63     * @return an Iterator object of the binary search tree
64     */

65    @SuppressWarnings("unchecked")
66    public Iterator iterator();
67}

68
  1package binarySearchTree;
  2
  3import java.io.Serializable;
  4import java.util.Iterator;
  5
  6import list.*;
  7
  8/**
  9 * @author 
 10 * @version 1.0
 11 */

 12public class BinarySearchTree implements IBinarySearchTree
 13{
 14    private class BinarySearchTreeNode implements Serializable
 15    {
 16        /**
 17         * Attributes
 18         */

 19        private static final long serialVersionUID = 1L;
 20        private BinarySearchTreeNode smallerLeft;
 21        private BinarySearchTreeNode biggerRight;
 22        private Object object;
 23        
 24        /**
 25         * Constructor
 26         */

 27        public BinarySearchTreeNode(Object object)
 28        {
 29            this.object = object;
 30            this.smallerLeft = null;
 31            this.biggerRight = null;
 32        }

 33        
 34        /**
 35         * Constructor
 36         */

 37        public BinarySearchTreeNode()
 38        {
 39            this.object = null;
 40            this.smallerLeft = null;
 41            this.biggerRight = null;
 42        }

 43        
 44        /**
 45         * Class attribute getter
 46         * 
 47         * @return the smallerLeft
 48         */

 49        public BinarySearchTreeNode getSmallerLeft()
 50        {
 51            return this.smallerLeft;
 52        }

 53        
 54        /**
 55         * Class attribute setter
 56         * 
 57         * @param smallerLeft the smallerLeft to set
 58         */

 59        public void setSmallerLeft(BinarySearchTreeNode smallerLeft)
 60        {
 61            this.smallerLeft = smallerLeft;
 62        }

 63        
 64        /**
 65         * Class attribute getter
 66         * 
 67         * @return the biggerRight
 68         */

 69        public BinarySearchTreeNode getBiggerRight()
 70        {
 71            return this.biggerRight;
 72        }

 73        
 74        /**
 75         * Class attribute setter
 76         * 
 77         * @param biggerRight the biggerRight to set
 78         */

 79        public void setBiggerRight(BinarySearchTreeNode biggerRight)
 80        {
 81            this.biggerRight = biggerRight;
 82        }

 83        
 84        /**
 85         * Class attribute getter
 86         * 
 87         * @return the object
 88         */

 89        public Object getObject()
 90        {
 91            return this.object;
 92        }

 93        
 94        /**
 95         * Class attribute setter
 96         * 
 97         * @param object the object to set
 98         */

 99        public void setObject(Object object)
100        {
101            this.object = object;
102        }

103    }

104    private class Direction implements Serializable
105    {
106        /**
107         * Attributes
108         */

109        private static final long serialVersionUID = 1L;
110        private int number;
111        
112        /**
113         * Class attribute getter
114         * 
115         * @return the number
116         */

117        public int getNumber()
118        {
119            return this.number;
120        }

121        
122        /**
123         * Class attribute setter
124         * 
125         * @param number the number to set
126         */

127        public void setNumber(int number)
128        {
129            this.number = number;
130        }

131    }

132    private class FatherNode implements Serializable
133    {
134        /**
135         * Attributes
136         */

137        private static final long serialVersionUID = 1L;
138        private BinarySearchTreeNode bstn;
139        
140        /**
141         * Class attribute getter
142         * 
143         * @return the bstn
144         */

145        public BinarySearchTreeNode getBstn()
146        {
147            return this.bstn;
148        }

149        
150        /**
151         * Class attribute setter
152         * 
153         * @param bstn the bstn to set
154         */

155        public void setBstn(BinarySearchTreeNode bstn)
156        {
157            this.bstn = bstn;
158        }

159    }

160    /**
161     * Attributes
162     */

163    private static final long serialVersionUID = 1L;
164    private BinarySearchTreeNode root;
165    private int size;
166    
167    public BinarySearchTree()
168    {
169        this.root = null;
170        this.size = 0;
171    }

172    
173    /*
174     * (non-Javadoc)
175     * 
176     * @see binarySearchTree.IBinarySearchTree#size()
177     */

178    public int size()
179    {
180        return this.size;
181    }

182    
183    /*
184     * (non-Javadoc)
185     * 
186     * @see binarySearchTree.IBinarySearchTree#clear()
187     */

188    public void clear()
189    {
190        this.root = null;
191        this.size = 0;
192    }

193    
194    /*
195     * (non-Javadoc)
196     * 
197     * @see binarySearchTree.IBinarySearchTree#isEmpty()
198     */

199    public boolean isEmpty()
200    {
201        return this.root == null;
202    }

203    
204    @SuppressWarnings("unchecked")
205    private BinarySearchTreeNode findNodeForInsertion(Comparable o,
206            BinarySearchTreeNode current, Direction direction)
207    {
208        if(o.compareTo((Comparable)current.getObject()) >= 0)// right side
209        {
210            if(current.getBiggerRight() == null)
211            {
212                direction.setNumber(2);
213                return current;
214            }

215            else
216            {
217                return findNodeForInsertion(o, current.getBiggerRight(),
218                        direction);
219            }

220        }

221        else
222        // left side
223        {
224            if(current.getSmallerLeft() == null)
225            {
226                direction.setNumber(1);
227                return current;
228            }

229            else
230            {
231                return findNodeForInsertion(o, current.getSmallerLeft(),
232                        direction);
233            }

234        }

235    }

236    
237    /*
238     * (non-Javadoc)
239     * 
240     * @see binarySearchTree.IBinarySearchTree#add(java.lang.Comparable)
241     */

242    @SuppressWarnings("unchecked")
243    public void add(Comparable o)
244    {
245        if(this.root == null)
246        {
247            BinarySearchTreeNode bstn = new BinarySearchTreeNode(o);
248            this.root = bstn;
249            this.size++;
250        }

251        else
252        {
253            Direction direction = new Direction();
254            BinarySearchTreeNode bstn = findNodeForInsertion(o, this.root,
255                    direction);
256            if(direction.getNumber() == 2)// right side
257            {
258                BinarySearchTreeNode newLeaf = new BinarySearchTreeNode(o);
259                bstn.setBiggerRight(newLeaf);
260            }

261            else
262            // left side
263            {
264                BinarySearchTreeNode newLeaf = new BinarySearchTreeNode(o);
265                bstn.setSmallerLeft(newLeaf);
266            }

267        }

268    }

269    
270    @SuppressWarnings("unchecked")
271    private BinarySearchTreeNode findNode(Comparable o,
272            BinarySearchTreeNode current, FatherNode upper, Direction direction)
273    {
274        if(o.compareTo((Comparable)current.getObject()) > 0)// right side
275        {
276            if(current.getBiggerRight() == null)// can't find it,error
277            {
278                upper.setBstn(null);
279                direction.setNumber(0);
280                return null;
281            }

282            else
283            {
284                upper.setBstn(current);
285                direction.setNumber(2);
286                return findNode(o, current.getBiggerRight(), upper, direction);
287            }

288        }

289        else if(o.compareTo((Comparable)current.getObject()) < 0)
290        // left side
291        {
292            if(current.getSmallerLeft() == null)// can't find it,error
293            {
294                upper.setBstn(null);
295                direction.setNumber(0);
296                return null;
297            }

298            else
299            {
300                upper.setBstn(current);
301                direction.setNumber(1);
302                return findNode(o, current.getSmallerLeft(), upper, direction);
303            }

304        }

305        else
306        // equal
307        {
308            return current;
309        }

310    }

311    
312    @SuppressWarnings("unchecked")
313    private BinarySearchTreeNode findNode(Comparable o,
314            BinarySearchTreeNode current)
315    {
316        if(o.compareTo((Comparable)current.getObject()) > 0)// right side
317        {
318            if(current.getBiggerRight() == null)
319            {
320                return null;
321            }

322            else
323            {
324                return findNode(o, current.getBiggerRight());
325            }

326        }

327        else if(o.compareTo((Comparable)current.getObject()) < 0)
328        // left side
329        {
330            if(current.getSmallerLeft() == null)
331            {
332                return null;
333            }

334            else
335            {
336                return findNode(o, current.getSmallerLeft());
337            }

338        }

339        else
340        // equal
341        {
342            return current;
343        }

344    }

345    
346    private BinarySearchTreeNode findLeftestLeaf(BinarySearchTreeNode current,
347            FatherNode upper)
348    {
349        if(current.getSmallerLeft() == null)
350        {
351            return current;
352        }

353        else
354        {
355            upper.setBstn(current);
356            return findLeftestLeaf(current.getSmallerLeft(), upper);
357        }

358    }

359    
360    /*
361     * (non-Javadoc)
362     * 
363     * @see binarySearchTree.IBinarySearchTree#remove(java.lang.Comparable)
364     */

365    @SuppressWarnings("unchecked")
366    public Object remove(Comparable o)
367    {
368        if(this.root == null)
369        {
370            return null;
371        }

372        FatherNode upper = new FatherNode();
373        Direction direction = new Direction();
374        BinarySearchTreeNode bstn = findNode(o, this.root, upper, direction);
375        if(bstn == null)
376        {
377            return null;
378        }

379        else
380        {
381            if(bstn.getSmallerLeft() == null && bstn.getBiggerRight() == null)// no
382            // children
383            {
384                if(upper.getBstn() == null)// root
385                {
386                    Object object = this.root.getObject();
387                    this.root = null;
388                    this.size--;
389                    return object;
390                }

391                else
392                // not root
393                {
394                    if(direction.getNumber() == 1)// left side to the leaf
395                    // node
396                    {
397                        Object object = bstn.getObject();
398                        upper.getBstn().setSmallerLeft(null);
399                        this.size--;
400                        return object;
401                    }

402                    else
403                    // right side to the leaf node
404                    {
405                        Object object = bstn.getObject();
406                        upper.getBstn().setBiggerRight(null);
407                        this.size--;
408                        return object;
409                    }

410                }

411            }

412            else if(bstn.getSmallerLeft() == null)// one child on the right
413            // side, not leaf
414            {
415                if(upper.getBstn() == null)// root
416                {
417                    Object object = this.root.getObject();
418                    this.root = this.root.getBiggerRight();
419                    this.size--;
420                    return object;
421                }

422                else
423                // not root
424                {
425                    if(direction.getNumber() == 1)// goes left side to reach
426                    // it
427                    {
428                        Object object = bstn.getObject();
429                        upper.getBstn().setSmallerLeft(bstn.getBiggerRight());
430                        this.size--;
431                        return object;
432                    }

433                    else
434                    // goes right side to reach it
435                    {
436                        Object object = bstn.getObject();
437                        upper.getBstn().setBiggerRight(bstn.getBiggerRight());
438                        this.size--;
439                        return object;
440                    }

441                }

442            }

443            else if(bstn.getBiggerRight() == null)// one child on the left
444            // side, not leaf
445            {
446                if(upper.getBstn() == null)// root
447                {
448                    Object object = this.root.getObject();
449                    this.root = this.root.getSmallerLeft();
450                    this.size--;
451                    return object;
452                }

453                else
454                // not root
455                {
456                    if(direction.getNumber() == 1)// goes left side to reach
457                    // it
458                    {
459                        Object object = bstn.getObject();
460                        upper.getBstn().setSmallerLeft(bstn.getSmallerLeft());
461                        this.size--;
462                        return object;
463                    }

464                    else
465                    // goes right side to reach it
466                    {
467                        Object object = bstn.getObject();
468                        upper.getBstn().setBiggerRight(bstn.getSmallerLeft());
469                        this.size--;
470                        return object;
471                    }

472                }

473            }

474            else
475            // two children
476            {
477                if(upper.getBstn() == null)// root
478                {
479                    FatherNode subUpper = new FatherNode();
480                    BinarySearchTreeNode newNode = findLeftestLeaf(bstn
481                            .getBiggerRight(), subUpper);
482                    Object object = this.root.getObject();
483                    newNode.setSmallerLeft(this.root.getSmallerLeft());
484                    newNode.setBiggerRight(this.root.getBiggerRight());
485                    if(subUpper.getBstn() == null)// only one level
486                    {
487                        newNode.setBiggerRight(null);
488                    }

489                    else
490                    // more then one level
491                    {
492                        subUpper.getBstn().setSmallerLeft(null);
493                    }

494                    this.root = newNode;
495                    this.size--;
496                    return object;
497                }

498                else
499                {
500                    if(direction.getNumber() == 1)// goes left side to reach
501                    // it
502                    {
503                        FatherNode subUpper = new FatherNode();
504                        BinarySearchTreeNode newNode = findLeftestLeaf(bstn
505                                .getBiggerRight(), subUpper);
506                        Object object = bstn.getObject();
507                        newNode.setSmallerLeft(bstn.getSmallerLeft());
508                        newNode.setBiggerRight(bstn.getBiggerRight());
509                        if(subUpper == null)// only one level
510                        {
511                            newNode.setBiggerRight(null);
512                        }

513                        else
514                        // more then one level
515                        {
516                            subUpper.getBstn().setSmallerLeft(null);
517                        }

518                        upper.getBstn().setSmallerLeft(newNode);
519                        this.size--;
520                        return object;
521                    }

522                    else
523                    // goes right side to reach it
524                    {
525                        FatherNode subUpper = new FatherNode();
526                        BinarySearchTreeNode newNode = findLeftestLeaf(bstn
527                                .getBiggerRight(), subUpper);
528                        Object object = bstn.getObject();
529                        newNode.setSmallerLeft(bstn.getSmallerLeft());
530                        newNode.setBiggerRight(bstn.getBiggerRight());
531                        if(subUpper == null)// only one level
532                        {
533                            newNode.setBiggerRight(null);
534                        }

535                        else
536                        // more then one level
537                        {
538                            subUpper.getBstn().setSmallerLeft(null);
539                        }

540                        upper.getBstn().setBiggerRight(newNode);
541                        this.size--;
542                        return object;
543                    }

544                }

545            }

546        }

547    }

548    
549    /*
550     * (non-Javadoc)
551     * 
552     * @see binarySearchTree.IBinarySearchTree#find(java.lang.Comparable)
553     */

554    @SuppressWarnings("unchecked")
555    public Object find(Comparable o)
556    {
557        if(this.root == null)
558        {
559            return null;
560        }

561        BinarySearchTreeNode bstn = findNode(o, this.root);
562        if(bstn == null)
563        {
564            return null;
565        }

566        else
567        {
568            return bstn.getObject();
569        }

570    }

571    
572    /*
573     * (non-Javadoc)
574     * 
575     * @see binarySearchTree.IBinarySearchTree#iterator()
576     */

577    @SuppressWarnings("unchecked")
578    public Iterator iterator()
579    {
580        DoublyLinkedList dll = getDLL();
581        return dll.iterator();
582    }

583    
584    private DoublyLinkedList getDLL()
585    {
586        DoublyLinkedList dll = new DoublyLinkedList();
587        fillDLL(dll, this.root);
588        return dll;
589    }

590    
591    private void fillDLL(DoublyLinkedList dll, BinarySearchTreeNode current)
592    {
593        if(current == null)
594        {
595            return;
596        }

597        if(current.getSmallerLeft() == null && current.getBiggerRight() == null)
598        {
599            dll.add(current.getObject());
600            return;
601        }

602        if(current.getSmallerLeft() != null)
603        {
604            fillDLL(dll, current.getSmallerLeft());
605        }

606        dll.add(current.getObject());
607        if(current.getBiggerRight() != null)
608        {
609            fillDLL(dll, current.getBiggerRight());
610        }

611    }

612}

613
posted @ 2007-12-03 06:39  N/A2011  阅读(638)  评论(0编辑  收藏  举报