#20202306 2021-2022-1 data structure and object oriented programming experiment 8 report
Course: programming and data structure
Class: 2023
Name: Heyu
Student No.: 20202325
Experimental teacher: Wang Zhiqiang
Experiment date: November 25, 2021
Compulsory / elective: compulsory
##1. Experimental contents
1. Refer to textbook PP16.1 and complete the implementation of the chain tree LinkedBinaryTree (getRight,contains,toString,preorder,postorder)
Use JUnit or write your own driver class to test your LinkedBinaryTree. Submit a screenshot of the test code, with a full screen, including your student number information
Push the code to the code hosting platform after class
2. Based on LinkedBinaryTree, realize the function of constructing a unique binary tree based on (middle order, first order) sequence, such as giving middle order HDIBEMJNAFCKGL and later order ABDHIEJMNCFGKL to construct the tree in the attached figure
Use JUnit or write your own driver class to test the functions you realize, and submit the screenshot of the test code. It should be full screen, including your own student number information
Push the code to the code hosting platform after class
3. Design and implement a decision tree
Submit a screenshot of the test code operation, with a full screen, including your student number information
Push the code to the code hosting platform after class
4. Input infix expression, use tree to convert infix expression into suffix expression, and output suffix expression and calculation results (if tree is not used, score normally. If tree is used, give full score as appropriate even if there are small problems)
Submit a screenshot of the test code operation, with a full screen, including your student number information
#Process
1. Refer to textbook PP16.1 and complete the implementation of the chain tree LinkedBinaryTree (getRight,contains,toString,preorder,postorder)
Use JUnit or write your own driver class to test your LinkedBinaryTree. Submit a screenshot of the test code, with a full screen, including your student number information
Push the code to the code hosting platform after class
code:
Node:
package BinaryTree; public class Node { int data; //Node data Node leftChild; //Reference of left child node Node rightChild; //Reference of right child node boolean isDelete;//Indicates whether the node is deleted public Node(int data){ this.data = data; } //Print node content // public void display(){ // System.out.println(data); // } @Override public String toString() { return "data=" + data +","+ "leftChild=" + leftChild + ","+" rightChild=" + rightChild + ","+"isDelete=" + isDelete; } }
BinareTree:
package BinaryTree; public class BinaryTree1 implements Tree { private Node root; //Insert node public boolean insert(int data) { Node newNode = new Node(data); if(root == null){//The current tree is empty and has no nodes root = newNode; return true; }else{ Node current = root; Node parentNode = null; while(current != null){ parentNode = current; if(current.data > data){//The current value is larger than the insertion value. Search the left child node current = current.leftChild; if(current == null){//If the left child node is empty, insert the new value directly into the node parentNode.leftChild = newNode; return true; } }else{ current = current.rightChild; if(current == null){//If the right child node is empty, insert the new value directly into the node parentNode.rightChild = newNode; return true; } } } } return false; } //Find node public Node find(int key) { Node current = root; while(current != null){ if(current.data > key){//The current value is larger than the lookup value. Search the left subtree current = current.leftChild; }else if(current.data < key){//The current value is smaller than the lookup value. Search the right subtree current = current.rightChild; }else{ return current; } } return null;//Traversing the entire tree, not found, return null } //Medium order traversal public void infixOrder(Node current){ if(current != null){ infixOrder(current.leftChild); System.out.print(current.data+" "); infixOrder(current.rightChild); } } //Preorder traversal public void preOrder(Node current){ if(current != null){ System.out.print(current.data+" "); preOrder(current.leftChild); preOrder(current.rightChild); } } //Postorder traversal public void postOrder(Node current){ if(current != null){ postOrder(current.leftChild); postOrder(current.rightChild); System.out.print(current.data+" "); } } //Maximum found public Node findMax(){ Node current = root; Node maxNode = current; while(current != null){ maxNode = current; current = current.rightChild; } return maxNode; } //Minimum value found public Node findMin(){ Node current = root; Node minNode = current; while(current != null){ minNode = current; current = current.leftChild; } return minNode; } public static void main(String[] args) { BinaryTree1 tree = new BinaryTree1(); tree.insert(21); tree.insert(50); tree.insert(20); tree.insert(11); tree.insert(27); tree.insert(23); tree.insert(25); tree.insert(100); System.out.println(tree.findMax().data); System.out.println(tree.findMin().data); System.out.println(tree.find(100)); System.out.println(tree.find(10)); System.out.println("Middle order:"); tree.infixOrder(tree.root);//Middle order System.out.println(); System.out.println("Post order:"); tree.postOrder(tree.root);//Post order System.out.println(); System.out.println("Precedence:"); tree.preOrder(tree.root);//Preamble } }
Operation results:
2. Based on LinkedBinaryTree, realize the function of constructing a unique binary tree based on (middle order, first order) sequence, such as giving middle order HDIBEMJNAFCKGL and later order ABDHIEJMNCFGKL to construct the tree in the attached figure
Use JUnit or write your own driver class to test the functions you realize, and submit the screenshot of the test code. It should be full screen, including your own student number information
Push the code to the code hosting platform after class
result:
3. Design and implement a decision tree
Submit a screenshot of the test code operation, with a full screen, including your student number information
Push the code to the code hosting platform after class
4. Input infix expression, use tree to convert infix expression into suffix expression, and output suffix expression and calculation results (if tree is not used, score normally. If tree is used, give full score as appropriate even if there are small problems)
Submit a screenshot of the test code operation, with a full screen, including your student number information
#Question
1. There is no idea of programming and I don't know how to start.
Solution: turn over the textbook, ask the students, and check the information on the Internet;
2. Programs are related to each other, inherit and quote from each other, and change a program. As a result, many seemingly unrelated programs in front directly report errors. Looking back, I find that I can't find what's wrong, patching together and reporting errors everywhere.
#Perception
Sentiment is still a cliche. I'm confused when writing programs. I feel that my brain is not enough... I have to concentrate on learning for some time.
##References
- [learning guide for Java and Android Development (Second Edition) people's Posts and telecommunications press]
- [Java software structure and data structure (Third Edition) Tsinghua University Press]