# 学号20192308 2020-2021-1 《数据结构与面向对象程序设计》实验8报告

## 2. 实验过程及结果

import java.util.ArrayList;

public class BTNode<T>
{
protected T element;
protected BTNode<T> left, right;

public BTNode (T element)
{
this.element = element;
left = right = null;
}

public T getElement()
{
return element;
}

public void setElement (T element)
{
this.element = element;
}

public BTNode<T> getLeft()
{
return left;
}

public void setLeft (BTNode<T> left)
{
this.left = left;
}

public BTNode<T> getRight()
{
return right;
}

public void setRight (BTNode<T> right)
{
this.right = right;
}

public BTNode<T> find (T target) {
BTNode<T> result = null;

if (element.equals(target))
result = this;
else
{
if (left != null)
result = left.find(target);
if (result == null && right != null)
result = right.find(target);
}

return result;
}

public int count() {
int result = 1;

if (left != null)
result += left.count();

if (right != null)
result += right.count();

return result;
}

public void inorder (ArrayList<T> iter) {
if (left != null)
left.inorder (iter);

if (right != null)
right.inorder (iter);
}

public void preorder (ArrayList<T> iter) {

if (left != null)
left.inorder (iter);

if (right != null)
right.inorder (iter);
}

public void postorder (ArrayList<T> iter) {
if (left != null)
left.inorder (iter);

if (right != null)
right.inorder (iter);

}

public char print() {
return (char) element;
}

import java.util.ArrayList;

public interface BinaryTree<T>
{
public T getRootElement();
public BinaryTree<T> getLeft();
public BinaryTree<T> getRight();
public boolean contains(T target);
public T find(T target);
public boolean isEmpty();
public int size();
public String toString();
public ArrayList<T> preorder();
public ArrayList<T> inorder();
public ArrayList<T> postorder();
public ArrayList<T> levelorder();
}

mport javafoundations.*;
import javafoundations.exceptions.*;

import java.util.ArrayList;
import java.util.Arrays;

{
protected BTNode<T> root;

{
root = null;
}

{
root = new BTNode<T>(element);
}

root = new BTNode<T>(element);
root.setLeft(left.root);
root.setRight(right.root);
}

public T getRootElement() {
if (root == null)
throw new EmptyCollectionException("Get root operation "
+ "failed. The tree is empty.");

return root.getElement();
}

if (root == null)
throw new EmptyCollectionException("Get left operation "
+ "failed. The tree is empty.");

result.root = root.getLeft();

return result;
}

public T find (T target) {
BTNode<T> node = null;

if (root != null)
node = root.find(target);

if (node == null)
throw new ElementNotFoundException("Find operation failed. "
+ "No such element in tree.");

return node.getElement();
}

public int size() {
int result = 0;

if (root != null)
result = root.count();

return result;
}

public ArrayList<T> inorder() {
ArrayList<T> iter = new ArrayList<T>();

if (root != null)
root.inorder (iter);

return iter;
}

public ArrayList<T> levelorder() {
ArrayList<T> iter = new ArrayList<T>();

if (root != null)
{
queue.enqueue(root);
while (!queue.isEmpty())
{
BTNode<T> current = queue.dequeue();

if (current.getLeft() != null)
queue.enqueue(current.getLeft());
if (current.getRight() != null)
queue.enqueue(current.getRight());
}
}

return iter;
}

if (root == null)
throw new EmptyCollectionException("Get left operation "
+ "failed. The tree is empty.");

result.root = root.getRight();

return result; }

public boolean contains (T target) {
if(root.find(target)==null)
return false;
else
return true;
}

public boolean isEmpty() {
if(root!=null)
return false;
else
return true;
}

public String toString() {
return super.toString();
}

public ArrayList<T> preorder() {
ArrayList<T> iter = new ArrayList<T>();

if (root != null)
root.preorder (iter);

return iter;
}

public ArrayList<T> postorder() {
ArrayList<T> iter = new ArrayList<T>();

if (root != null)
root.postorder (iter);

return iter;
}

