19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

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

}<br />

// Adjust child links for nonleaf node<br />

if (u.child.size() > 0)<br />

leftNode.child.add(u.child.remove(0));<br />

/** Perform a fusion with a right sibling */<br />

private void rightSiblingFusion(int k, Tree24Node rightNode,<br />

Tree24Node u, Tree24Node parentOfu) {<br />

// Transfer an element from the parent <strong>to</strong> the right sibling<br />

rightNode.elements.add(0, parentOfu.elements.remove(k));<br />

}<br />

// Remove the link <strong>to</strong> the empty node<br />

parentOfu.child.remove(k);<br />

// Adjust child links for nonleaf node<br />

if (u.child.size() > 0)<br />

rightNode.child.add(0, u.child.remove(0));<br />

/** Get the number of nodes in the tree */<br />

public int getSize() {<br />

return size;<br />

}<br />

/** Preorder traversal from the root */<br />

public void preorder() {<br />

preorder(root);<br />

}<br />

/** Preorder traversal from a subtree */<br />

private void preorder(Tree24Node root) {<br />

if (root == null)return;<br />

for (int i = 0; i < root.elements.size(); i++)<br />

System.out.print(root.elements.get(i) + " ");<br />

}<br />

for (int i = 0; i < root.child.size(); i++)<br />

preorder(root.child.get(i));<br />

/** Inorder traversal from the root*/<br />

public void inorder() {<br />

// Left as exercise<br />

}<br />

/** Pos<strong>to</strong>rder traversal from the root */<br />

public void pos<strong>to</strong>rder() {<br />

// Left as exercise<br />

}<br />

/** Return true if the tree is empty */<br />

public boolean isEmpty() {<br />

return root == null;<br />

}<br />

/** Return an itera<strong>to</strong>r <strong>to</strong> traverse elements in the tree */<br />

public java.util.Itera<strong>to</strong>r itera<strong>to</strong>r() {<br />

21

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

Saved successfully!

Ooh no, something went wrong!