Skip to content

Commit 03745a1

Browse files
committed
finish ch3
1 parent c450851 commit 03745a1

35 files changed

+980569
-3
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,8 @@ Algorithms, 4th edition textbook code (using c++)
5656
| [3.2](https://algs4.cs.princeton.edu/31elementary/index.php#3.2) | [BinarySearchST.h](ch3/head/BinarySearchST.h) | binary search | [3.3](https://algs4.cs.princeton.edu/32bst/index.php#3.3) | [BST.h](ch3/head/BST.h) | binary search tree |
5757
| [3.4](https://algs4.cs.princeton.edu/33balanced/index.php#3.4) | [RedBlackBST.h](ch3/head/RedBlackBST.h) | red-black tree | [3.5](https://algs4.cs.princeton.edu/34hash/index.php#3.5) | [SeparateChainingHashST.h](ch3/head/SeparateChainingHashST.h) | separate chaining hash table |
5858
| [3.6](https://algs4.cs.princeton.edu/34hash/index.php#3.6) | [LinearProbingHashST.h](ch3/head/LinearProbingHashST.h) | linear probing hash table | [-](https://algs4.cs.princeton.edu/35applications/index.php#-) | [ST.h](ch3/head/ST.h) | ordered symbol table |
59+
| [-](https://algs4.cs.princeton.edu/35applications/index.php#-) | [SET.h](ch3/head/SET.h) | ordered set | [-](https://algs4.cs.princeton.edu/35applications/index.php#-) | [DeDup.cpp](ch3/10_DeDup/main.cpp) | remove duplicates |
60+
| [-](https://algs4.cs.princeton.edu/35applications/index.php#-) | [WhiteFilter.cpp](ch3/11_WhiteFilter/main.cpp) | whitelist filter | [-](https://algs4.cs.princeton.edu/35applications/index.php#-) | [BlackFilter.cpp](ch3/12_BlackFilter) | blacklist filter |
61+
| [-](https://algs4.cs.princeton.edu/35applications/index.php#-) | [LookupCSV.cpp](ch3/13_LookupCSV/main.cpp) | dictionary lookup | [-](https://algs4.cs.princeton.edu/35applications/index.php#-) | [LookupIndex.cpp](ch3/14_LookupIndex/main.cpp) | index and inverted index |
62+
| [-](https://algs4.cs.princeton.edu/35applications/index.php#-) | [FileIndex.cpp](ch3/15_FileIndex/main.cpp) | file indexing | [-](https://algs4.cs.princeton.edu/35applications/index.php#-) | [SparseVector.h](ch3/head/SparseVector.h) | sparse vector |
5963

ch3/10_DeDup/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(10_DeDup)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/ST.h ../head/SequentialSearchST.h ../head/BinarySearchST.h ../head/BST.h ../head/RedBlackBST.h ../head/SeparateChainingHashST.h ../head/LinearProbingHashST.h ../head/SET.h ../head/SparseVector.h)
7+
add_executable(10_DeDup ${SOURCE_FILES})

ch3/10_DeDup/main.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include "../head/SET.h"
4+
5+
using namespace std;
6+
7+
/**
8+
* The {@code DeDup} class provides a client for reading in a sequence of
9+
* words from standard input and printing each word, removing any duplicates.
10+
* It is useful as a test client for various symbol table implementations.
11+
* <p>
12+
* For additional documentation, see <a href="https://algs4.cs.princeton.edu/35applications">Section 3.5</a> of
13+
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
14+
*
15+
* @author Robert Sedgewick
16+
* @author Kevin Wayne
17+
*/
18+
int main() {
19+
SET<string> st;
20+
fstream file("/home/ace/AceDev/C++/algorithm/ch3/data/tinyTale.txt");
21+
string key;
22+
while (file >> key) {
23+
if (!st.contains(key)) {
24+
st.add(key);
25+
cout << key << endl;
26+
}
27+
}
28+
}

ch3/11_WhiteFilter/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(11_WhiteFilter)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/ST.h ../head/SequentialSearchST.h ../head/BinarySearchST.h ../head/BST.h ../head/RedBlackBST.h ../head/SeparateChainingHashST.h ../head/LinearProbingHashST.h ../head/SET.h)
7+
add_executable(11_WhiteFilter ${SOURCE_FILES})

ch3/11_WhiteFilter/main.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include "../head/SET.h"
4+
5+
using namespace std;
6+
7+
/**
8+
* The {@code WhiteFilter} class provides a client for reading in a <em>whitelist</em>
9+
* of words from a file; then, reading in a sequence of words from standard input,
10+
* printing out each word that appears in the file.
11+
* It is useful as a test client for various symbol table implementations.
12+
* <p>
13+
* For additional documentation, see <a href="https://algs4.cs.princeton.edu/35applications">Section 3.5</a> of
14+
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
15+
*
16+
* @author Robert Sedgewick
17+
* @author Kevin Wayne
18+
*/
19+
int main() {
20+
SET<string> st;
21+
fstream file("/home/ace/AceDev/C++/algorithm/ch3/data/tinyTale.txt");
22+
fstream filter("/home/ace/AceDev/C++/algorithm/ch3/data/list.txt");
23+
string key;
24+
while (filter >> key) {
25+
if (!st.contains(key)) {
26+
st.add(key);
27+
}
28+
}
29+
string tmp;
30+
while (getline(file, key)) {
31+
stringstream ss(key);
32+
while (ss >> tmp) {
33+
if (st.contains(tmp))
34+
cout << tmp << " ";
35+
}
36+
cout << endl;
37+
}
38+
}

ch3/12_BlackFilter/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(12_BlackFilter)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/ST.h ../head/SequentialSearchST.h ../head/BinarySearchST.h ../head/BST.h ../head/RedBlackBST.h ../head/SeparateChainingHashST.h ../head/LinearProbingHashST.h ../head/SET.h)
7+
add_executable(12_BlackFilter ${SOURCE_FILES})

ch3/12_BlackFilter/main.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include "../head/SET.h"
4+
5+
using namespace std;
6+
7+
/**
8+
* The {@code BlackFilter} class provides a client for reading in a <em>blacklist</em>
9+
* of words from a file; then, reading in a sequence of words from standard input,
10+
* printing out each word that <em>does not</em> appear in the file.
11+
* It is useful as a test client for various symbol table implementations.
12+
* <p>
13+
* For additional documentation, see <a href="https://algs4.cs.princeton.edu/35applications">Section 3.5</a> of
14+
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
15+
*
16+
* @author Robert Sedgewick
17+
* @author Kevin Wayne
18+
*/
19+
int main() {
20+
SET<string> st;
21+
fstream file("/home/ace/AceDev/C++/algorithm/ch3/data/tinyTale.txt");
22+
fstream filter("/home/ace/AceDev/C++/algorithm/ch3/data/list.txt");
23+
string key;
24+
while (filter >> key) {
25+
if (!st.contains(key)) {
26+
st.add(key);
27+
}
28+
}
29+
30+
string tmp;
31+
while (getline(file, key)) {
32+
stringstream ss(key);
33+
while (ss >> tmp) {
34+
if (!st.contains(tmp))
35+
cout << tmp << " ";
36+
}
37+
cout << endl;
38+
}
39+
}

ch3/13_LookupCSV/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(13_LookupCSV)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/ST.h ../head/SequentialSearchST.h ../head/BinarySearchST.h ../head/BST.h ../head/RedBlackBST.h ../head/SeparateChainingHashST.h ../head/LinearProbingHashST.h ../head/SET.h)
7+
add_executable(13_LookupCSV ${SOURCE_FILES})

ch3/13_LookupCSV/main.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <sstream>
4+
#include <vector>
5+
#include "../head/ST.h"
6+
7+
using namespace std;
8+
9+
/**
10+
* The {@code LookupCSV} class provides a data-driven client for reading in a
11+
* key-value pairs from a file; then, printing the values corresponding to the
12+
* keys found on standard input. Both keys and values are strings.
13+
* The fields to serve as the key and value are taken as command-line arguments.
14+
* <p>
15+
* For additional documentation, see <a href="https://algs4.cs.princeton.edu/35applications">Section 3.5</a> of
16+
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
17+
*
18+
* @author Robert Sedgewick
19+
* @author Kevin Wayne
20+
*/
21+
22+
auto split = [](string s, char delim) {
23+
vector<string> tokens;
24+
stringstream ss(s);
25+
string tmp;
26+
while (getline(ss, tmp, delim)) {
27+
if (tmp != "")
28+
tokens.push_back(tmp);
29+
}
30+
return tokens;
31+
};
32+
33+
int main() {
34+
int keyField = 0;
35+
int valField = 3;
36+
// symbol table
37+
ST<string, string> st;
38+
// read in the data from csv file
39+
fstream file("/home/ace/AceDev/C++/algorithm/ch3/data/amino.csv");
40+
string tmp;
41+
while (getline(file, tmp)) {
42+
vector<string> tokens = split(tmp, ',');
43+
string key = tokens[keyField];
44+
string val = tokens[valField];
45+
st.put(key, val);
46+
}
47+
48+
string s;
49+
cout << "Please input the seeking key: (-1 mains break)" << endl;
50+
while (cin >> s) {
51+
if (s == "-1")
52+
break;
53+
if (st.contains(s))
54+
cout << st.get(s) << endl;
55+
else
56+
cout << "Not found" << endl;
57+
}
58+
}

ch3/14_LookupIndex/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(14_LookupIndex)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/ST.h ../head/SequentialSearchST.h ../head/BinarySearchST.h ../head/BST.h ../head/RedBlackBST.h ../head/SeparateChainingHashST.h ../head/LinearProbingHashST.h ../head/SET.h)
7+
add_executable(14_LookupIndex ${SOURCE_FILES})

0 commit comments

Comments
 (0)