public BTNode construct(char[] pre, char[] in){
if (pre.length == 0 || in.length == 0) {
return null;
}
BTNode<Character> tree = new BTNode<Character>(pre[0]);
int index = search(0, in.length, in, tree.getElement());
tree.setLeft(construct(Arrays.copyOfRange(pre, 1, index + 1), Arrays.copyOfRange(in, 0, index)));
tree.setRight(construct(Arrays.copyOfRange(pre, index + 1, pre.length),
Arrays.copyOfRange(in, index + 1, in.length)));
return tree;
}

public int search(int start, int end, char[] inOrders, char data) {
for (int i = start; i < end; i++) {
if (data == inOrders[i]) {
return i;
}
}
return -1;
}

public void preOrder(BTNode<T> Tree)
{
System.out.print(Tree.getElement()+" ");
BTNode<T> leftTree = Tree.left;
if(leftTree != null)
{
preOrder(leftTree);
}
BTNode<T> rightTree = Tree.right;
if(rightTree != null)
{
preOrder(rightTree);
}
}

public static void inOrder(BTNode tree)
{
if(tree == null)
return;
else
{
inOrder(tree.left);
System.out.print(tree.print()+" ");
inOrder(tree.right);
}
}
}


foundation 包的截图

public class part2 {
public static void main(String[] args) {
BTNode tree;
char[] pre = {'A','B','D','H','I','E','J','M','N','C','F','G','K','L'};
char[] in = {'H','D','I','B','E','M','J','N','A','F','C','K','G','L'};
tree = t.construct(pre,in);

System.out.println("先序遍历");
t.preOrder(tree);
System.out.println("\n中序遍历");
t.inOrder(tree);
}
}


import java.util.Scanner;

public class Expert {

public Expert(){
String e1 = "喜欢球鞋？";
String e2 = "有钱？";
String e3 = "那没事了";
String e4 = "你觉得李宁球鞋好吗";
String e5 = "没钱你说个锤子";
String e6 = "那你去买李宁去吧";
String e7 = "就是，李宁脸都不要了，四百块一双鞋能炒到一千多是真的离谱";

}

public void diagnose(){
Scanner scan = new Scanner(System.in);

System.out.println("开始");
while(current.size()>1)
{
System.out.println(current.getRootElement());
if(scan.nextLine().equalsIgnoreCase("Y"))
current = current.getLeft();
else
current = current.getRight();
}
System.out.println(current.getRootElement());
}
}


import java.util.Stack;

public class Fix {
static Stack<Character> op = new Stack<>();

public static Float getv(char op, Float f1, Float f2) {
if (op == '+') return f2 + f1;
else if (op == '-') return f2 - f1;
else if (op == '*') return f2 * f1;
else if (op == '/') return f2 / f1;
else return Float.valueOf(-0);
}

public static float calrp(String rp) {
Stack<Float> v = new Stack<>();
char[] arr = rp.toCharArray();
int len = arr.length;
for (int i = 0; i < len; i++) {
Character ch = arr[i];
if (ch >= '0' && ch <= '9') v.push(Float.valueOf(ch - '0'));
else v.push(getv(ch, v.pop(), v.pop()));
}
return v.pop();
}

public static String getrp(String s) {
char[] arr = s.toCharArray();
int len = arr.length;
String out = "";
for (int i = 0; i < len; i++) {
char ch = arr[i];
if (ch == ' ') continue;
if (ch >= '0' && ch <= '9') {
out += ch;
continue;
}
if (ch == '(')
op.push(ch);
if (ch == '+' || ch == '-') {
while (!op.empty() && (op.peek() != '('))
out += op.pop();
op.push(ch);
continue;
}
if (ch == '*' || ch == '/') {
while (!op.empty() && (op.peek() == '*' || op.peek() == '/'))
out += op.pop();
op.push(ch);
continue;
}
if (ch == ')') {
while (!op.empty() && op.peek() != '(')
out += op.pop();
op.pop();
continue;
}
}
while (!op.empty()) out += op.pop();
return out;
}
}


## 3. 实验过程中遇到的问题和解决过程

• 问题1 教材代码中的ArrayIterator类无法识别

• 问题1解决方案：修改方法的定义类型为ArrayList

• 问题2 书中代码内容残缺，对实验要求理解不全

• 问题2 已解决，尽力补全代码使得书上程序能运行

## 其他（感悟、思考等）

posted @ 2020-12-09 21:42  就是个哈皮  阅读(117)  评论(0编辑  收藏  举报