20,182,311 2019-2020-1 "data structures and object-oriented programming" Week 9 learning summary

text

20,182,311 2019-2020-1 "data structures and object-oriented programming" Week 9 learning summary

Learning content summary

Textbook Chapter 15

  • Overview tree
    1. Definition: tree is a result of non-linear elements in a hierarchical organization. It contains nodes and edges connecting nodes, the highest level of the tree is the root node of the root, the root node with respect to the presentation layer node. Node in the tree does not contain child node is a leaf node, the other nodes except for the root node and the leaf nodes are the nodes.
    2. Order tree: the sub-node containing the maximum number of nodes.
    3. Path length: two number of nodes connected to the edges. Height: length of the longest path from the root node to the leaf node.
    4. n-ary tree: any number of child nodes of a node is no more than n
    5. Balance: all leaf nodes are at the same level
    6. Trees are left in the tree balanced, and the bottom of the leaf nodes: completely
  • Tree implementation:
    1. Array implementation strategy: an array implemented using a tree, an array subscript of the left subtree of the labeled node n to 2n, the right subtree labeled 2n + 1. The idea is simple, rapid access, but the array will allocate space for the tree position without data, waste of storage space.
    2. Analog connection policies: children into the array index in the array, in order to save space will increase to delete elements in the tree, the array elements of the cost shift.
    3. List implementation: the list is the most common method of making tree, binary node list may be connected around, trigeminal parent node may point list, the list can point to multi-branch on one or more siblings
  • Traversing the tree: recursive traversal simpler than non-recursive traversal, a reference sequence can be written to traverse other traversal order.
    • Preorder traversal: first root node , after the left subtree, then the right subtree
    • Preorder: first the left subtree, the root node , then the right subtree
    • Postorder: left subtree first, the right subtree, then the root node

    • Non-recursive traversal example (first order):
    public void preorderTraverse(){
          //非递归实现先序遍历
          Node t=root;
          Node top;
              Stack<Node> stack=new Stack<Node>();
              while(t!=null||!stack.isEmpty()){
                  while(t!=null){
                      System.out.print(t.getData()+"\t");
                      stack.push(t);
                      t=t.getLeft();
                  }
                  top=(Node)stack.peek();
                  stack.pop();
                  t=top.getRight();
              }
          }
    • Example recursive traversal (first order):
    public static void preOrderRe(Node t)
      {//递归实现前序遍历
          System.out.print(t.getData()+"\t");
          Node leftTree = t.getLeft();
          if(leftTree != null){
              preOrderRe(leftTree);
          }
          Node rightTree = t.getRight();
          if(rightTree != null){
              preOrderRe(rightTree);
          }
      }
    • Non-recursive traversal example (in order):
    public void midorderTraverse(){
          //非递归实现中序遍历
          Node t=root;
          Stack<Node> stack=new Stack<Node>();
          while(t!=null||!stack.isEmpty()){
              while(t!=null){
                  stack.push(t);
                  t=t.getLeft();
              }
              t=(Node)stack.peek();
              stack.pop();
              System.out.print(t.getData()+"\t");
              t=t.getRight();
          }
      }
    
    • Example recursive traversal (in order):
     public static void midOrderRe(Node t)
      {//递归实现中序遍历
          Node leftTree = t.getLeft();
          if(leftTree != null){
              midOrderRe(leftTree);
          }
          System.out.print(t.getData()+"\t");
          Node rightTree = t.getRight();
          if(rightTree != null){
              midOrderRe(rightTree);
          }
      }
    • Examples of non-recursive traversal (subsequent):
    public void lasorderTraverse(){
          //非递归实现后序遍历
          Node t=root;
          Node top,last=null;
          Stack<Node> stack=new Stack<Node>();
          while(t!=null||!stack.isEmpty()){
              while(t!=null){
                  stack.push(t);
                  t=t.getLeft();
              }
              top=(Node)stack.peek();
              if(top.getRight()==null||top.getRight()==last){
                  stack.pop();
                  System.out.print(top.getData()+"\t");
                  last=top;
              }
              else{
                  t=top.getRight();
              }
          }
      }
    • Example recursive traversal (subsequent):
    public static void lasOrderRe(Node t)
      {//递归实现后序遍历
          Node leftTree = t.getLeft();
          if(leftTree != null){
              lasOrderRe(leftTree);
          }
          Node rightTree = t.getRight();
          if(rightTree != null){
              lasOrderRe(rightTree);
          }
          System.out.print(t.getData()+"\t");
      }
    • Traversal sequence:
    public void seqTraverse(Node t){
          //利用队列先进先出实现层级遍历
          Queue<Node> queue=new LinkedList<Node>();
          queue.add(t);
          while(!queue.isEmpty()){
              Node q=queue.peek();
              queue.remove();
              if(q.getLeft()!=null){
                  queue.add(q.getLeft());
              }
              if(q.getRight()!=null){
                  queue.add(q.getRight());
              }
              System.out.print(q.getData()+"\t");
          }
      }
  • 决策树:基于链树实现,将决策信息填入结点中,定义一个访问方法,设置判定条件为输入n即访问左子树,输入y即访问右子树。

教材第16章

  • 二叉查找树的概述:
    • 定义:二叉查找树在二叉树的基础上规定了,左孩子小于父节点,父节点又小于等于右孩子。
    • 具体过程:

  • 一些方法:addElement、removeElement(不能简单的删除节点的相关引用指针,而是要寻找替代点),replacement(寻找替代点的方法,若结点没有孩子,返回空;若结点只有一个孩子,返回这个孩子;若结点有两个孩子,返回他的后继/前继结点),后继就是按顺序排列后他的后面一位,前继就是按顺序排列后他的前面一位。这两个结点一定没有孩子,否则就不是前继/后继结点(分别位于左子树的最右边和右子树的最左边)
  • 一些异常:树中元素不是Comparable,addElemnt会抛出NoComparableElementException异常。

教材学习中的问题和解决过程

  • 问题1:怎样实现从子结点返回到根结点?或者说建立虚空结点树的时候,遇到#返回null以后怎么回到根节点重新赋值?
  • 问题1解决方案:尝试了两种方法,一种是建立三叉链表,能实现就是有点麻烦。递归的思想其实就包括返回上一级,所以直接使用递归就能

代码调试中的问题和解决过程

  • 问题1:返回空指针异常和0>1错误

  • 问题1解决方案:习惯规定search方法未找到元素返回-1,造成copyOf里面会出现(0,1)的情况。把未找到元素的返回值设置为1即可。

代码托管

结对及互评

点评过的同学博客和代码

学习进度条

代码行数(新增/累积) 博客量(新增/累积) 学习时间(新增/累积) 重要成长
目标 10000行 30篇 400小时
第一周 246/246 2/2 30/30 初步掌握linux命令、java小程序和jdb调试
第二周 73/319 3/3 30/60
第三周 906/1225 3/6 20/80
第四周 748/1973 2/8 20/100
第五周 849/2822 2/10 20/120
第六周 962/ 3784 2/12 30/150
第七周 1883/5668 3/15 50/200
第八周 579/6247 1/16 30/230
第九周

Guess you like

Origin www.cnblogs.com/lengchong/p/11873918.html