20172323 2018-2019-1 Summary of the sixth week of program design and data structure

Source: Internet
Author: User

Tag: the bucket exceeds the get judgment setting and is not created for the add element htm.

20172323 2018-2019-1 Summary of the sixth week of program design and data structure

I learned Chapter 1 this week.Tree

  • 10.1 Overview
    • A tree is a non-linear structure that consists of a set of nodes and edges. The elements are stored in the node, and the edge connects the node.
    • Terms

      The root node is the only node on the top of the tree.
      The lower-layer node in the tree is the child of the Upper-layer node. One node has only one parent node.
      The two nodes of the same parent are called brothers.
      The root node is the only node in the tree that has no parent and no child is called a leaf. The rest are internal nodes.
      The node layer is the path length from the root node to the node
      The tree height refers to the maximum path length from the root to the leaves.

    • The maximum number of children that any node in the tree can have is called the degree of the tree. If there is no limit on the degree, it is called a generalized tree. The number of children at each node cannot exceed n. A tree with 2 degrees is called a binary tree.
    • If a tree is balanced (all the leaves of the tree are on the same layer or at least one layer is different from each other) and all the leaves at the bottom are on the left of the tree, the tree is regarded as a complete tree.
    • If all the leaves of an n-yuan tree are on the same layer and each node is either a leaf or has n Children, the tree is full.

  • 10.2 implement the tree Policy
    • Calculation policy: place the left child of element N in the location (2xn-1) and the right child in the location (2x (n + 1 )). The disadvantage is that a large amount of storage space is wasted.
    • Simulate a link policy: each element of an array is a node class, and each node stores an array index for each child. This policy allows continuous allocation of array locations without considering the completeness of the tree.
    • Generally, the height of a balanced N-element Tree Containing M elements is lognm.
  • 10.3 tree traversal
    • Forward traversal: access each node and its children from the root node
    • In-order traversal: Root Node --> left child --> node --> remaining Node
    • Post-order traversal: Root Node --> child --> node
    • Sequence traversal: access all nodes of each layer from the root node, one layer at a time
  • 10.4 Binary Tree
Operation Description
Getroot Returns a reference pointing to a binary tree root.
Isempty Determines whether the tree is empty.
Size Number of elements in the decision tree
Contains Determines whether the specified target is in the tree
Find If a specified element is found, a reference pointing to it is returned.
Tostring Returns the string representation of the tree.
Iteratorinorder Returns an iterator for the middle-order traversal of the tree.
Iteratorpreorder Returns an iterator for the previous traversal of the tree.
Iteratorpostorder Returns an iterator for the post-order traversal of the tree.
Iteratorlevelorder Returns an iterator for tree sequence traversal.

In all the listed operations, there is no operation to add elements to the tree. Tree elements are not deleted in some trees.

  • 10.5 use a binary tree: Expression Tree
    • Evaluate the Expression Tree from bottom up
  • 10.6 back pain diagnoses
    • Decision tree: its nodes represent decision points, and its subnodes represent candidate items of the Decision Point.
    • A decision tree with a better image (This section describes the Binary Decision Tree)
  • 10.7 implement binary tree using linked list
    • When a method is written recursively, it usually requires a private method, because the first call may be different from the signature and behavior of each subsequent method.
Problems and Solutions in teaching material Learning
  • Question 1: Method Used in program list 10.3 to determine whether it is an operator
public boolean isOperator(){    return (termType == 1);}

Why is it necessary to determine if termtype is a temporary operator?

  • Problem 1 Solution: According to the previous expressionop definition, two int variables and one char variable are set.
private int termType;private char operator;private int value;

Node storage only requires one int variable to store the operands and one char variable to store the operators. The extra int variable termtype is not used to store the operands. In this class, I found that the use of termtype is used to determine whether it is an operand, so the significance of setting this variable should be in advance when setting the knot, it indicates what the node stores. If it is an operator, it will change termtype to 1. If it is not, it will be set to another number. This avoids the need to write a long string of judgment conditions during the judgment.if (operator == "+" || operator == "-" || operator == "*" || operator == "/")Status

  • Question 2: The program list 10.2expressiontree class provides an output tree method, which is hard to understand and problematic.
  • Problem 2 solution: Analyze with specific code
