1+ import java .util .Stack ;
2+ public class BST <E extends Comparable <E >>
3+ extends AbstractTree <E >implements Cloneable {
4+ protected TreeNode <E >root ;
5+ protected int size =0 ;
6+
7+ /** Create a default binary search tree */
8+ public BST () {
9+ }
10+
11+ /** Create a binary search tree from an array of objects */
12+ public BST (E []objects ) {
13+ for (int i =0 ;i <objects .length ;i ++)
14+ insert (objects [i ]);
15+ }
16+
17+ @ Override /** Return true if the element is in the tree */
18+ public boolean search (E e ) {
19+ TreeNode <E >current =root ;// Start from the root
20+
21+ while (current !=null ) {
22+ if (e .compareTo (current .element ) <0 ) {
23+ current =current .left ;// Go left
24+ }
25+ else if (e .compareTo (current .element ) >0 ) {
26+ current =current .right ;// Go right
27+ }
28+ else // Element matches current.element
29+ return true ;// Element is found
30+ }
31+
32+ return false ;// Element is not in the tree
33+ }
34+
35+ @ Override /** Insert element e into the binary search tree.
36+ *Return true if the element is inserted successfully. */
37+ public boolean insert (E e ) {
38+ if (root ==null )
39+ root =createNewNode (e );// Create a new root
40+ else {
41+ // Locate the parent node
42+ TreeNode <E >parent =null ;
43+ TreeNode <E >current =root ;
44+ while (current !=null ) {
45+ if (e .compareTo (current .element ) <0 ) {
46+ parent =current ;
47+ current =current .left ;
48+ }
49+ else if (e .compareTo (current .element ) >0 ) {
50+ parent =current ;
51+ current =current .right ;
52+ }
53+ else
54+ return false ;// Duplicate node not inserted
55+ }
56+ // Create the new node and attach it to the parent
57+ if (e .compareTo (parent .element ) <0 )
58+ parent .left =createNewNode (e );
59+ else
60+ parent .right =createNewNode (e );
61+ }
62+
63+ size ++;
64+ return true ;// Element inserted successfully
65+ }
66+
67+ protected TreeNode <E >createNewNode (E e ) {
68+ return new TreeNode <>(e );
69+ }
70+
71+ @ Override /** Inorder traversal from the root */
72+ public void inorder () {
73+ inorder (root );
74+ }
75+
76+ /** Inorder traversal from a subtree */
77+ protected void inorder (TreeNode <E >root ) {
78+ if (root ==null )return ;
79+ inorder (root .left );
80+ System .out .print (root .element +" " );
81+ inorder (root .right );
82+ }
83+
84+ @ Override /** Postorder traversal from the root */
85+ public void postorder () {
86+ postorder (root );
87+ }
88+
89+ /** Postorder traversal from a subtree */
90+ protected void postorder (TreeNode <E >root ) {
91+ if (root ==null )return ;
92+ postorder (root .left );
93+ postorder (root .right );
94+ System .out .print (root .element +" " );
95+ }
96+
97+ @ Override /** Preorder traversal from the root */
98+ public void preorder () {
99+ preorder (root );
100+ }
101+
102+ /** Preorder traversal from a subtree */
103+ protected void preorder (TreeNode <E >root ) {
104+ if (root ==null )return ;
105+ System .out .print (root .element +" " );
106+ preorder (root .left );
107+ preorder (root .right );
108+ }
109+
110+ /** This inner class is static, because it does not access
111+ any instance members defined in its outer class */
112+ public static class TreeNode <E extends Comparable <E >> {
113+ protected E element ;
114+ protected TreeNode <E >left ;
115+ protected TreeNode <E >right ;
116+
117+ public TreeNode (E e ) {
118+ element =e ;
119+ }
120+ }
121+
122+ @ Override /** Get the number of nodes in the tree */
123+ public int getSize () {
124+ return size ;
125+ }
126+
127+ /** Returns the root of the tree */
128+ public TreeNode <E >getRoot () {
129+ return root ;
130+ }
131+
132+ /** Return a path from the root leadting to the specified element */
133+ public java .util .ArrayList <TreeNode <E >>path (E e ) {
134+ java .util .ArrayList <TreeNode <E >>list =
135+ new java .util .ArrayList <>();
136+ TreeNode <E >current =root ;// Start from the root
137+
138+ while (current !=null ) {
139+ list .add (current );// Add the node to the list
140+ if (e .compareTo (current .element ) <0 ) {
141+ current =current .left ;
142+ }
143+ else if (e .compareTo (current .element ) >0 ) {
144+ current =current .right ;
145+ }
146+ else
147+ break ;
148+ }
149+
150+ return list ;// Return an array list of nodes
151+ }
152+
153+ @ Override /** Delete an element from the binary search tree.
154+ * Return true if the element is deleted successfully.
155+ * Return false if the element is not in the tree. */
156+ public boolean delete (E e ) {
157+ //Locate the node to be deleted and also locate its parent node
158+ TreeNode <E >parent =null ;
159+ TreeNode <E >current =root ;
160+ while (current !=null ) {
161+ if (e .compareTo (current .element ) <0 ) {
162+ parent =current ;
163+ current =current .left ;
164+ }
165+ else if (e .compareTo (current .element ) >0 ) {
166+ parent =current ;
167+ current =current .right ;
168+ }
169+ else
170+ break ;// Element is in the tree pointed at by current
171+ }
172+
173+ if (current ==null )
174+ return false ;// Element is not in the tree
175+
176+ // Case 1: current has no left child
177+ if (current .left ==null ) {
178+ // Connect the parent with the right child of the current node
179+ if (parent ==null ) {
180+ root =current .right ;
181+ }
182+ else {
183+ if (e .compareTo (parent .element ) <0 )
184+ parent .left =current .right ;
185+ else
186+ parent .right =current .right ;
187+ }
188+ }
189+ else {
190+ // Case 2: The current node has a left child.
191+ // Locate the rightmost node in the left subtree of
192+ // the current node an also its parent.
193+ TreeNode <E >parentOfRightMost =current ;
194+ TreeNode <E >rightMost =current .left ;
195+
196+ while (rightMost .right !=null ) {
197+ parentOfRightMost =rightMost ;
198+ rightMost =rightMost .right ;// Keep going to the right
199+ }
200+
201+ // Replace the element in current by the element in rightMost
202+ current .element =rightMost .element ;
203+
204+ // Eliminate rightmost node
205+ if (parentOfRightMost .right ==rightMost )
206+ parentOfRightMost .right =rightMost .left ;
207+ else
208+ // Special case: parentOfRightMost == current
209+ parentOfRightMost .left =rightMost .left ;
210+ }
211+
212+ size --;
213+ return true ;// Element deleted successfully
214+ }
215+
216+ @ Override /** Obtain an iterator. Use inorder. */
217+ public java .util .Iterator <E >iterator () {
218+ return new InorderIterator ();
219+ }
220+
221+ // Inner class InorderIterator
222+ private class InorderIterator implements java .util .Iterator <E > {
223+ // Store the elements in a list
224+ private java .util .ArrayList <E >list =
225+ new java .util .ArrayList <>();
226+ private int current =0 ;// Point to the current element in list
227+
228+ public InorderIterator () {
229+ inorder ();// Traverse binary tree and store elements in list
230+ }
231+
232+ /** Inorder traversal from the root */
233+ private void inorder () {
234+ inorder (root );
235+ }
236+
237+ /** Inorder traversal from a subtree */
238+ private void inorder (TreeNode <E >root ) {
239+ if (root ==null )return ;
240+ inorder (root .left );
241+ list .add (root .element );
242+ inorder (root .right );
243+ }
244+
245+ @ Override /** More elements for traversing? */
246+ public boolean hasNext () {
247+ if (current <list .size ())
248+ return true ;
249+
250+ return false ;
251+ }
252+
253+ @ Override /** Get the current element and move to the next */
254+ public E next () {
255+ return list .get (current ++);
256+ }
257+
258+ @ Override /** Remove the current element */
259+ public void remove () {
260+ delete (list .get (current ));// Delete the current element
261+ list .clear ();// Clear the list
262+ inorder ();// Rebuild the list
263+ }
264+ }
265+
266+ /** Remove all elements from the tree */
267+ public void clear () {
268+ root =null ;
269+ size =0 ;
270+ }
271+
272+ /** Displays the nodes in a breadth-first traversal */
273+ public void breadthFirstTraversal () {
274+ if (root ==null )return ;
275+ java .util .Queue <TreeNode <E >>queue =new java .util .LinkedList <>();
276+ queue .add (root );
277+ while (!queue .isEmpty ()){
278+ TreeNode <E >current =queue .element ();
279+ if (current .left !=null ) {
280+ queue .add (current .left );
281+ }
282+ if (current .right !=null ) {
283+ queue .add (current .right );
284+ }
285+ System .out .print (queue .remove ().element +" " );
286+ }
287+ }
288+
289+ /** Returns the height of this binary tree */
290+ public int height () {
291+ return height (root );
292+ }
293+
294+ /** Height from a subtree */
295+ protected int height (TreeNode <E >root ) {
296+ if (root ==null )return 0 ;
297+ return 1 +Math .max (height (root .left ),height (root .right ));
298+ }
299+
300+ /** Returns true if the tree is a full binary tree */
301+ public boolean isFullBST () {
302+ return size ==Math .pow (2 ,height ()) -1 ?true :false ;
303+ }
304+
305+ /** Returns the number of leaf nodes */
306+ public int getNumberOfLeaves () {
307+ return getNumberOfLeaves (root );
308+ }
309+
310+ /** Returns the number of leaf nodes */
311+ protected int getNumberOfLeaves (TreeNode <E >root ) {
312+ if (root ==null )return 0 ;
313+
314+ // If node has no children return 1
315+ // else return the sum of all the leaves
316+ return root .left ==null &&root .right ==null ?1 :
317+ getNumberOfLeaves (root .left ) +getNumberOfLeaves (root .right );
318+ }
319+
320+ /** Returns the number of nonleaf nodes */
321+ public int getNumberOfNonLeaves () {
322+ return getNumberOfNonLeaves (root );
323+ }
324+
325+ /** Returns the number of nonleaf nodes */
326+ protected int getNumberOfNonLeaves (TreeNode <E >root ) {
327+ if (root ==null )return 0 ;
328+
329+ // If node has children return 0
330+ // else return 1 plus the sum of the nonleaves
331+ return (root .left ==null &&root .right ==null ) ?0 :
332+ 1 +getNumberOfNonLeaves (root .left ) +
333+ getNumberOfNonLeaves (root .right ) ;
334+ }
335+
336+ /** Returns true if two trees are equal.
337+ Otherwise returns false (recursive) */
338+ public boolean equals (BST <E >tree ) {
339+ if (tree .size !=size )return false ;
340+ return equals (root ,tree .root );
341+ }
342+
343+ /** Equals helper */
344+ protected boolean equals (TreeNode <E >root1 ,TreeNode <E >root2 ) {
345+ if (root1 ==root2 )return true ;
346+ if (root1 ==null ||root2 ==null )return false ;
347+ return root1 .element .equals (root2 .element ) &&
348+ equals (root1 .left ,root2 .left ) &&
349+ equals (root1 .right ,root2 .right );
350+ }
351+
352+ @ Override /** Override the protected clone method
353+ defined in the Object class, and deep copy BST */
354+ public BST <E >clone ()throws CloneNotSupportedException {
355+ BST <E >cloneBST =new BST <>();
356+ clone (cloneBST ,root );
357+ return cloneBST ;
358+ }
359+
360+ /** Clone helper */
361+ protected void clone (BST <E >clone ,TreeNode <E >root ) {
362+ if (root ==null )return ;
363+ clone .insert (root .element );
364+ clone (clone ,root .left );
365+ clone (clone ,root .right );
366+ }
367+ }