20172301 Summary of the seventh week of program design and data structure

Source: Internet
Author: User
Tags comparable
20172301 program design and data structure 7 learning summary teaching material Learning Content summary
  • Binary Search Tree isInclude additional attributesBinary Tree, itsThe left child is less than the parent node, and the parent node is less than or equal to the right child.
Use a linked list to implement a binary search tree
  • Addelement operation: based on the value of the given element, add the element to the appropriate position in the tree.
    • If the element is not comparable, an exception is thrown.
    • Tree is empty: the new element becomes the root node.
    • Non-empty tree: Comparison between new elements and root elements
      • Less than: If the left child of the root is empty, it becomes the left child of the root. If the left child is not empty, traverse and Add.
      • Greater than: if the right child of the root is empty, it becomes the right child of the root. If the right child is not empty, traverse and Add.
  • Removeelement operation: deletes the specified comparable element from the binary search tree. If no comparable element is found, an exception is thrown.
    • Select three scenarios for replacing a node:
      (1) If the deleted node has no children, the return value of replacement is null;
      (2) The deleted node has a child, and the child is returned by replacement;
      (3) The deleted node has two children, and the replacement returns the Middle-order successor (in the right subtree of the root node)
  • Removealloccurrences operation: removes all existing elements from the binary search tree.
    • Method usedLinkedBinaryTreeClasscontainsMethod.
  • Removemin operation:
    • Possible condition of the minimum element in the binary search tree:
      (1) If the root has no left child, the root is the smallest element, and the right child becomes a new root node;
      (2) The leftmost node of the tree is a leaf. The leaf is the smallest element and the left child application of its parent node is set to null;
      (3) The leftmost node of the tree is an internal node, and the left child of its parent node is set to reference the right child pointing to the smallest element.
Problems and Solutions in teaching material Learning
  • Question 1: understanding of the successor in the middle order on the p228 page of the book.
  • Problem 1 solution:
    • The so-called middle-order successor means:Traverse the successor node of a binary tree node in the middle order
    • How do I find the successor in the middle order?
      • If the right subtree is not empty, locate the leftmost leaf node of the right subtree;
      • If the right subtree is empty and has the right Father's Day, find the right Father's Day;
      • If the right subtree is empty and has the left Father's Day, find the nearest right ancestor node;
    • ForWhen you delete a node with two children, the replacement does not necessarily return the Middle-order successor. You can also return the relay precursor.You need to view the code implementation instead of limiting it to books.
    • How do I find the precursor in the middle order?
      • If the left subtree is not empty, find the rightmost leaf node of the Left subtree;
      • If the left subtree is empty and has the left Father's Day, find the left Father's Day;
      • If the left subtree is empty and has the right Father's Day, find the nearest left parent node;
  • Question 2:
  • Problem 2 solution: xxxxxx
  • ...
Issues and Solutions in code debugging
  • Question 1: Do I need to define a new pointer class?AVLTreeNodeIn other words, the relationship between the AVL Tree, the binary search tree, And the binary tree implemented by the linked list.
  • Problem 1 solution:
    • First, according to p240 in the book

      Because the tree needs to be traced back, it is recommended that the AVL Tree contain a reference pointing to its parent node for each node.

    • The reason for this is that,The tree becomes unbalanced because of node insertion or deletion.Therefore, each time you perform these two operations, you need to update the balance factor and check the root node from the inserted or deleted node.Therefore, in addition to the pointer to the left and right children, we also need a pointer to the parent node.
    • Secondly, according to p239

      For each node in the tree, we track the height of the left and right subtree.

    • Therefore, the pointer class needsintType VariableheightTo get the height of the node.
    • After I implement the pointer classAVLTreeNodeAndLinkedAVLTreeAfter balancing method, I need to implement the add and delete methods. However,The only difference between the AVL Tree and the binary search tree is that if they are not balanced during addition or deletion, they must be rotated.Therefore, the AVL tree can inherit the binary search tree.
    • At this moment, I am actually in a misunderstanding. Pointer class I wroteAVLTreeNodeTherefore, we must inherit the binary tree pointer class.BinaryTreeNode. However, you don't have to bother yourself!
      Directly inBinaryTreeNodeBuilding a new constructor is not enough!
    public BinaryTreeNode(T obj, LinkedBinaryTree<T> left, LinkedBinaryTree<T> right,int height)`
    • Problems:
      Although I have accurately understood the rotation and balancing operations in the AVL tree, I have not fully understood the relationship between code and code. It took a lot of time to do nothing, and at the same time put yourself into a wrong cycle.
      If I directly find that the AVL Tree is a subclass of the binary search tree, I will not build a new pointer class.
      Therefore, to solve the code problem, we must first observe the problem and determine the overall architecture. This is the importance of UML class diagrams. Otherwise, even if the details are processed perfectly and the direction is wrong, the farther and farther you go.

      First design, consider all the situations, and then implement.

  • Question 2: sequence of linked list rotation methods.
  • Problem 2 solution: Right-handed is used as an example.
    • Right-handed operations are given based on book p238

      • Make the left child element of the root become a new root element.
      • Make the original root element the right child element of the new root.
      • Make the right child of the left child of the original tree root become the new left child of the original tree root.
    • So we can use the right-hand method, wherenodeIs the original root,node1Is the new root.
    node1 = node.left;node1.right = node;node.left = node1.right;
    • Then, add the update height operation. The new root element is returned.
    node.height = Math.max(height(node.left),height(node.right));node1.height = Math.max(height(node1.left),height(node1.right));return node1;
    • Run, the first thing that is thrown to me isStackOverflowErrorError.

    • This error is thrown when the Application Recursion is too deep and stack overflow occurs. That is to say, in the MethodDead Recursion. I have seen this problem in my blog last week.
  • ...

Code hosting

Summary of last week's exam errors

No mistakes last week. Excellent!

Peer and peer evaluation comments
  • Blog peer evaluation last week
    • 20172304
    • 20172328
Other learning progress bars
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 10/10
Week 2 610/610 1/2 20/30
Week 3 593/1230 1/3 18/48
Week 4 2011/3241 2/5 30/78
Week 5 956/4197 1/6 22/100
Week 6 2294/6491 2/8 20/120
Week 7 914/7405 1/9 20/140
References
  • The precursor and successor of Binary Trees

20172301 Summary of the seventh 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.