《算法》第三章部分程序 part 5

Posted cuancuancuanhao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《算法》第三章部分程序 part 5相关的知识,希望对你有一定的参考价值。

? 书中第三章部分程序,加上自己补充的代码,包含公共符号表、集合类型

● 公共符号表,用于普通查找表的基本类

  1 package package01;
  2 
  3 import java.util.NoSuchElementException;
  4 import java.util.TreeMap;
  5 import edu.princeton.cs.algs4.StdIn;
  6 import edu.princeton.cs.algs4.StdOut;
  7 
  8 public class class01<Key extends Comparable<Key>, Value> implements Iterable<Key>
  9 {
 10     private TreeMap<Key, Value> st;
 11 
 12     public class01()
 13     {
 14         st = new TreeMap<Key, Value>();
 15     }
 16 
 17     public Value get(Key key)
 18     {
 19         if (key == null)
 20             throw new IllegalArgumentException("
<get> key == null.
");
 21         return st.get(key);
 22     }
 23 
 24     public void put(Key key, Value val)
 25     {
 26         if (key == null)
 27             throw new IllegalArgumentException("
<put> key == null.
");
 28         if (val == null)
 29             st.remove(key);
 30         else
 31             st.put(key, val);
 32     }
 33 
 34     public void delete(Key key)
 35     {
 36         if (key == null)
 37             throw new IllegalArgumentException("
<delete> key == null.
");
 38         st.remove(key);
 39     }
 40 
 41     public boolean contains(Key key)
 42     {
 43         if (key == null)
 44             throw new IllegalArgumentException("
<contains> key == null.
");
 45         return st.containsKey(key);
 46     }
 47 
 48     public int size()
 49     {
 50         return st.size();
 51     }
 52 
 53     public boolean isEmpty()
 54     {
 55         return size() == 0;
 56     }
 57 
 58     public Iterable<Key> keys()
 59     {
 60         return st.keySet();
 61     }
 62 
 63     public Key min()
 64     {
 65         if (isEmpty())
 66             throw new NoSuchElementException("
<min> empty.
");
 67         return st.firstKey();
 68     }
 69 
 70     public Key max()
 71     {
 72         if (isEmpty())
 73             throw new NoSuchElementException("
<max> empty.
");
 74         return st.lastKey();
 75     }
 76 
 77     public Key ceiling(Key key)
 78     {
 79         if (key == null)
 80             throw new IllegalArgumentException("
<min> key == null.
");
 81         Key k = st.ceilingKey(key);
 82         if (k == null)
 83             throw new NoSuchElementException("
<min> k == null.
");
 84         return k;
 85     }
 86 
 87     public Key floor(Key key)
 88     {
 89         if (key == null)
 90             throw new IllegalArgumentException("
<min> key == null.
");
 91         Key k = st.floorKey(key);
 92         if (k == null)
 93             throw new NoSuchElementException("
<min> k == null.
");
 94         return k;
 95     }
 96 
 97     public static void main(String[] args)
 98     {
 99         class01<String, Integer> st = new class01<String, Integer>();
100         for (int i = 0; !StdIn.isEmpty(); i++)
101         {
102             String key = StdIn.readString();
103             st.put(key, i);
104         }
105         for (String s : st.keys())
106             StdOut.println(s + " " + st.get(s));
107     }
108 }

● 集合类型

  1 package package01;
  2 
  3 import java.util.NoSuchElementException;
  4 import java.util.Iterator;
  5 import java.util.TreeSet;
  6 import edu.princeton.cs.algs4.StdOut;
  7 
  8 public class class01<Key extends Comparable<Key>> implements Iterable<Key>
  9 {
 10     private TreeSet<Key> set;
 11 
 12     public class01()
 13     {
 14         set = new TreeSet<Key>();
 15     }
 16 
 17     public class01(class01<Key> x)
 18     {
 19         set = new TreeSet<Key>(x.set);
 20     }
 21 
 22     public void add(Key key)
 23     {
 24         if (key == null)
 25             throw new IllegalArgumentException("
<add> key == null.
");
 26         set.add(key);
 27     }
 28 
 29     public boolean contains(Key key)
 30     {
 31         if (key == null)
 32             throw new IllegalArgumentException("
<contains> key == null.
");
 33         return set.contains(key);
 34     }
 35 
 36     public void delete(Key key)
 37     {
 38         if (key == null)
 39             throw new IllegalArgumentException("
<delete> key == null.
");
 40         set.remove(key);
 41     }
 42 
 43     public int size()
 44     {
 45         return set.size();
 46     }
 47 
 48     public boolean isEmpty()
 49     {
 50         return size() == 0;
 51     }
 52 
 53     public Iterator<Key> iterator()
 54     {
 55         return set.iterator();
 56     }
 57 
 58     public Key max()
 59     {
 60         if (isEmpty())
 61             throw new NoSuchElementException("
<max> empty.
");
 62         return set.last();
 63     }
 64 
 65     public Key min()
 66     {
 67         if (isEmpty())
 68             throw new NoSuchElementException("
<min> key == null.
");
 69         return set.first();
 70     }
 71 
 72     public Key ceiling(Key key)
 73     {
 74         if (key == null)
 75             throw new IllegalArgumentException("
<ceiling> key == null.
");
 76         Key k = set.ceiling(key);
 77         if (k == null)
 78             throw new NoSuchElementException("
<ceiling> k == null.
");
 79         return k;
 80     }
 81 
 82     public Key floor(Key key)
 83     {
 84         if (key == null)
 85             throw new IllegalArgumentException("
<floor> key == null.
");
 86         Key k = set.floor(key);
 87         if (k == null)
 88             throw new NoSuchElementException("
<floor> k == null.
");
 89         return k;
 90     }
 91 
 92     public class01<Key> union(class01<Key> that)
 93     {
 94         if (that == null)
 95             throw new IllegalArgumentException("
<floor> key == null.
");
 96         class01<Key> c = new class01<Key>();
 97         for (Key x : this)
 98             c.add(x);
 99         for (Key x : that)
100             c.add(x);
101         return c;
102     }
103 
104     public class01<Key> intersects(class01<Key> that)
105     {
106         if (that == null)
107             throw new IllegalArgumentException("
<floor> key == null.
");
108         class01<Key> c = new class01<Key>();
109         if (this.size() < that.size())          // 遍历较小的集合,去较大的集合中匹配,无所谓?
110         {
111             for (Key x : this)
112             {
113                 if (that.contains(x))
114                     c.add(x);
115             }
116         }
117         else
118         {
119             for (Key x : that)
120             {
121                 if (this.contains(x))
122                     c.add(x);
123             }
124         }
125         return c;
126     }
127 
128     public boolean equals(Object other)
129     {
130         if (other == this)
131             return true;
132         if (other == null)
133             return false;
134         if (other.getClass() != this.getClass())
135             return false;
136         class01 that = (class01) other;
137         return this.set.equals(that.set);
138     }
139 
140     @Override
141     public int hashCode()
142     {
143         throw new UnsupportedOperationException("
<hashCode> hashCode() not supported,
");
144     }
145 
146     @Override
147     public String toString()                    // 把集合的元素放进大括号中列出
148     {
149         String s = set.toString();
150         return "{ " + s.substring(1, s.length() - 1) + " }";
151     }
152 
153     public static void main(String[] args)
154     {
155         class01<String> set = new class01<String>();
156         StdOut.println("set = " + set);     // 输出空集合
157 
158         set.add("www.cs.princeton.edu");    // 插入一些元素用于测试方法
159         set.add("www.cs.princeton.edu");
160         set.add("www.princeton.edu");
161         set.add("www.math.princeton.edu");
162         set.add("www.yale.edu");
163         set.add("www.amazon.com");
164         set.add("www.simpsons.com");
165         set.add("www.stanford.edu");
166         set.add("www.google.com");
167         set.add("www.ibm.com");
168         set.add("www.apple.com");
169         set.add("www.slashdot.com");
170         set.add("www.whitehouse.gov");
171         set.add("www.espn.com");
172         set.add("www.snopes.com");
173         set.add("www.movies.com");
174         set.add("www.cnn.com");
175         set.add("www.iitb.ac.in");
176 
177         StdOut.println(set.contains("www.cs.princeton.edu"));
178         StdOut.println(!set.contains("www.harvardsucks.com"));
179         StdOut.println();
180         StdOut.println("ceiling(www.simpsonr.com) = " + set.ceiling("www.simpsonr.com"));
181         StdOut.println("ceiling(www.simpsons.com) = " + set.ceiling("www.simpsons.com"));
182         StdOut.println("floor(www.simpsonr.com)   = " + set.floor("www.simpsonr.com"));
183         StdOut.println("floor(www.simpsons.com)   = " + set.floor("www.simpsons.com"));
184         StdOut.println();
185         StdOut.println("set = " + set);
186         StdOut.println();
187         for (String s : set)                // 直接列出表中元素
188             StdOut.println(s);
189         StdOut.println();
190         class01<String> set2 = new class01<String>(set);
191         StdOut.println(set.equals(set2));
192     }
193 }

 

以上是关于《算法》第三章部分程序 part 5的主要内容,如果未能解决你的问题,请参考以下文章

《算法》第三章部分程序 part 2

《算法》第三章部分程序 part 6

《算法》第六章部分程序 part 7

《算法》第五章部分程序 part 1

《算法》BEYOND 部分程序 part 3

《算法》BEYOND 部分程序 part 2