Public String printtree () {unorderedlistadt <binarytreenode <expressiontreeop> nodes = new arrayunorderedlist <binarytreenode <expressiontreeop> (); // creates an unordered list for storing the binarytreenode class unorderedlistadt <integer> levellist = new arrayunorderedlist <integer> (); // create an unordered list of int variables binarytreenode <expressiontreeop> current; // set a temporary variable pointing to the unordered list current string result = ""; int printdepth = This. getheight (); // defines printdepth Int possiblenodes = (INT) math. pow (2, printdepth + 1); // defines the number of nodes of the possiblenodes tree. possiblenodes = 2 ^ (printdepth + 1) int countnodes = 0; nodes. addtorear (Root); // Add the root node to the end of the unordered list integer currentlevel = 0; integer previuslevel =-1; // set two int variables, determines levellist for the next line feed operation. addtorear (currentlevel); // currentlevel is added to the end of levellist while (countnodes <possiblenodes) {countnodes = countnodes + 1; curre Nt = nodes. removefirst (); // current is the first currentlevel to be removed by nodes = levellist. removefirst (); If (currentlevel> previuslevel) {result = Result + "\ n"; // If the currelevel is greater than the previuslevel, perform the line feed operation previuslevel = currentlevel; for (Int J = 0; j <(math. pow (2, (printdepth-currentlevel)-1); j ++) Result = Result + ""; // Add a space after line feed} else {for (INT I = 0; I <(math. pow (2, (printdepth-currentlevel + 1 ))- 1); I ++) {result = Result + ""; // if no line feed is performed, it indicates a node in the middle of a layer, add a space between two nodes} If (current! = NULL) {result = Result + (current. getelement ()). tostring (); // Add the root node of the current tree to nodes in the result string. addtorear (current. getleft (); levellist. addtorear (currentlevel + 1); nodes. addtorear (current. getright (); levellist. addtorear (currentlevel + 1); // store the left and right children of the previous root node to the end of nodes, currelevel + 1 is saved to levellist, indicating that the Left and Right children are in the next row of the root node} else {nodes. addtorear (null); levellist. addtorear (currentlevel + 1); nodes. addtorear (null); levellist. addtorear (currentlevel + 1); Result = Result + "" ;}} return result ;}

I think this part of the code is hard to understand in order to output the image close to the structure of our general tree, and to perform the line feed operation and add spaces.
Determine the layer at which a node uses a list of levellist, and assign values of two int variables currentlevel and previuslevel to 0 and-1, respectively, use the relationship between currentlevel and previuslevel to determine whether a line break is required. For example, when the loop is to the second layer, there are two left and right nodes in nodes, and the first and second nodes in levellist store 1 at this time, the previuslevel is 0 at this time, and the first loop iscurrentLevel = levelList.removeFirst, Currentlevel = 1> previuslevel = 0, so you need to start another rowresult = result + "\n\n"And then assign the previuslevel to the current currentlevel value.previousLevel = currentLevel;, Current is the first node of nodes, that is, the left child, the result string, and the left and right children are added to the end of the list respectively. Their corresponding levellist values are currentlevel + 1 = 2, then, the right child in the list header is assigned to current. In this case, currlevel = previuslevel = 1. Therefore, the else path does not need to be wrapped and the next operation is performed. If the right child is a leaf node, although there are no sub-nodes, there will still be storage spacenodes.addToRear(current.getLeft());But it stores null, so the computer will continue to execute when it comes to the output and create new buckets. Therefore, a while loop needs to be set outside, because the first N layers of the binary tree have no more than 2 ^ (n + 1) elements, so the possiblenodes value is definedint possibleNodes = (int) Math.pow(2, printDepth + 1);
In addition, I am not very familiar with the space-adding method. For example, why is this definition necessary?j < ((Math.pow(2, (printDepth - currentLevel))) - 1)Add a space between two nodes.
It seems that the values of the two are not so strict, that is, it is not necessary to take 0 and-1, and change to 1 and 0,-1 and-2, however, different values may affect the width and width of the tree structure, or the difference between the two may not necessarily be 1, and the difference between the two may be no problem.levelList.addToRear(currentLevel + 1);Corresponding to add 2 or 3.

Issues and Solutions in code debugging
  • Question 1: When the printtree method of the expressiontree class is run, not all tree structures are printed,

  • Problem 1 Solution: Debug it. I found that the getheight method has not been completed yet. The getheight method still returns 0; naturally, an error occurs. The method for returning height should be divided into three steps to determine whether the node of the tree is the root node, the internal node, and the leaf node. First, determine whether the root node is empty, do not add a blank height, and then use a while loop and pp10.3 to repeatedly judge whether the next node is an internal node. If so, add a height and assign the node to the left child of the node, if it is determined that this node is a leaf node, the height is added and the height value is returned again and again.
BinaryTreeNode cur = root;        int height = 0;        if (root != null){            height++;        }        cur = cur.left;        while (cur.judge() != true){            height++;            cur = cur.left;        }        height++;        return height;

The modified code runs

  • Question 2: When the tostring method of the semi binarytree is used, a garbled string is output.

  • Problem 2 solution: Find a solution on the Internet, and then learn that the array does not have the tostring method.

    The tostring () method in the object returns the type name and digest of the input parameter (The hexadecimal encoding of the hashcode of the string). The tostring () method is used directly for the array, you will get an ljava. lang. string; @ 175d6ab
    Here, ljava. Lang. String is exponential data of the string type.
    Although the garbled information here is 3ecf72fd, the solution provided by 3ecf72fd is to output every value in the array using a loop method.
    Here, the structure of the output tree needs to be recursive, and the output is a bit strange: I put the right child on the parent, and the left child on the parent, so there will be some different tree shapes

public void oString() {        string(root);    }private void string(BinaryTreeNode temp){        if(temp != null){            string(temp.getRight());            System.out.println(temp.getElement() + " ");            string(temp.getLeft());        }    }

Output result

Make it happen.

Code hosting

Summary of last week's exam errors

No questions last week

Pairing and peer evaluation
  • Problems worth learning in a blog:
    • Tan Xin's blog has always maintained a high level, especially the problem summary and solution capabilities are worth learning. It can be seen that he is studying hard.
    • Fang Yiwen summarized the learning content of his blog materials in detail, provided the illustration and his own understanding, and liked it.
  • Based on the scoring standard, I scored tan Xin's blog: 8 points. The score is as follows:
    Correct markdown syntax (+ 1 point ):
    Complete elements in the template (plus 1 point)
    Three questions plus three points during the Problem and Solution Process in the course of teaching material Learning
    Three points are added for three problems during code debugging and troubleshooting.

  • Based on the scoring standard, I scored Fang Yiwen's blog: 8 points. The score is as follows :,
    Correct markdown syntax (+ 1 point ):
    Complete elements in the template (plus 1 point)
    The Problem and Solution Process in the course of teaching material learning add two points
    Three points are added for three problems during code debugging and troubleshooting.
    Excellent layout + 1 point

  • Study of this week's pairing
    • 20172305
    • 20172314
    • Peer learning content
      • Tree
  • Blog peer evaluation last week
    • 20172305
    • 20172314
Others

This week's learning is not bad. The most difficult part is not the understanding of several pp projects, but the printtree Code mentioned in the question. However, the test was a little bad. I don't know why I got two wrong questions.

Learning progress bar
Number of lines of code (Add/accumulate) Blog volume (New/accumulated) Learning time (Add/accumulate) Important Growth
Target 5000 rows 30 articles 400 hours
WEEK 1 0/0 1/1 8/8
Week 2 470/470 1/2 12/20
Week 3 685/1155 2/4 10/30
Week 4 2499/3654 2/6 12/42
Week 6 1218/4872 2/8 10/52
Week 7 590/5462 1/9 12/64
References
  • Blue Mo cloud class

20172323 2018-2019-1 Summary of the sixth week of program design and data structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.