Java Reference
In-Depth Information
33 @return true if the object is contained in the tree
34 */
35 public boolean find(Comparable obj)
36 {
37 Node current = root;
38 while (current != null )
39 {
40 int d = current.data.compareTo(obj);
41 if (d == 0 ) return true ;
42 else if (d > 0 ) current =
current.left;
43 else current = current.right;
44 }
45 return false ;
46 }
47
48 /**
49 Tries to remove an object from the tree. Does nothing
50 if the object is not contained in the tree.
51 @param obj the object to remove
52 */
53 public void remove(Comparable obj)
54 {
55 // Find node to be removed
56
57 Node toBeRemoved = root;
58 Node parent = null ;
59 boolean found = false ;
60 while (!found && toBeRemoved != null )
61 {
62 int d =
toBeRemoved.data.compareTo(obj);
63 if (d == 0 ) found = true ;
64 else
65 {
66 parent = toBeRemoved;
67 if (d > 0 ) toBeRemoved =
toBeRemoved.left;
68 else toBeRemoved =
toBeRemoved.right;
69 }
70 }
728
729
Search WWH ::




Custom Search