23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

addition to the B<strong>in</strong>ary Tree <strong>in</strong>terface methods, L<strong>in</strong>kedB<strong>in</strong>aryTree has<br />

various other methods, <strong>in</strong>clud<strong>in</strong>g accessor method sibl<strong>in</strong>g(v), which returns<br />

the sibl<strong>in</strong>g of a node v, <strong>and</strong> the follow<strong>in</strong>g update methods:<br />

addRoot(e):<br />

Create <strong>and</strong> return a new node r stor<strong>in</strong>g element e <strong>and</strong> make r the root of<br />

the tree; an error occurs if the tree is not empty.<br />

<strong>in</strong>sertLeft(v,e):<br />

Create <strong>and</strong> return a new node w stor<strong>in</strong>g element e, add w as the the left<br />

child of v <strong>and</strong> return w; an error occurs if v already has a left child.<br />

<strong>in</strong>sertRight(v,e):<br />

Create <strong>and</strong> return a new node z stor<strong>in</strong>g element e, add z as the the right<br />

child of v <strong>and</strong> return z; an error occurs if v already has a right child.<br />

remove(v):<br />

Remove node v, replace it with its child, if any, <strong>and</strong> return the element<br />

stored at v; an error occurs if v has two children.<br />

attach(v,T 1 ,T 2 ):<br />

Attach T 1 <strong>and</strong> T 2 , respectively, as the left <strong>and</strong> right subtrees of the<br />

external node v; an error condition occurs if v is not external.<br />

Class L<strong>in</strong>kedB<strong>in</strong>aryTree has a constructor with no arguments that returns an<br />

empty b<strong>in</strong>ary tree. Start<strong>in</strong>g from this empty tree, we can build any b<strong>in</strong>ary tree by<br />

creat<strong>in</strong>g the first node with method addRoot <strong>and</strong> repeatedly apply<strong>in</strong>g the<br />

<strong>in</strong>sertLeft <strong>and</strong> <strong>in</strong>sertRight methods <strong>and</strong>/or the attach method. Likewise,<br />

we can dismantle any b<strong>in</strong>ary tree T us<strong>in</strong>g the remove operation, ultimately<br />

reduc<strong>in</strong>g such a tree T to an empty b<strong>in</strong>ary tree.<br />

When a position v is passed as an argument to one of the methods of class<br />

L<strong>in</strong>kedB<strong>in</strong>aryTree, its validity is checked by call<strong>in</strong>g an auxiliary helper<br />

method, checkPosition(v). A list of the nodes visited <strong>in</strong> a preorder traversal<br />

of the tree is constructed by recursive method preorderPositions. Error<br />

conditions are <strong>in</strong>dicated by throw<strong>in</strong>g exceptions Invalid Position<br />

Exception, BoundaryViolation Exception,<br />

EmptyTreeException, <strong>and</strong> NonEmptyTreeException.<br />

Code Fragment 7.16: Portions of the L<strong>in</strong>ked<br />

B<strong>in</strong>ary Tree class, which implements the B<strong>in</strong>ary<br />

Tree <strong>in</strong>terface. (Cont<strong>in</strong>ues <strong>in</strong> Code Fragment 7.17.)<br />

405

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!