20202320 2021-2022-1 实验八《数据结构与面向对象程序设计——树》实验报告

课程:《程序设计与数据结构》
班级: 2023
姓名: 陈欢
学号:20202320
实验教师:王志强
实验日期:2021年11月18日
必修/选修: 必修

##1.实验内容

  1.参考教材PP16.1,完成链树LinkedBinaryTree的实现(getRight,contains,toString,preorder,postorder),用JUnit或自己编写的驱动类对自己实现的LinkedBinaryTree进行测试,提交代码运行截图,要全屏,包含自己的学号信息,课下把代码推送到代码托管平台。

  2.基于LinkedBinaryTree,实现基于(中序,先序)序列构造唯一一颗二叉树的功能,比如给出中序和后续,构造出附图中的树,用JUnit或自己编写的驱动类对自己实现的LinkedBinaryTree进行测试,提交代码运行截图,要全屏,包含自己的学号信息,课下把代码推送到代码托管平台。

  3.自己设计并实现一棵决策树,提交代码运行截图,要全屏,包含自己的学号信息,课下把代码推送到代码托管平台。

  4.输入中缀表达式,使用树将中缀表达式转换成后缀表达式,并输出后缀表达式和计算结果(如果没有用树,正常评分。如果用了树,即使有小的问题,也酌情给满分),提交代码运行截图,要全屏,包含自己的学号信息

##2.实验过程及结果

  1.参考教材PP16.1,完成链树LinkedBinaryTree的实现(getRight,contains,toString,preorder,postorder),用JUnit或自己编写的驱动类对自己实现的LinkedBinaryTree进行测试,提交代码运行截图,要全屏,包含自己的学号信息,课下把代码推送到代码托管平台。

 完整代码如下:

//import com.sun.source.tree.BinaryTree;

import javax.swing.*;

public class LinkedBinaryTree {
public BinaryTree head;
public int nChenhuan, count;
public LinkedBinaryTree() {
nChenhuan = 0;
count = 0;
head = null;
}
public class BinaryTree {
public char data;
public BinaryTree nextright, nextleft;

public BinaryTree(char obj) {
this.data = obj;
}
}
public void compare(BinaryTree root,BinaryTree obj){
if(root.data>obj.data){
if(root.nextleft==null){
root.nextleft=obj;
count++;
nChenhuan++;
}else{
root=root.nextleft;
compare(root,obj);
}
}else{
if(root.nextright==null){
root.nextright=obj;
count++;
nChenhuan++;
}else{
root=root.nextright;
compare(root,obj);
}
}
}
public BinaryTree add(char obj) {
BinaryTree element = new BinaryTree(obj);
if (head == null) {
head = element;
nChenhuan++;
count++;
return element;
} else {
BinaryTree headrear = head;
while(true){
compare(headrear,element);
break;
}
return element;
}
}
public void findNode(char obj,BinaryTree node){
if(node!=null) {
if (obj > node.data) {
node = node.nextright;
findNode(obj, node);
} else if (obj < node.data) {
node = node.nextleft;
findNode(obj, node);
} else if (obj == node.data) {
if (node.nextright == null)
System.out.println("寻找结点没有右结点!");
else
System.out.println(node.nextright.data);
}
}
}
public void getRight(char obj){
BinaryTree node=head;
findNode(obj,node);
}
//先序遍历
public String str0="";
public String preorder(BinaryTree root){
if(root!=null){
str0=str0+root.data+",";
preorder(root.nextleft);
preorder(root.nextright);
}
return str0;
}
public String str1="";
public String inorder(BinaryTree root){
if(root!=null){
inorder(root.nextleft);
str1=str1+root.data+",";
inorder(root.nextright);
}
return str1;
}
public String str2="";
public String postorder(BinaryTree root){
if(root!=null){
postorder(root.nextleft);
postorder(root.nextright);
str2=str2+root.data+",";
}
return str2;
}
public int contains(){
return nChenhuan;
}
public String toString(){
return str1;
}
public String string="";
//基于中序后序序列的唯一二叉树
public String inpost(char[] in,char[] post,int root,int start,int end){//基于中序和后序序列的唯一二叉树
if(start>end)
return string;
int i=start;
while(i<end&&in[i]!=post[root])
i++;
string=string+post[root]+",";
inpost(in,post,root-1-end+i,start,i-1);
inpost(in,post,root-1,i+1,end);
return string;
}
}

测试结果如下:

   2.基于LinkedBinaryTree,实现基于(中序,先序)序列构造唯一一颗二叉树的功能,比如给出中序和后续,构造出附图中的树,用JUnit或自己编写的驱动类对自己实现的LinkedBinaryTree进行测试,提交代码运行截图,要全屏,包含自己的学号信息,课下把代码推送到代码托管平台。

 完整代码已展示,见上。

