《Java语言程序设计与数据结构》编程练习答案(第二十五章)(二)

本文主要是介绍《Java语言程序设计与数据结构》编程练习答案(第二十五章)(二),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

《Java语言程序设计与数据结构》编程练习答案(第二十五章)(二)

英文名:Introduction to Java Programming and Data Structures, Comprehensive Version, 11th Edition

25.7

//exercise 25.7public int getNumberOfNonLeaves(){return getNumberOfNonLeaves(root);}private int getNumberOfNonLeaves(TreeNode<T> root){if(root == null)return 0;return getNumberOfNonLeaves(root.right) + getNumberOfNonLeaves(root.left) + 1;}

25.8

//exercise 25.8private class DoubleIterator implements ListIterator<T>{private ArrayList<T> list = new ArrayList<>();private int current = 0;public DoubleIterator(){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);}@Overridepublic boolean hasNext() {return current < list.size();}@Overridepublic T next() {return list.get(current++);}@Overridepublic boolean hasPrevious() {return current >= 0;}@Overridepublic T previous() {return list.get(current--);}@Overridepublic int nextIndex() {return current+1;}@Overridepublic int previousIndex() {return current-1;}@Overridepublic void remove() {BST.this.remove(list.get(current));}@Overridepublic void set(T t) {BST.this.delete(list.get(current));BST.this.insert(t);}@Overridepublic void add(T t) {BST.this.add(t);}}

25.9

//exercise 25.9@Overridepublic BST<T> clone(){BST<T> cloneTree = new BST<>();cloneTree.root = clone(this.root);cloneTree.size = this.size;return cloneTree;}private TreeNode<T> clone(TreeNode<T> root){if(root == null){return null;}TreeNode<T> cloneRoot = new TreeNode<>(root.element);cloneRoot.left = clone(root.left);cloneRoot.right = clone(root.right);return cloneRoot;}@Overridepublic boolean equals(Object o){BST<T> treeToBeCompared = (BST<T>) o;if(this.size == treeToBeCompared.size){for(T t : treeToBeCompared){if(!this.search(t)){return false;}}return true;}else{return false;}}

25.10

//exercise 25.10Iterator<T> preorderIterator(){return new preorderIerator();}private class preorderIerator implements Iterator<T>{private ArrayList<T> list = new ArrayList<>();private int current = 0;public preorderIerator(){preorder();}private void preorder(){preorder(root);}private void preorder(TreeNode<T> root){if(root == null)return;list.add(root.element);preorder(root.left);preorder(root.right);}@Overridepublic boolean hasNext() {return current < list.size();}@Overridepublic T next() {return list.get(current++);}}

25.11

🐎

25.12

public class book {public static void main(String[] args) {Scanner input = new Scanner(System.in);BST<Integer> test = new BST<>();for(int i=0;i<20;i++){test.add((int)(100*Math.random()));}BST<Integer> cloneTest = test.clone();for(int i: test){System.out.print(i+" ");}System.out.print("\n");for(int i: cloneTest){System.out.print(i+" ");}System.out.println(test.equals(cloneTest));}
}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(){}@Overridepublic default boolean isEmpty(){return size() == 0;}@Overridepublic default boolean contains(Object e){return search((T) e);}@Overridepublic default boolean add(T e){return insert(e);}@Overridepublic default boolean remove(Object e){return delete((T) e);}@Overridepublic default int size(){return getSize();}@Overridepublic default boolean containsAll(Collection<?> c){return false;}@Overridepublic default boolean addAll(Collection<? extends T> c){return false;}@Overridepublic default boolean removeAll(Collection<?> c){return false;}@Overridepublic default boolean retainAll(Collection<?> c){return false;}@Overridepublic default Object[] toArray(){return null;}@Overridepublic 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);}}@Overridepublic 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;}@Overridepublic 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);}@Overridepublic 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);}@Overridepublic 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+" ");}@Overridepublic 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);}@Overridepublic 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;}@Overridepublic int getSize() {return size;}@Overridepublic Iterator<T> iterator() {return new InorderIterator();}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);}@Overridepublic boolean hasNext(){return current < list.size();}@Overridepublic T next(){return list.get(current++);}}//exercise 25.10Iterator<T> preorderIterator(){return new preorderIerator();}private class preorderIerator implements Iterator<T>{private ArrayList<T> list = new ArrayList<>();private int current = 0;public preorderIerator(){preorder();}private void preorder(){preorder(root);}private void preorder(TreeNode<T> root){if(root == null)return;list.add(root.element);preorder(root.left);preorder(root.right);}@Overridepublic boolean hasNext() {return current < list.size();}@Overridepublic T next() {return list.get(current++);}}//exercise 25.8private class DoubleIterator implements ListIterator<T>{private ArrayList<T> list = new ArrayList<>();private int current = 0;public DoubleIterator(){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);}@Overridepublic boolean hasNext() {return current < list.size();}@Overridepublic T next() {return list.get(current++);}@Overridepublic boolean hasPrevious() {return current >= 0;}@Overridepublic T previous() {return list.get(current--);}@Overridepublic int nextIndex() {return current+1;}@Overridepublic int previousIndex() {return current-1;}@Overridepublic void remove() {BST.this.remove(list.get(current));}@Overridepublic void set(T t) {BST.this.delete(list.get(current));BST.this.insert(t);}@Overridepublic void add(T t) {BST.this.add(t);}}@Overridepublic 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;}}//exercise 25.1public 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;}}}//exercise 25.2public boolean isPerfectBST(){return size == (int)(Math.pow(2,height())-1);}//exercise 25.3public 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;}}}//exercise 25.4public 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);}}}//exercise 25.5public 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;}}}//exercise 25.6public 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);}}//exercise 25.7public int getNumberOfNonLeaves(){return getNumberOfNonLeaves(root);}private int getNumberOfNonLeaves(TreeNode<T> root){if(root == null)return 0;return getNumberOfNonLeaves(root.right) + getNumberOfNonLeaves(root.left) + 1;}//exercise 25.9@Overridepublic BST<T> clone(){BST<T> cloneTree = new BST<>();cloneTree.root = clone(this.root);cloneTree.size = this.size;return cloneTree;}private TreeNode<T> clone(TreeNode<T> root){if(root == null){return null;}TreeNode<T> cloneRoot = new TreeNode<>(root.element);cloneRoot.left = clone(root.left);cloneRoot.right = clone(root.right);return cloneRoot;}@Overridepublic boolean equals(Object o){BST<T> treeToBeCompared = (BST<T>) o;if(this.size == treeToBeCompared.size){for(T t : treeToBeCompared){if(!this.search(t)){return false;}}return true;}else{return false;}}
}

