2017-2018 Chapter 6 study summary

Source: Internet
Author: User
Tags iterable concurrentmodificationexception
2017-2018 Chapter 6 Study Summary 1. Teaching Material Content summary
  • What is a tree?
    • Stacks, queues, and lists are linear structures, whileNon-linear Tree Structure.
    • The linear structure isOne-to-oneAnd the tree featuresOne-to-multiple.
    • A tree consistsNodeAndEdge.
    • The connection between the node and the node is completed by the edge.
    • Nodes at a lower level in the tree areChildThe node without any children is calledLeaf.
    • The number of edges between two nodes isPath Length.
    • The height of a tree refers to the maximum path length between the root and the leaf.
    • Use graphs:
  • Tree Classification:
    • Any node in the tree can have the maximum number of children.DegreesAccording to this classification standard, it can be divided:Generalized tree, N-element tree, and binary tree.
    • Balance Tree: All the leaves in the tree are at the same level or at least one layer is different from each other. The height of a balanced N-element Tree Containing M elements isLogn (m).
    • Full tree: The tree is balanced and all leaves are on the left.

    • Manshu: All the leaves of the N-element tree are on the same layer, and each node is either a leaf or has n Children.

  • Implementation tree policy:
    • Calculation policy for tree array implementation:

      This calculation method places the left child of element N on '2n + 1', And the right child on '2n + 2'. where N is the index of the parent child.

      For example:

      The index of A is 0, and B is the left child of a, so its position in the array is2*0+1=1So the position of B in the array is at the position where the index is 1. D is the left child of B, B's index is 1, so d's index is2*1+1=3.

    • Tree array implementation:

      Each element in the array is a node, and each child's index is stored in the node. For example, the child of a has two B and C, and their indexes are 2 and 1, respectively, therefore, 2 and 1 are stored in.

  • Tree traversal: There are four basic methods for tree traversal: pre-order traversal, middle-order traversal, post-order traversal, and sequence traversal.
    • Forward traversal: access each node and its children from the root node. For example, the traversal sequence is A, B, D, F, E, and C. Use pseudo code to achieve:
    Visit node;Traverse(left child);Traverse(right child);
    • Sequencing in the middle: Starting from the root node, access the left child of the node, then the node, and then any remaining node. The traversal sequence of is: D, B, E, F, A, and C. The pseudo code is implemented as follows:
    Traverse(left child);Visit node;Traverse(right child);
    • Next traversal: The child accessing the node starting from the root node, and then the child accessing the node. The traversal sequence is F, D, E, B, C, and. Use pseudo code to achieve:
    Traverse(left child);Traverse(right child);Visit node;
    • Sequence traversal: access all nodes of each layer at a time starting from the root node. The traversal sequence is A, B, C, D, E, and F.
  • Binary Tree: a binary tree is a very important data structure. It also has the characteristics of arrays and linked lists. It can be quickly searched like an array or added as a linked list. But he also has his own shortcomings:Complex deletion operations.

    The nature of Binary Trees:

    • There are at most 2 ^ (I-1) nodes on the I layer of the binary tree.
    • The depth of K binary tree has at most 2 ^ K-1 nodes (k ≥ 1), at least K nodes.
    • For any binary tree, if the number of terminal nodes is N0 and the number of nodes with the degree of 2 is N2, N0 = n2 + 1.
    • The depth of the Complete Binary Tree with N nodes is [log2n] + 1, where [log2n] + 1 is rounded down.
    • If there is a Complete Binary Tree node with N nodes numbered by layer, for any layer of node I (1 <= I <= N) there are:
      • If I = 1, the node is the root of the binary tree and has no parent. If I> 1, the parent node is [I/2] and rounded down.
      • If 2I> N, node I has no left child; otherwise, the left child is 2I.
      • If 2I + 1> N, the node does not have the right child; otherwise, the right child is 2I + 1.
  • Implement Binary Tree:
    • Interface for writing binary trees (binarytreeadt) :
    • Binary Tree:
    1. Expression Tree
    2. Back pain diagnoses
    • Use a linked list to implement a binary tree:
    1. Binarytreenode

      Ii. Problems and Solutions in teaching material Learning
  • Question 1: with the definition of sequential traversal: Starting from the root node, access the left child of the node, then the node, and then any remaining node. When a node has only one child, there is no right or right child. Are the children who access this node first, or are they accessing other nodes first? When the node is D, access h first or access e.
  • My understanding: for D, although node H is not his left child, for d h and E are all his remaining nodes, H is still his child, therefore, H still has priority. Therefore, the order is H, D, B, E, A, F, C, and G.
  • Question 2: What is the principle of the print tree printtree () in the Expression Tree?
  • Problem 2 solution:
    This is a method for printing a tree: The while loop prints a node loop every time. Example 2 × 3 (Expression Tree)
    • Note:
      • Nodes: defines a list for node installation.
      • Levellist: defines a list of nodes to be loaded.
      • Printdepth: the depth of the tree (starting from scratch ).
      • Countnode: used to record the number of output nodes.
      • Currentlevel: the current node level.
      • Previuslevel: the level of the previous node.
      • Result is the output result.
    • Before the first cycle:
      • Nodes: × // contains the root node, that is, "×"
      • Printdepth = 1, possiblebodes = 4, countnodes = 0, currentlevel = 0, previouslevel =-1,
      • Levellist: 0 // contains 0
    • First cycle:
      • Countnodes = 1;
      • Current = node ("X"), nodes = NULL;
      • Currentlevel = 0. levellist = NULL;
      • Enter the IF () statement, and then result = "_" and previuslevel = 0; (Note: Only one space is added, and the line break is automatically omitted .)
      • Enter the next if () statement, and then: Result = "_" + "×"; node: 2 3; levellist: 1 1;
    • The second cycle:
      • Countnodes = 2;
      • Current = node ("2"), now nodes: 3;
      • Currentlevel = 1, levellist: 1;
      • Enter the IF () statement, and then result = "_" + "×" + "\ n", previuslevel = 1; (Note: the number of spaces added this time is 0, line breaks are automatically omitted .)
      • Enter the next if () statement, and then: Result = "_" + "×" + "\ n" + 2; node: 3, null, null; levellist: 1, 2, 2; // this time, because Node 2 does not have left or right children, nodes has two null values, and their level is 2.
    • The third cycle:
      • Countnodes = 3;
      • Current = node ("3"), nodes: NULL, null;
      • Currentlevel = 1, levellist: 2, 2;
      • Go to the IF () statement. If the condition is not met, go to Else. Result = "-" + "X" + "\ n" + "2" + "-" + "-", previuslevel = 1; (Note: 2 spaces are added .)
      • Enter the next if () statement: result = "-" + "X" + "\ n" + "2" + "-" + "-" + "3"; node: NULL, null, null, null; levellist:,; // this time because Node 3 has no left or right children, so nodes has two null values, and their level is 2.
    • The fourth cycle: the output is null and cannot be seen. Therefore, we do not need to discuss it.
      Code running:

      Method Code:
    Public String printtree () // print the tree code. {Unorderedlistadt <binarytreenode <expressiontreeop> nodes = new unorderedlistarraylist <binarytreenode <expressiontreeop> (); // defines a list for loading nodes. Unorderedlistadt <integer> levellist = new unorderedlistarraylist <integer> (); // defines a list of nodes used for node loading. Binarytreenode <expressiontreeop> current; string result = ""; int printdepth = This. getheight (); int possiblenodes = (INT) math. pow (2, printdepth + 1); int countnodes = 0; nodes. addtorear (Root); integer currentlevel = 0; integer previuslevel =-1; levellist. addtorear (currentlevel); While (countnodes <possiblenodes) {countnodes = countnodes + 1; current = nodes. removefirst (); currentlevel = level List. removefirst (); If (currentlevel> previuslevel) {result = Result + "\ n"; previuslevel = currentlevel; For (Int J = 0; j <(math. pow (2, (printdepth-currentlevel)-1); j ++) Result = Result + ";} else {for (INT I = 0; I <(math. pow (2, (printdepth-currentlevel + 1)-1); I ++) {result = Result + "" ;}} if (current! = NULL) {result = Result + (current. getelement ()). tostring (); nodes. addtorear (current. getleft (); levellist. addtorear (currentlevel + 1); nodes. addtorear (current. getright (); levellist. addtorear (currentlevel + 1);} else {nodes. addtorear (null); levellist. addtorear (currentlevel + 1); nodes. addtorear (null); levellist. addtorear (currentlevel + 1); Result = Result + "" ;}} return result ;}
  • Question 3: How do I write the iterator in linkbinarytree?
  • Solution to problem 3: For the iterator in chapter 7, write this method. So let's summarize the relevant knowledge:
    • First, let's take a look at how the iterator interface in Java is implemented:
    Package Java. util; public interface iterator <E> {Boolean hasnext (); // determine whether the next object element e next () exists; // obtain the next element void remove (); // remove element}
    • Iterable:
      1. Java also provides an iterable interface. After the iterable interface is implemented, it returns an iterator.
      2. Implement with code:
      Package  java.lang;import   java.util.Iterator;public interface Iterable<T> {Iterator<T> iterator();}
    • It can also be used to traverse elements in a set.
    Iterator<Book> itr = myList.iterator();while(ite.hasnext());System.out.println(itr.next());
    You can also use the for -- each loop:
    Java for (Book: mylist) // these two methods are the same. System. Out. println (book );
    • It is worth noting that the iterator time-consuming cannot delete the elements in the set!
      • When using iterator, you cannot change the size and structure of the traversed containers. For example, if you perform add or remove operations on the set during iteration using iterator, A concurrentmodificationexception occurs.
      • Because before you iterate, The iterator has been passed list. itertor () is created. If the list is modified in the iteration process, Java will give an exception. Because at this time the iterator object cannot actively synchronize the changes made by the list, Java will think that this operation is not thread safe, and will give a goodwill reminder (throw concurrentmodificationexception exception)
Issues and Solutions in code debugging
  • Question 1: xxxxxx
  • Problem 1 solution: xxxxxx
  • Question 2: xxxxxx
  • Problem 2 solution: xxxxxx
  • ...
Code hosting

Summary of last week's exam errors
  • Error 1:
    _________ Traversal means visit the left child, then the node, then the right child.
    A. preorder
    B. postorder
    C. Inorder
    D. Level-order
  • Understanding:
    • Sequencing in the middle: first access the left child, then the node, and then any node.
    • Subsequent sorting: The child accessing the node, and then the node.
    • It may be a bit wrong. ····
  • Error 2: The simulated link strategy does not allow array positions to be allocated contiguously regardless of the completeness of the tree.
    A. True
    B. False
  • Incorrect Question 2: Simulate the linked list policy using arrays to continuously allocate array locations. I thought that the test site is the integrity of the back end. I didn't see that the front cannot be consecutively allocated array positions.
Comment template: a question or question worth learning in a blog or code:
-The content is full and the textbook knowledge is fully explained. -You can gain insight into concepts and improve yourself. -But the reference is a bit perfunctory.
  • Study of this week's pairing
    • 20172310
    • Peer learning content
      • Chapter 10: Tree
      • Chapter 7: iterator;
  • ...

    Others (perception, thinking, etc., optional)

    This chapter tree is very difficult for me, and many code in the book cannot be understood. I learned a lot after asking questions and checking the information. There is also the Chapter of the iterator that has not been read before. Now let's take a look. Although I had a lot of homework this week, I felt very full.

Learning progress bar (7200 as of last semester)
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 260/0 1/1 05/05
Week 2 300/560 1/2 13/18
Week 3 212/772 1/4 21/39
Week 4 330/1112 2/7 21/60
Week 5 1321/2433 1/8 30/90
Week 6 1024/3457 1/9 20/110
  • 1. Review the Data Structure
  • 2. understand in depth how the iterator accesses Elements
  • 3. markdown format

2017-2018 Chapter 6 study summary

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.