20172303 2018-2019-1 program design and data structure 7th weekly study summary

Source: Internet
Author: User
20172303 2018-2019-1 program design and data structure 7th weekly learning summary teaching material Learning Content summary

On the basis of learning binary trees last week, I learned a special form of Binary Tree, called binary tree and binary tree. This chapter describes the implementation of two binary search trees and their application.

I. Overview 1. Binary Search Tree
  • Concept: All nodes in the tree. The left child is smaller than the parent node, and the parent node is smaller than or equal to the right child.
  • Nature:
    • If the left subtree of any node is not empty, the value of all nodes on the left subtree is smaller than the value of its root node;
    • If the right subtree of any node is not empty, the value of all nodes on the right subtree is greater than the value of its root node;
    • The left and right subtree of any node are also binary lookup trees;
    • There are no nodes with equal elements.
2. ADT
  • The ADT of the Binary Search Tree is an extension of the Binary Tree discussed in the previous chapter. The operations in the ADT are complementary to the operations defined in the binary tree.
  • Operations in the binary search tree:
    • addElement: Add an element to the tree
    • removeElement: Delete an element from the tree
    • removeAllOccurrences: Deletes any existence of the specified element from the tree.
    • removeMin: Delete the smallest element in the tree
    • removeMax: Delete the largest element in the tree.
    • findMin: Returns the minimum elementReference
    • findMax: Returns the largest element in the tree.Reference
II. Implementation of the Binary Search Tree 1. Search
  • The method of finding the binary search tree is similar to that of the Binary Search Tree. The elements to be searched are compared with those of the root node. If the value is smaller than the root node, the search tree continues to be compared with the left child, if the root node is greater than the root node, the system continues to compare it with the right child. If the root node is equal, the system returns the element. There are two implementation methods: iteration and recursion.
  • Iterative implementation
public void find(T element){    T result = null;    BinaryTreeNode node = root;    while (node != null)    {         if (element.CompareTo(node.getElement) > 0)        {            node = node.right;        }        else if (element.CompareTo(node.getElement) < 0)        {            node = node.left;        }        else        {            result = node.getElement;            break;        }    }    return result;}
  • Recursive Implementation
public void find(T element){    return find(root, element);} private void find(BinaryTreeNode root, T element){    if (root == null) {        return element;    }        int comparable = element.CompareTo(root.getElement);    if (comparable > 0){        find(root.right,element);    }    else if (comparable < 0){        find(root.left,element);    }    else {        return root.getElement;    }}
2. Insert
  • There are three possible insert operations:
    • If the current Binary Search Tree is empty, the added element becomes the root node.
    • If the element of the inserted node is smaller than that of the root node:
      • If the left child of the root isnull, Insert the knot to become a new left child.
      • If the root left child is notnullThen, the left subtree will be traversed and compared at the same time.
    • If the element of the inserted node is greater than or equal to the element of the Root Node
      • If the right child of the root isnull, Insert the knot to become a new right child.
      • If the right child of the root is notnullThen, the system will continue to traverse the right subtree and perform comparison operations while traversing it.
3. Delete
  • The delete operation of the binary search tree is the most complex among all operations. Let's first consider a special situation:The deleted element is the maximum or minimum value in the tree..
(1) Special Case: The deleted element is the maximum or minimum value in the tree.
  • Because of the special form of the binary query tree, the minimum value is generally located in the left subtree of the tree, and the maximum value is located in the right subtree of the tree. The two deletion methods are similar. The only difference is "Left" and "right". The following example describes how to delete the maximum value, to delete the minimum value, you only need to swap the "Left" and "right" values in the example.
  • There are three conditions for deleting the maximum value:
    • Scenario 1: If the root node has no right child, the root node has the largest element, and the left child of the original root node becomes a new root node.
    • Case 2: If the maximum node is a leaf node, set the reference of the right child of the parent nodenullYou can.
    • Case 3: If the maximum node is an intermediate node, you need to set the reference of the right child of the parent node as the left child of the node.
(2) normal conditions
  • There are also three situations for deleting elements under normal circumstances, but these three cases are not that simple.
  • Scenario 1:
Problems and Solutions in teaching material Learning
  • Question 1:
  • Problem 1 solution:
  • Question 2:
  • Problem 2 solution:
  • Question 3:
  • Problem 3 solution:
Issues and Solutions in code debugging
  • Question 1:
  • Problem 1 solution:
  • Question 2:
  • Problem 2 solution:
Code hosting last week exam error Summary (correct green, error red)
  • Error 1: the Java collections API contains _________ implementations of an indexed list.
    • A. Two
    • B. Three
    • C. Four
    • D. Five
  • Solution: I originally understood that Java API provides several methods to implement the list. Therefore, either of them isArrayListThe other isLinkedList. Later I found that I had read the wrong question and did not see the word "Index". The original words are on the 120 page of the book.
  • Error 2: the elements of an unordered list are kept in whatever order the client chooses.
    • A. True
    • B. False
  • Incorrect Question 2 solution: When I was doing the question, I thought that the order of the unordered list was indeed determined by the user. Then I thought the error may be due to "whatever.
Peer and peer evaluation template:
  • Problems worth learning in a blog:
    • Advantage: this week's blog has made great strides! The rich content has finally been illustrated and illustrated, so it is worthy of praise!
    • Problem: The Image Layout needs to be enhanced.
  • Problems worth learning in the Code:
    • Advantage: after several weeks of submission, the commit has finally been improved. I feel that my partner has made a qualitative leap this week.It may have been repeated, but as Ma Yuan said, quantitative changes will lead to qualitative changes!
    • Question: there are not many comments on the code this week.

      Blog and code commented on by students
  • Study of this week's pairing
    • 20172322
    • Peer learning content
      • I have explained the ASL Calculation Method in the classroom experiment.
      • This article mainly discusses the counting method of Merge Sorting.
Others (perception, thinking, etc., optional)
  • I feel that my recent courses are always coming to everyone because of my cheerleading competition. Now, when my cheerleading competition is over, I have to catch up with everyone else (? °? ° ?)??
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 10/10 1/1 10/10
Week 2 246/366 2/3 20/30
Week 3 567/903 1/4 10/40
Week 4 2346/3294 2/6 20/60
Week 5 1343/4637 2/8 30/90
Week 6 1343/4637 2/8 30/90
Week 7 1343/4637 2/8 30/90
  • Planned learning time: 20 hours

  • Actual learning time: 30 hours

  • Improvement: most of the time of this week has basically been spent on understanding the search algorithm and sorting algorithm, and I feel more skillful in understanding the time complexity and computing applications.

References
  • Four GIF images help you understand the Binary Search Tree

  • Algorithm and data structure (7): Binary Search Tree

20172303 2018-2019-1 program design and data structure 7th weekly 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.