25.13

//exercise 25.13private ArrayList<T> inorderList = new ArrayList<>();private ArrayList<T> preorderList = new ArrayList<>();private ArrayList<T> postorderList = new ArrayList<>();public List<T> inorderList(){addInorderList(root);return inorderList;}private void addInorderList(TreeNode<T> root){if(root == null)return;addInorderList(root.left);inorderList.add(root.element);addInorderList(root.right);}public List<T> preorderList(){addPreorderList(root);return preorderList;}private void addPreorderList(TreeNode<T> root){if(root == null)return;preorderList.add(root.element);addPreorderList(root.left);addPreorderList(root.right);}public List<T> postorderList(){addPostorderList(root);return postorderList;}private void addPostorderList(TreeNode<T> root){if(root == null)return;addPostorderList(root.left);addPostorderList(root.right);postorderList.add(root.element);}

25.14

🐎

25.15

public class book {public static void main(String[] args) {Scanner input = new Scanner(System.in);ArrayList<Integer> integers = new ArrayList<>();BST<Integer> test = new BST<>();System.out.println("Enter 10 integers:");for(int i=0;i<10;i++){int tmp = input.nextInt();integers.add(tmp);test.add(tmp);}test.delete(integers.get(0));integers.remove(0);for(int i: integers){if(test.isLeaf(i)){System.out.println(Arrays.toString(test.getPath(i).toArray()));}}}
}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(){}@Overridepublic default boolean isEmpty(){return size() == 0;}@Overridepublic default boolean contains(Object e){return search((T) e);}@Overridepublic default boolean add(T e){return insert(e);}@Overridepublic default boolean remove(Object e){return delete((T) e);}@Overridepublic default int size(){return getSize();}@Overridepublic default boolean containsAll(Collection<?> c){return false;}@Overridepublic default boolean addAll(Collection<? extends T> c){return false;}@Overridepublic default boolean removeAll(Collection<?> c){return false;}@Overridepublic default boolean retainAll(Collection<?> c){return false;}@Overridepublic default Object[] toArray(){return null;}@Overridepublic 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(){}@Overridepublic boolean search(T e) {return false;}@Overridepublic 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;}elsereturn false;if(e.compareTo(parent.element) < 0){parent.left = createNewNode(e);(parent.left).parent = parent;}else{parent.right = createNewNode(e);(parent.right).parent = parent;}}size++;return true;}@Overridepublic 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;}elsebreak;}if(current == null)return false;if(current.left == null){if(parent == null){root = current.right;root.parent = null;}else{if(e.compareTo(parent.element) < 0){parent.left = current.right;if(parent.left != null) {(parent.left).parent = parent;}}else{parent.right = current.right;if(parent.right != null) {(parent.right).parent = parent;}}}}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;if(parentOfRightMost.right != null) {(parentOfRightMost.right).parent = parentOfRightMost;}}else{parentOfRightMost.left = rightMost.left;if(parentOfRightMost.left != null) {(parentOfRightMost.left).parent = parentOfRightMost;}}}size--;return true;}private TreeNode<T> getNode(T element){return findNode(element, root);}private TreeNode<T> findNode(T element, TreeNode<T> root){if(root == null)return null;if(root.element.equals(element)){return root;}if(findNode(element, root.left) != null){return findNode(element, root.left);}if(findNode(element, root.right) != null){return findNode(element, root.right);}return null;}public boolean isLeaf(T element){TreeNode<T> current = getNode(element);if(current == null)return false;return current.left == null && current.right == null;}public ArrayList<T> getPath(T e){ArrayList<T> ret = new ArrayList<>();TreeNode<T> current = getNode(e);while (!current.element.equals(root.element)){ret.add(current.element);current = current.parent;}ret.add(root.element);return ret;}@Overridepublic int getSize() {return size;}@Overridepublic Iterator<T> iterator() {return null;}@Overridepublic void clear() {}protected TreeNode<T> createNewNode(T e){return new TreeNode<>(e);}public static class TreeNode<T>{protected T element;protected TreeNode<T> left;protected TreeNode<T> right;protected TreeNode<T> parent;public TreeNode(T e){this.element = e;}}
}

