没有合适的资源?快使用搜索试试~ 我知道了~
实验10练习1题目在忽略字符串中的非英语字母字符并把大小写英语字母字符看成等价后,正读和反读完全一样的字符串称为英语回文。输出该二叉树的先序、中序、后序和层序序
资源详情
资源评论
资源推荐
实验10
练习1
题目
在忽略字符串中的非英语字母字符并把大小写英语字母字符看成等价后,正读和反读完全一样的字符串
称为英语回文。如: Rise to vote, sir. No lemons no melon. Was it a car or a cat I saw?
判断输入的字符串是否为英语回文。要求使用栈和队列。
解析
仅仅是栈和队列的简单应用,不做过多解释。
代码
Exercise_1.java
import java.util.Scanner;
public class Exercise_1 {
public static void main(String[] args) throws Exception {
// read data
Scanner in = new Scanner(System.in);
String s = in.nextLine();
in.close();
// initialize
Queue<Character> queue = new Queue<Character>();
Stack<Character> stack = new Stack<Character>();
// add elements of string to queue / stack
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (Character.isLetter(c)) {
queue.enqueue(Character.toLowerCase(c));
stack.push(Character.toLowerCase(c));
}
}
// take out elements of queue/stack one by one, and compare their
differences
boolean result = true;
while (stack.peek() != null) {
if (queue.dequeue() != stack.pop()) {
result = false;
break;
}
}
// print result
System.out.println(result);
}
}
输入
输出
练习2
题目
要求用二叉链表实现二叉树。根据与一棵二叉树对应的扩展二叉树的层序序列构造该二叉树。输出该二
叉树的先序、中序、后序和层序序列。交换该二叉树的所有结点的左、右子树。输出新二叉树的先序、
中序、后序和层序序列。
解析
先序、中序、后序遍历原理很简单,不做解释。
层序遍历时创建一个队列q。初始时根节点入队,然后不断出队并使出队结点的左右孩子重新入队(如果
孩子不为null),直到队列为空,结点的出队顺序即为层序遍历的结果。
交换左右子树和层序遍历的原理类似,只是将记录结点的出队顺序改成交换左右孩子。
代码
BiTree.java
Rise to vote, sir.
true
// 使用Java泛型实现的二叉树,利用扩展二叉树的层序序列构建。
public class BiTree<Item> {
private Node root;
// Node类型
private class Node {
Item data;
Node left;
Node right;
public Node(Item data, Node left, Node right) {
this.data = data;
this.left = left;
this.right = right;
}
}
// 初始化
public BiTree() {
this.root = null;
}
public BiTree(Item data) {
this.root = new Node(data, null, null);
}
// 使用扩展二叉树构造二叉树
public BiTree(Item[] arr) {
if (arr.length == 0) {
this.root = null;
} else {
this.root = new Node(arr[0], null, null);
Queue<Node> queue = new Queue<Node>();
queue.enqueue(this.root);
int i = 1;
while (i < arr.length) {
Node p = queue.dequeue();
if (arr[i] != null) {
p.left = new Node(arr[i], null, null);
queue.enqueue(p.left);
}
if (arr[i+1] != null) {
p.right = new Node(arr[i+1], null, null);
queue.enqueue(p.right);
}
i += 2;
}
}
}
// 交换左右子树
public void exchange() {
Queue<Node> queue = new Queue<Node>();
queue.enqueue(this.root);
while (!queue.isEmpty()) {
Node p = queue.dequeue();
Node tmp = p.left;
p.left = p.right;
p.right = tmp;
if (p.left != null) {
queue.enqueue(p.left);
}
if (p.right != null) {
queue.enqueue(p.right);
}
}
}
// 先序遍历
public Item[] preOrder() throws Exception {
return preOrder(this.root);
}
public Item[] preOrder(Node T) throws Exception {
List<Item> result = new List<Item>();
if (T != null) {
result.append(T.data);
result.extend(preOrder(T.left));
result.extend(preOrder(T.right));
}
return result.toArray();
}
剩余12页未读,继续阅读
韩金虎
- 粉丝: 28
- 资源: 285
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0