测试结果见图上testinpost()。

  3..自己设计并实现一棵决策树,提交代码运行截图,要全屏,包含自己的学号信息,课下把代码推送到代码托管平台。

 测试结果:

 

 完整代码如下:

public class DecisionTree{
public nodeDecision head;
public int nChenhuan;
public class nodeDecision{
public String data;
public nodeDecision left,right;
public char[] selection;
public nodeDecision(String obj,char[] ch){
this.data=obj;
this.selection=ch;
}
}
public DecisionTree(){
head=null;
nChenhuan=0;
}
public void addDecision(char[] choice,String obj){
nodeDecision element=new nodeDecision(obj,choice);
if(nChenhuan==0){
head=element;
nChenhuan++;
}else {
nodeDecision rearhead=head;
compare(rearhead,element);
}
}
public int j=0,i=0;
public void compare(nodeDecision node,nodeDecision add) {
if (node.selection[i] == 'N' && add.selection[i] == 'N') { //即选择为no
if (add.selection[i + 1] == 'N') {
if (node.left == null) {
node.left = add;
nChenhuan++;
i=0;
} else {
node = node.left;
i++;
compare(node, add);
}
} else if (add.selection[i + 1] == 'Y') {
if (node.right == null) {
node.right = add;
nChenhuan++;
i=0;
} else {
node = node.right;
i++;
compare(node, add);
}
}
} else if (node.selection[i] == 'Y' && add.selection[i] == 'Y') {
if (add.selection[i + 1] == 'Y') {
if (node.right == null) {
node.right = add;
nChenhuan++;
i=0;
} else {
node = node.right;
i++;
compare(node, add);
}
} else if (add.selection[i + 1] == 'N') {
if (node.left == null) {
node.left = add;
nChenhuan++;
i=0;
} else {
node = node.left;
i++;
compare(node, add);
}
}
}
}
public DecisionTree run( String ch, DecisionTree rear) {
if (ch.equals("Yes")) {
if(rear.head!=null)
rear.head=rear.head.right;
} else if (ch.equals("No")) {
if(rear.head!=null)
rear.head=rear.head.left;
}else if(ch.equals("Ok")){
return null;
}
return rear;
}
public DecisionTree runDecision(String choice,DecisionTree reartree){
reartree=run(choice,reartree);
if(reartree.head==null){
System.out.println("no answer!");
}else {
System.out.println(reartree.head.data);
}
return reartree;
}
}

  4.输入中缀表达式,使用树将中缀表达式转换成后缀表达式,并输出后缀表达式和计算结果(如果没有用树,正常评分。如果用了树,即使有小的问题,也酌情给满分),提交代码运行截图,要全屏,包含自己的学号信息

  

 完整代码如下:

public class StackTest {
public static void main(String[] args) {
Character[] infix1={'a','+','b','*','c','(','d','*','e','+','f',')','*','g'};
ArrayStack<Character> infix=new ArrayStack<Character>(infix1);
for(int num=0;num<infix1.length;num++){
infix.push(infix1[num]);
}
String postfix="";
infix.toString();

System.out.println(Trans(infix,postfix));
}
public static int prio(char op){
int priority=-1 ;
if(op=='*'||op=='/')
priority=2;
if(op=='+'||op=='-')
priority=1;
if(op=='(')
priority=0;
return priority;

}
public static String Trans(ArrayStack<Character> str,String str1){
Character[] n=new Character[14];
ArrayStack<Character> s = new ArrayStack<>(n);
int i;
for(i=0;i<str.size();i++){
if(str.arrayStack2023[i]>='0'&&str.arrayStack2023[i]<='9'
||str.arrayStack2023[i]>='a'&&str.arrayStack2023[i]<='z'){
str1+=str.arrayStack2023[i];
}else{
if(s.isEmpty())
s.push(str.arrayStack2023[i]);
else if(str.arrayStack2023[i]=='(')
s.push(str.arrayStack2023[i]);
else if(str.arrayStack2023[i]==')'){
while(s.top()!='('){
str1=str1+s.top();
s.pop();
}
s.pop();
}else{
while(prio(str.arrayStack2023[i])<=prio(s.top())){
str1=str1+s.top();
s.pop();
if(s.isEmpty())
break;
}
s.push(str.arrayStack2023[i]);
}
}
}
while(!s.isEmpty()){
str1=str1+s.pop();
s.pop();
}
return str1;
}
}

 ##3.实验心得与体会

  难呀难呀!!!事实上,数据结构考验的是人的逻辑思维,有时候想通了就会简单得多。比如递归,到底怎么“递”,有时候就需要问题简单化。

##4.参考资料

 ---《Java程序设计教程(第九版)》
---   https://www.cnblogs.com/wkfvawl/p/12864789.html
---《Java软件结构与数据结构(第四版)》

 

posted @ 2021-11-28 21:48  20202320-陈欢  阅读(33)  评论(0编辑  收藏  举报