第二十五章 完

这篇关于《Java语言程序设计与数据结构》编程练习答案(第二十五章)(二)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/766337

相关文章

springboot健康检查监控全过程

《springboot健康检查监控全过程》文章介绍了SpringBoot如何使用Actuator和Micrometer进行健康检查和监控,通过配置和自定义健康指示器,开发者可以实时监控应用组件的状态,... 目录1. 引言重要性2. 配置Spring Boot ActuatorSpring Boot Act

使用Java解析JSON数据并提取特定字段的实现步骤(以提取mailNo为例)

《使用Java解析JSON数据并提取特定字段的实现步骤(以提取mailNo为例)》在现代软件开发中,处理JSON数据是一项非常常见的任务,无论是从API接口获取数据,还是将数据存储为JSON格式,解析... 目录1. 背景介绍1.1 jsON简介1.2 实际案例2. 准备工作2.1 环境搭建2.1.1 添加

Java实现任务管理器性能网络监控数据的方法详解

《Java实现任务管理器性能网络监控数据的方法详解》在现代操作系统中,任务管理器是一个非常重要的工具,用于监控和管理计算机的运行状态,包括CPU使用率、内存占用等,对于开发者和系统管理员来说,了解这些... 目录引言一、背景知识二、准备工作1. Maven依赖2. Gradle依赖三、代码实现四、代码详解五

java如何分布式锁实现和选型

《java如何分布式锁实现和选型》文章介绍了分布式锁的重要性以及在分布式系统中常见的问题和需求,它详细阐述了如何使用分布式锁来确保数据的一致性和系统的高可用性,文章还提供了基于数据库、Redis和Zo... 目录引言:分布式锁的重要性与分布式系统中的常见问题和需求分布式锁的重要性分布式系统中常见的问题和需求

SpringBoot基于MyBatis-Plus实现Lambda Query查询的示例代码

《SpringBoot基于MyBatis-Plus实现LambdaQuery查询的示例代码》MyBatis-Plus是MyBatis的增强工具,简化了数据库操作,并提高了开发效率,它提供了多种查询方... 目录引言基础环境配置依赖配置(Maven)application.yml 配置表结构设计demo_st

在Ubuntu上部署SpringBoot应用的操作步骤

《在Ubuntu上部署SpringBoot应用的操作步骤》随着云计算和容器化技术的普及,Linux服务器已成为部署Web应用程序的主流平台之一,Java作为一种跨平台的编程语言,具有广泛的应用场景,本... 目录一、部署准备二、安装 Java 环境1. 安装 JDK2. 验证 Java 安装三、安装 mys

Springboot的ThreadPoolTaskScheduler线程池轻松搞定15分钟不操作自动取消订单

《Springboot的ThreadPoolTaskScheduler线程池轻松搞定15分钟不操作自动取消订单》:本文主要介绍Springboot的ThreadPoolTaskScheduler线... 目录ThreadPoolTaskScheduler线程池实现15分钟不操作自动取消订单概要1,创建订单后

JAVA中整型数组、字符串数组、整型数和字符串 的创建与转换的方法

《JAVA中整型数组、字符串数组、整型数和字符串的创建与转换的方法》本文介绍了Java中字符串、字符数组和整型数组的创建方法,以及它们之间的转换方法,还详细讲解了字符串中的一些常用方法,如index... 目录一、字符串、字符数组和整型数组的创建1、字符串的创建方法1.1 通过引用字符数组来创建字符串1.2

SpringCloud集成AlloyDB的示例代码

《SpringCloud集成AlloyDB的示例代码》AlloyDB是GoogleCloud提供的一种高度可扩展、强性能的关系型数据库服务,它兼容PostgreSQL,并提供了更快的查询性能... 目录1.AlloyDBjavascript是什么?AlloyDB 的工作原理2.搭建测试环境3.代码工程1.

Java调用Python代码的几种方法小结

《Java调用Python代码的几种方法小结》Python语言有丰富的系统管理、数据处理、统计类软件包,因此从java应用中调用Python代码的需求很常见、实用,本文介绍几种方法从java调用Pyt... 目录引言Java core使用ProcessBuilder使用Java脚本引擎总结引言python