《Java语言程序设计与数据结构》编程练习答案(第二十五章)(一)
英文名:Introduction to Java Programming and Data Structures, Comprehensive Version, 11th Edition
25.1
public class book {
public static void main ( String[ ] args) {
Scanner input = new Scanner ( System. in) ;
}
}
interface Tree < T> extends Collection < T> {
public boolean search ( T e) ;
public boolean insert ( T e) ;
public boolean delete ( T e) ;
public int getSize ( ) ;
public default void inorder ( ) { }
public default void postorder ( ) { }
public default void preorder ( ) { }
@Override
public default boolean isEmpty ( ) {
return size ( ) == 0 ;
}
@Override
public default boolean contains ( Object e) {
return search ( ( T) e) ;
}
@Override
public default boolean add ( T e) {
return insert ( e) ;
}
@Override
public default boolean remove ( Object e) {
return delete ( ( T) e) ;
}
@Override
public default int size ( ) {
return getSize ( ) ;
}
@Override
public default boolean containsAll ( Collection< ? > c) {
return false ;
}
@Override
public default boolean addAll ( Collection< ? extends T > c) {
return false ;
}
@Override
public default boolean removeAll ( Collection< ? > c) {
return false ;
}
@Override
public default boolean retainAll ( Collection< ? > c) {
return false ;
}
@Override
public default Object[ ] toArray ( ) {
return null;
}
@Override
public default < T> T[ ] toArray ( T[ ] array) {
return null;
}
}
class BST < T extends Comparable < T> > implements Tree < T> {
protected TreeNode< T> root;
protected int size = 0 ;
public BST ( ) { }
public BST ( T[ ] objects) {
for ( T o: objects) {
add ( o) ;
}
}
@Override
public boolean search ( T e) {
TreeNode< T> current = root;
while ( current != null) {
if ( e. compareTo ( current. element) < 0 ) {
current = current. left;
}
else if ( e. compareTo ( current. element) > 0 ) {
current = current. right;
}
else {
return true ;
}
}
return false ;
}
@Override
public boolean insert ( T e) {
if ( root == null) {
root = createNewNode ( e) ;
} else {
TreeNode< T> parent = null;
TreeNode< T> current = root;
while ( current != null) {
if ( e. compareTo ( current. element) < 0 ) {
parent = current;
current = current. left;
}
else if ( e. compareTo ( current. element) > 0 ) {
parent = current;
current = current. right;
}
else {
return false ;
}
}
if ( e. compareTo ( parent. element) < 0 ) {
parent. left = createNewNode ( e) ;
} else {
parent. right = createNewNode ( e) ;
}
}
size++ ;
return true ;
}
protected TreeNode< T> createNewNode ( T e) {
return new TreeNode < > ( e) ;
}
@Override
public void inorder ( ) {
inorder ( root) ;
}
protected void inorder ( TreeNode< T> root) {
if ( root == null) {
return ;
}
inorder ( root. left) ;
System. out. print ( root. element+ " " ) ;
inorder ( root. right) ;
}
@Override
public void postorder ( ) {
postorder ( root) ;
}
protected void postorder ( TreeNode< T> root) {
if ( root == null) {
return ;
}
postorder ( root. left) ;
postorder ( root. right) ;
System. out. print ( root. element+ " " ) ;
}
@Override
public void preorder ( ) {
preorder ( root) ;
}
protected void preorder ( TreeNode< T> root) {
if ( root == null) {
return ;
}
System. out. print ( root. element+ " " ) ;
preorder ( root. left) ;
preorder ( root. right) ;
}
@Override
public boolean delete ( T e) {
TreeNode< T> parent = null;
TreeNode< T> current = root;
while ( current != null) {
if ( e. compareTo ( current. element) < 0 ) {
parent = current;
current = current. left;
}
else if ( e. compareTo ( current. element) > 0 ) {
parent = current;
current = current. right;
}
else {
break ;
}
}
if ( current == null) {
return false ;
}
if ( current. left == null) {
if ( parent == null) {
root = current. right;
}
else {
if ( e. compareTo ( parent. element) < 0 ) {
parent. left = current. right;
}
else {
parent. right = current. right;
}
}
}
else {
TreeNode< T> parentOfRightMost = current;
TreeNode< T> rightMost = current. left;
while ( rightMost. right != null) {
parentOfRightMost = rightMost;
rightMost = rightMost. right;
}
current. element = rightMost. element;
if ( parentOfRightMost. right == rightMost) {
parentOfRightMost. right = rightMost. left;
} else {
parentOfRightMost. left = rightMost. left;
}
}
size-- ;
return true ;
}
@Override
public int getSize ( ) {
return size;
}
@Override
public Iterator< T> iterator ( ) {
return null;
}
private class InorderIterator implements Iterator < T> {
private ArrayList< T> list = new ArrayList < > ( ) ;
private int current = 0 ;
public InorderIterator ( ) {
inorder ( ) ;
}
private void inorder ( ) {
inorder ( root) ;
}
private void inorder ( TreeNode< T> root) {
if ( root == null)
return ;
inorder ( root. left) ;
list. add ( root. element) ;
inorder ( root. right) ;
}
@Override
public boolean hasNext ( ) {
return current < list. size ( ) ;
}
@Override
public T next ( ) {
return list. get ( current++ ) ;
}
}
@Override
public void clear ( ) {
root = null;
size = 0 ;
}
public static class TreeNode < T> {
protected T element;
protected TreeNode< T> left;
protected TreeNode< T> right;
public TreeNode ( T e) {
element = e;
}
}
public void breadthFirstTraversal ( ) {
ArrayList< TreeNode< T> > list = new ArrayList < > ( ) ;
if ( root == null)
return ;
list. add ( root) ;
while ( ! list. isEmpty ( ) ) {
TreeNode< T> current = list. remove ( 0 ) ;
System. out. print ( current. element+ " " ) ;
if ( current. left != null)
list. add ( current. left) ;
if ( current. right != null)
list. add ( current. right) ;
}
}
public int height ( ) {
return height ( root) ;
}
private int height ( TreeNode< T> root) {
if ( root == null)
return 0 ;
else {
if ( height ( root. left) > height ( root. right) ) {
return height ( root. left) + 1 ;
}
else {
return height ( root. right) + 1 ;
}
}
}
}
25.2
public class book {
public static void main ( String[ ] args) {
Scanner input = new Scanner ( System. in) ;
}
}
interface Tree < T> extends Collection < T> {
public boolean search ( T e) ;
public boolean insert ( T e) ;
public boolean delete ( T e) ;
public int getSize ( ) ;
public default void inorder ( ) { }
public default void postorder ( ) { }
public default void preorder ( ) { }
@Override
public default boolean isEmpty ( ) {
return size ( ) == 0 ;
}
@Override
public default boolean contains ( Object e) {
return search ( ( T) e) ;
}
@Override
public default boolean add ( T e) {
return insert ( e) ;
}
@Override
public default boolean remove ( Object e) {
return delete ( ( T) e) ;
}
@Override
public default int size ( ) {
return getSize ( ) ;
}
@Override
public default boolean containsAll ( Collection< ? > c) {
return false ;
}
@Override
public default boolean addAll ( Collection< ? extends T > c) {
return false ;
}
@Override
public default boolean removeAll ( Collection< ? > c) {
return false ;
}
@Override
public default boolean retainAll ( Collection< ? > c) {
return false ;
}
@Override
public default Object[ ] toArray ( ) {
return null;
}
@Override
public default < T> T[ ] toArray ( T[ ] array) {
return null;
}
}
class BST < T extends Comparable < T> > implements Tree < T> {
protected TreeNode< T> root;
protected int size = 0 ;
public BST ( ) { }
public BST ( T[ ] objects) {
for ( T o: objects) {
add ( o) ;
}
}
@Override
public boolean search ( T e) {
TreeNode< T> current = root;
while ( current != null) {
if ( e. compareTo ( current. element) < 0 ) {
current = current. left;
}
else if ( e. compareTo ( current. element) > 0 ) {
current = current. right;
}
else {
return true ;
}
}
return false ;
}
@Override
public boolean insert ( T e) {
if ( root == null) {
root = createNewNode ( e) ;
} else {
TreeNode< T> parent = null;
TreeNode< T> current = root;
while ( current != null) {
if ( e. compareTo ( current. element) < 0 ) {
parent = current;
current = current. left;
}
else if ( e. compareTo ( current. element) > 0 ) {
parent = current;
current = current. right;
}
else {
return false ;
}
}
if ( e. compareTo ( parent. element) < 0 ) {
parent. left = createNewNode ( e) ;
} else {
parent. right = createNewNode ( e) ;
}
}
size++ ;
return true ;
}
protected TreeNode< T> createNewNode ( T e) {
return new TreeNode < > ( e) ;
}
@Override
public void inorder ( ) {
inorder ( root) ;
}
protected void inorder ( TreeNode< T> root) {
if ( root == null) {
return ;
}
inorder ( root. left) ;
System. out. print ( root. element+ " " ) ;
inorder ( root. right) ;
}
@Override
public void postorder ( ) {
postorder ( root) ;
}
protected void postorder ( TreeNode< T> root) {
if ( root == null) {
return ;
}
postorder ( root. left) ;
postorder ( root. right) ;
System. out. print ( root. element+ " " ) ;
}
@Override
public void preorder ( ) {
preorder ( root) ;
}
protected void preorder ( TreeNode< T> root) {
if ( root == null) {
return ;
}
System. out. print ( root. element+ " " ) ;
preorder ( root. left) ;
preorder ( root. right) ;
}
@Override
public boolean delete ( T e) {
TreeNode< T> parent = null;
TreeNode< T> current = root;
while ( current != null) {
if ( e. compareTo ( current. element) < 0 ) {
parent = current;
current = current. left;
}
else if ( e. compareTo ( current. element) > 0 ) {
parent = current;
current = current. right;
}
else {
break ;
}
}
if ( current == null) {
return false ;
}
if ( current. left == null) {
if ( parent == null) {
root = current. right;
}
else {
if ( e. compareTo ( parent. element) < 0 ) {
parent. left = current. right;
}
else {
parent. right = current. right;
}
}
}
else {
TreeNode< T> parentOfRightMost = current;
TreeNode< T> rightMost = current. left;
while ( rightMost. right != null) {
parentOfRightMost = rightMost;
rightMost = rightMost. right;
}
current. element = rightMost. element;
if ( parentOfRightMost. right == rightMost) {
parentOfRightMost. right = rightMost. left;
} else {
parentOfRightMost. left = rightMost. left;
}
}
size-- ;
return true ;
}
@Override
public int getSize ( ) {
return size;
}
@Override
public Iterator< T> iterator ( ) {
return null;
}
private class InorderIterator implements Iterator < T> {
private ArrayList< T> list = new ArrayList < > ( ) ;
private int current = 0 ;
public InorderIterator ( ) {
inorder ( ) ;
}
private void inorder ( ) {
inorder ( root) ;
}
private void inorder ( TreeNode< T> root) {
if ( root == null)
return ;
inorder ( root. left) ;
list. add ( root. element) ;
inorder ( root. right) ;
}
@Override
public boolean hasNext ( ) {
return current < list. size ( ) ;
}
@Override
public T next ( ) {
return list. get ( current++ ) ;
}
}
@Override
public void clear ( ) {
root = null;
size = 0 ;
}
public static class TreeNode < T> {
protected T element;
protected TreeNode< T> left;
protected TreeNode< T> right;
public TreeNode ( T e) {
element = e;
}
}
public void breadthFirstTraversal ( ) {
ArrayList< TreeNode< T> > list = new ArrayList < > ( ) ;
if ( root == null)
return ;
list. add ( root) ;
while ( ! list. isEmpty ( ) ) {
TreeNode< T> current = list. remove ( 0 ) ;
System. out. print ( current. element+ " " ) ;
if ( current. left != null)
list. add ( current. left) ;
if ( current. right != null)
list. add ( current. right) ;
}
}
public int height ( ) {
return height ( root) ;
}
private int height ( TreeNode< T> root) {
if ( root == null)
return 0 ;
else {
if ( height ( root. left) > height ( root. right) ) {
return height ( root. left) + 1 ;
}
else {
return height ( root. right) + 1 ;
}
}
}
public boolean isPerfectBST ( ) {
return size == ( int ) ( Math. pow ( 2 , height ( ) ) - 1 ) ;
}
}
25.3
public class book {
public static void main ( String[ ] args) {
Scanner input = new Scanner ( System. in) ;
BST< Integer> test = new BST < > ( ) ;
System. out. println ( "Enter 10 integers:" ) ;
for ( int i= 0 ; i< 10 ; i++ ) {
int tmp = input. nextInt ( ) ;
test. add ( tmp) ;
}
test. inorderUsingStack ( ) ;
}
}
public void inorderUsingStack ( ) {
inorderUsingStack ( root) ;
}
private void inorderUsingStack ( TreeNode< T> root) {
if ( root == null)
return ;
Stack< TreeNode< T> > stack = new Stack < > ( ) ;
TreeNode< T> current = root;
while ( current != null || ! stack. isEmpty ( ) ) {
if ( current != null) {
stack. push ( current) ;
current = current. left;
}
else {
current = stack. pop ( ) ;
System. out. print ( current. element+ " " ) ;
current = current. right;
}
}
}
25.4
public class book {
public static void main ( String[ ] args) {
Scanner input = new Scanner ( System. in) ;
BST< Integer> test = new BST < > ( ) ;
System. out. println ( "Enter 10 integers:" ) ;
for ( int i= 0 ; i< 10 ; i++ ) {
int tmp = input. nextInt ( ) ;
test. add ( tmp) ;
}
test. preorderUsingStack ( ) ;
}
}
public void preorderUsingStack ( ) {
preorderUsingStack ( root) ;
}
private void preorderUsingStack ( TreeNode< T> root) {
if ( root == null)
return ;
Stack< TreeNode< T> > stack = new Stack < > ( ) ;
stack. push ( root) ;
while ( ! stack. isEmpty ( ) ) {
TreeNode< T> current = stack. pop ( ) ;
System. out. print ( current. element+ " " ) ;
if ( current. right != null) {
stack. push ( current. right) ;
}
if ( current. left != null) {
stack. push ( current. left) ;
}
}
}
25.5
public class book {
public static void main ( String[ ] args) {
Scanner input = new Scanner ( System. in) ;
BST< Integer> test = new BST < > ( ) ;
System. out. println ( "Enter 10 integers:" ) ;
for ( int i= 0 ; i< 10 ; i++ ) {
int tmp = input. nextInt ( ) ;
test. add ( tmp) ;
}
test. postorderUsingStack ( ) ;
}
}
public void postorderUsingStack ( ) {
postorderUsingStack ( root) ;
}
private void postorderUsingStack ( TreeNode< T> root) {
if ( root == null)
return ;
Stack< TreeNode< T> > stack = new Stack < > ( ) ;
TreeNode< T> current = root;
TreeNode< T> pre = null;
while ( current != null) {
stack. push ( current) ;
current = current. left;
}
while ( ! stack. empty ( ) ) {
current = stack. pop ( ) ;
if ( current. right != null && current. right != pre) {
stack. push ( current) ;
current = current. right;
while ( current != null) {
stack. push ( current) ;
current = current. left;
}
}
else {
System. out. print ( current. element+ " " ) ;
pre = current;
}
}
}
25.6
public int getNumberOfLeaves ( ) {
return getNumberOfLeaves ( root) ;
}
private int getNumberOfLeaves ( TreeNode< T> root) {
if ( root. left == null && root. right == null) {
return 1 ;
} else if ( root. left == null) {
return getNumberOfLeaves ( root. right) ;
} else if ( root. right == null) {
return getNumberOfLeaves ( root. left) ;
} else {
return getNumberOfLeaves ( root. left) + getNumberOfLeaves ( root. right) ;
}
}