20192321 2020-2021-1 《数据结构与面向对象程序设计》实验八报告
20192321 实验八 《数据结构与面向对象程序设计》实验报告
课程:《程序设计与数据结构》
班级:1923
姓名:李锦程
学号:20192321
实验教师:王志强
实验日期:2020年12月3日
必修/选修:必修
1.实验内容
- 参考教材PP16.1,完成链树LinkedBinaryTree的实现(getRight,contains,toString,preorder,postorder)
用JUnit或自己编写驱动类对自己实现的LinkedBinaryTree进行测试,提交测试代码运行截图,要全屏,包含自己的学号信息
课下把代码推送到代码托管平台 - 基于LinkedBinaryTree,实现基于(中序,先序)序列构造唯一一棵二㕚树的功能,比如给出中序HDIBEMJNAFCKGL和后序ABDHIEJMNCFGKL,构造出附图中的树
用JUnit或自己编写驱动类对自己实现的功能进行测试,提交测试代码运行截图,要全屏,包含自己的学号信息
课下把代码推送到代码托管平台 - 自己设计并实现一颗决策树
提交测试代码运行截图,要全屏,包含自己的学号信息
课下把代码推送到代码托管平台 - 输入中缀表达式,使用树将中缀表达式转换为后缀表达式,并输出后缀表达式和计算结果(如果没有用树,正常评分。如果用到了树,即使有小的问题,也酌情给满分)
提交测试代码运行截图,要全屏,包含自己的学号信息
2.实验过程及结果
完成链树LinkedBinaryTree的实现
BTNode
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);
iter.add (element);
if (right != null)
right.inorder (iter);
}
public void preorder (ArrayList<T> iter) {
iter.add (element);
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);
iter.add (element);
}
public char print() {
return (char) element;
}
}
LinkedBinaryTree
import java.util.Iterator;
public class LinkedBinaryTree<T> implements BinaryTree<T> {
public BTNode<T> root;//定义根结点
public BTNode left;
public BTNode right;
public LinkedBinaryTree()
{
root = null;
}//未赋值时,根结点为空
public LinkedBinaryTree (T element)
{
root = new BTNode<T>(element);
}//进行根结点赋值
public LinkedBinaryTree (T element, LinkedBinaryTree<T> left,
LinkedBinaryTree<T> right)//根、左、右、再思考
{
root = new BTNode<T>(element);
root.setLeft(left.root);
root.setRight(right.root);
}
public T getRootElement() throws Exception {
if (root == null)
throw new Exception ("Get root operation "
+ "failed. The tree is empty.");
return root.getElement();
}
//先左子树
public LinkedBinaryTree<T> getLeft() throws Exception {
if (root == null)
throw new Exception ("Get left operation "
+ "failed. The tree is empty.");
LinkedBinaryTree<T> result = new LinkedBinaryTree<T>();
result.root = root.getLeft();
return result;
}
public T find (T target) throws Exception {
BTNode<T> node = null;
if (root != null)
node = root.find(target);
if (node == null)
throw new Exception("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 Iterator<T> inorder()
{
ArrayIterator<T> iter = new ArrayIterator<T>();
if (root != null)
root.inorder (iter);
return iter;
}
public Iterator<T> levelorder() throws EmptyCollectionException {
LinkedQueue<BTNode<T>> queue = new LinkedQueue<BTNode<T>>();
ArrayIterator<T> iter = new ArrayIterator<T>();
if (root != null)
{
queue.enqueue(root);
while (!queue.isEmpty())
{
BTNode<T> current = queue.dequeue();
iter.add (current.getElement());
if (current.getLeft() != null)
queue.enqueue(current.getLeft());
if (current.getRight() != null)
queue.enqueue(current.getRight());
}
}
return iter;
}
public Iterator<T> ArrayIterator()
{
return inorder();
}
//再右子树
public LinkedBinaryTree<T> getRight() throws Exception {
if (root == null)
throw new Exception ("Get Right operation "
+ "failed. The tree is empty.");
LinkedBinaryTree<T> result = new LinkedBinaryTree<T>();
result.root = root.getRight();
return result;
}
public boolean contains (T target) throws Exception {
BTNode<T> node = null;
boolean result = true;
if (root != null)
node = root.find(target);
if(node == null)
result = false;
return result;
}
public boolean isEmpty() {
if(root!=null)
return false;
else
return true;
}
public String toString() {
ArrayIterator<T> list = (ArrayIterator<T>) preorder();
String result = "<top of Tree>\n";
for(T i : list){
result += i + "\t";
}
return result + "<bottom of Tree>";
}
public Iterator<T> preorder() {
ArrayIterator<T> list = new ArrayIterator<>();
if(root!=null)
root.preorder(list);
return list;
}
public Iterator<T> postorder() {
ArrayIterator<T> list = new ArrayIterator<>();
if(root!=null)
root.postorder(list);
return list;
}
@Override
public Iterator<T> iterator() {
return null;
}
}
基于(中序,先序)序列构造唯一一棵二㕚树的功能
扩展LinkedBinaryTree
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);
}
}
自己设计并实现一颗决策树
import ex81.LinkedBinaryTree;
import java.util.Scanner;
public class PsychologicalTest
{
private LinkedBinaryTree<String> tree;
public PsychologicalTest()
{
String e1 = "你看球赛吗 ?";
String e2 = "你看美剧吗 ?";
String e3 = "你经常打游戏吗 ?";
String e4 = "你有对象吗 ?";
String e5 = "你氪金了吗 ?";
String e6 = "你喜欢打实况足球吗 ?";
String e7 = "你有传奇马拉多纳吗 ?";
String e8 = "同道单身狗";
String e9 = "咱俩无话可聊";
String e10 = "穷鬼";
String e11 = "大户人家";
String e12 = "祝你早日登上大地球";
String e13 = "nt";
String e14 = "废物";
String e15 ="精神小伙";
LinkedBinaryTree<String> n2, n3, n4, n5, n6, n7, n8, n9,
n10, n11, n12, n13,n14,n15;
n8 = new LinkedBinaryTree<String>(e8);
n9 = new LinkedBinaryTree<String>(e9);
n4 = new LinkedBinaryTree<String>(e4, n8, n9);
n10 = new LinkedBinaryTree<String>(e10);
n11 = new LinkedBinaryTree<String>(e11);
n5 = new LinkedBinaryTree<String>(e5, n10, n11);
n12 = new LinkedBinaryTree<String>(e12);
n13 = new LinkedBinaryTree<String>(e13);
n6 = new LinkedBinaryTree<String>(e6, n12, n13);
n14 = new LinkedBinaryTree<String>(e14);
n15 = new LinkedBinaryTree<String>(e15);
n7 = new LinkedBinaryTree<String>(e7,n14,n15);
n2 = new LinkedBinaryTree<String>(e2, n4, n5);
n3 = new LinkedBinaryTree<String>(e3, n6, n7);
tree = new LinkedBinaryTree<String>(e1, n2, n3);
}
public void start()
{
Scanner scan = new Scanner(System.in);
LinkedBinaryTree<String> current = tree;
System.out.println ("让我们聊一聊");
while (current.size() > 1)
{
System.out.println (current.getRootElement());
if (scan.nextLine().equalsIgnoreCase("N"))
current = current.getLeft();
else
current = current.getRight();
}
System.out.println (current.getRootElement());
}
public static void main(String[] args){
PsychologicalTest test = new PsychologicalTest();
test.start();
}
}
中缀表达式转换
普通使用栈设计转换代码
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;
}
}
测试
import java.util.Scanner;
public class Test4 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入表达式:");
String s = scan.nextLine();
Fix fix = new Fix();
System.out.println("后缀表达式为:\n"+Fix.getrp(s));
System.out.println("计算结果为:\n"+fix.calrp(Fix.getrp(s)));
}
}
运行结果截图
其他(感悟、思考等)
在这次实验过程中,我遇到了许多问题,其中既有知识上的漏洞,也有不细心导致的马虎,这一切都补充,完善,丰富,扩展了我的计算机知识体系。在这个过程中,我还进一步熟悉了IDEA这个平台的使用与运行方式,提高了自己自主学习的能力,为我接下来学习数据结构以及JAVA语言程序设计打下了坚实的基础,并在不断探索的过程中逐步提升了自己。