第4章 树

Posted tjj-love-world

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第4章 树相关的知识,希望对你有一定的参考价值。

 二叉查找树

  1 public class BinarySearchTree<AnyType extends Comparable<? super AnyType>>
  2 {
  3     private static class BinaryNode<AnyType> 
  4     {
  5         BinaryNode(AnyType theElement) 
  6         {
  7             this(theElement, null, null);
  8         }
  9 
 10         BinaryNode(AnyType theElement, BinaryNode<AnyType> lt, BinaryNode<AnyType> rt) 
 11         {
 12             element = theElement;
 13             left = lt;
 14             right = rt;
 15         }
 16 
 17         AnyType element;
 18         BinaryNode<AnyType> left;
 19         BinaryNode<AnyType> right;
 20     }
 21 
 22     private BinaryNode<AnyType> root;
 23 
 24     public BinarySearchTree() 
 25     {
 26         root = null;
 27     }
 28 
 29     public void makeEmpty() 
 30     {
 31         root = null;
 32     }
 33 
 34     public boolean isEmpty() 
 35     {
 36         return root == null;
 37     }
 38 
 39     public boolean contains(AnyType x)
 40     { return contains(x, root); }
 41     public AnyType findMin()
 42     {
 43         if (isEmpty()) throw new UnderflowException();
 44         return findMin(root).element;
 45     }
 46     public AnyType findMax()
 47     {
 48         if (isEmpty()) throw new UnderflowException();
 49         return findMax(root).element;
 50     }
 51     public void insert(AnyType x)
 52     {
 53         root = insert(x, root);
 54     }
 55     public void remove(AnyType x)
 56     {
 57         root = remove(x, root);
 58     }
 59 
 60     private boolean contains(AnyType x, BinaryNode<AnyType> t)
 61     {
 62         if (t == null)
 63             return false;
 64 
 65         int compareResult = x.compareTo(t.element);
 66 
 67         if (compareResult < 0)
 68             return contains(x, t.left);
 69         if (compareResult > 0)
 70             return contains(x, t.right);
 71         else
 72             return true;
 73     }
 74 
 75     private BinaryNode<AnyType> findMin(BinaryNode<AnyType> t)
 76     {
 77         if (t == null)
 78             return null;
 79         else if (t.left == null)
 80             return t;
 81         return findMin(t.left);
 82     }
 83 
 84     private BinaryNode<AnyType> findMax(BinaryNode<AnyType> t) 
 85     {
 86         if (t != null)
 87             while (t.right != null)
 88                 t = t.right;
 89 
 90         return t;
 91     }
 92 
 93     private BinaryNode<AnyType> insert(AnyType x, BinaryNode<AnyType> t)
 94     {
 95         if (t == null)
 96             return new BinaryNode<>(x, null, null);
 97 
 98         int compareResult = x.compareTo(t.element);
 99 
100         if (compareResult < 0)
101             t.left = insert(x, t.left);
102         if (compareResult > 0)
103             t.right = insert(x, t.right);
104         else
105             ;
106         return t;
107     }
108 
109     private BinaryNode<AnyType> remove(AnyType x, BinaryNode<AnyType> t)
110     {
111         if (t == null)
112             return t;
113 
114         int compareResult = x.compareTo(t.element);
115 
116         if (compareResult < 0)
117             t.left = remove(x, t.left);
118         else if (compareResult > 0)
119             t.right = remove(x, t.right);
120         else if (t.left != null && t.right != null)
121         {
122             t.element = findMin(t.right).element;
123             t.right = remove(t.element, t.right);
124         }
125         else
126             t = (t.left != null) ? t.left : t.right;
127         return t;
128     }
129 
130     public void printTree()
131     {
132         if (isEmpty())
133             System.out.println("Empty tree");
134         else
135             printTree(root);
136     }
137 
138     public void printTree(BinaryNode<AnyType> t)
139     {
140         if (t != null)
141         {
142             printTree(t.left);
143             System.out.println(t.element);
144             printTree(t.right);
145         }
146     }
147 }
1 public class UnderflowException extends RuntimeException {
2 }

 

懒惰删除:当一个元素要被删除时,它仍留在树中,而只是被标记为删除。

 

以上是关于第4章 树的主要内容,如果未能解决你的问题,请参考以下文章

《机器学习》(周志华)第4章 决策树 笔记 理论及实现——“西瓜树”——CART决策树

复现经典:《统计学习方法》第 7 章 支持向量机

数据挖掘导论 第4章 分类:基本概念决策树与模型评估

第4章 决策树理解记录

《机器学习》西瓜书习题 第 4 章

数据结构-王道2017-第4章 树与二叉树-树森林