Skip to content

Commit 3b90cf6

Browse files
committed
small changes
1 parent bfe5f8b commit 3b90cf6

File tree

76 files changed

+605
-606
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+605
-606
lines changed

0-1_knapsack.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,22 @@ int knapsack(int w, const vector<Item>& items) {
4444

4545
void small_test() {
4646
// The example in the book.
47-
vector<Item> items = {{20, 65}, {8, 35}, {60, 245}, {55, 195}, {40, 65}, {70, 150}, {85, 275}, {25, 155}, {30, 120}, {65, 320}, {75, 75}, {10, 40}, {95, 200}, {50, 100}, {40, 220}, {10, 99}};
47+
vector<Item> items = {{20, 65},
48+
{8, 35},
49+
{60, 245},
50+
{55, 195},
51+
{40, 65},
52+
{70, 150},
53+
{85, 275},
54+
{25, 155},
55+
{30, 120},
56+
{65, 320},
57+
{75, 75},
58+
{10, 40},
59+
{95, 200},
60+
{50, 100},
61+
{40, 220},
62+
{10, 99}};
4863
assert(695 == knapsack(130, items));
4964
}
5065

2-exists.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ bool DFS(GraphVertex* cur, const GraphVertex* pre);
2020

2121
// @include
2222
struct GraphVertex {
23-
enum Color {
24-
white,
25-
gray,
26-
black
27-
} color;
23+
enum Color { white, gray, black } color;
2824
vector<GraphVertex*> edges;
2925
// @exclude
3026
GraphVertex() : color(white) {}

Add_operators_in_string.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,8 @@ using std::string;
1717
using std::uniform_int_distribution;
1818
using std::vector;
1919

20-
bool exp_synthesis_helper(const vector<int>& A,
21-
int k,
22-
list<int>* operand_list,
23-
list<char>* oper_list,
24-
int cur,
25-
int level);
20+
bool exp_synthesis_helper(const vector<int>& A, int k, list<int>* operand_list,
21+
list<char>* oper_list, int cur, int level);
2622
int remaining_int(const vector<int>& A, int idx);
2723
int evaluate(list<int> operand_list, const list<char>& oper_list);
2824

@@ -38,12 +34,9 @@ void exp_synthesis(const vector<int>& A, int k) {
3834
}
3935
}
4036

41-
bool exp_synthesis_helper(const vector<int>& A,
42-
int k,
43-
list<int>* operand_list,
44-
list<char>* oper_list,
45-
int cur,
46-
int level) {
37+
bool exp_synthesis_helper(const vector<int>& A, int k,
38+
list<int>* operand_list, list<char>* oper_list,
39+
int cur, int level) {
4740
cur = cur * 10 + A[level];
4841
if (level == A.size() - 1) {
4942
operand_list->emplace_back(cur);
@@ -123,7 +116,8 @@ void small_test() {
123116
exp_synthesis(A, k);
124117
list<int> golden_operand_res = {123, 2, 5, 3, 7, 85, 9};
125118
assert(golden_operand_res.size() == operand_res.size());
126-
assert(equal(operand_res.begin(), operand_res.end(), golden_operand_res.begin()));
119+
assert(equal(operand_res.begin(), operand_res.end(),
120+
golden_operand_res.begin()));
127121
list<char> golden_oper_res = {'+', '+', '*', '*', '+', '*'};
128122
assert(golden_oper_res.size() == oper_res.size());
129123
assert(equal(oper_res.begin(), oper_res.end(), golden_oper_res.begin()));

BST_lowest_common_ancestor.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ int main(int argc, char* argv[]) {
3030
// 3
3131
// 2 5
3232
// 1 4 6
33-
unique_ptr<BSTNode<int>> root =
34-
unique_ptr<BSTNode<int>>(new BSTNode<int>{3});
33+
unique_ptr<BSTNode<int>> root = unique_ptr<BSTNode<int>>(new BSTNode<int>{3});
3534
root->left = unique_ptr<BSTNode<int>>(new BSTNode<int>{2});
3635
root->left->left = unique_ptr<BSTNode<int>>(new BSTNode<int>{1});
3736
root->right = unique_ptr<BSTNode<int>>(new BSTNode<int>{5});

BST_prototype_shared_ptr.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) 2013 Elements of Programming Interviews. All rights reserved.
22

3-
#ifndef SOLUTIONS_BST_PROTOTYPE_H_
4-
#define SOLUTIONS_BST_PROTOTYPE_H_
3+
#ifndef SOLUTIONS_BST_PROTOTYPE_SHARED_PTR_H_
4+
#define SOLUTIONS_BST_PROTOTYPE_SHARED_PTR_H_
55

66
#include <memory>
77

@@ -14,4 +14,4 @@ struct BSTNode {
1414
shared_ptr<BSTNode<T>> left, right;
1515
};
1616
// @exclude
17-
#endif // SOLUTIONS_BST_PROTOTYPE_H_
17+
#endif // SOLUTIONS_BST_PROTOTYPE_SHARED_PTR_H_

BST_sorted_order.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ using std::vector;
1717
vector<int> result;
1818

1919
// @include
20-
void print_BST_in_sorted_order(const unique_ptr<BSTNode<int>>& n) {
20+
void print_BST_in_sorted_order(const unique_ptr<BSTNode<int>>& r) {
2121
stack<const BSTNode<int>*> s;
22-
const BSTNode<int>* curr = n.get();
22+
const BSTNode<int>* curr = r.get();
2323

2424
while (!s.empty() || curr) {
2525
if (curr) {
@@ -44,16 +44,11 @@ int main(int argc, char* argv[]) {
4444
// 1 4 6
4545
unique_ptr<BSTNode<int>> root =
4646
unique_ptr<BSTNode<int>>(new BSTNode<int>{3, nullptr});
47-
root->left =
48-
unique_ptr<BSTNode<int>>(new BSTNode<int>{2, nullptr});
49-
root->left->left =
50-
unique_ptr<BSTNode<int>>(new BSTNode<int>{1, nullptr});
51-
root->right =
52-
unique_ptr<BSTNode<int>>(new BSTNode<int>{5, nullptr});
53-
root->right->left =
54-
unique_ptr<BSTNode<int>>(new BSTNode<int>{4, nullptr});
55-
root->right->right =
56-
unique_ptr<BSTNode<int>>(new BSTNode<int>{6, nullptr});
47+
root->left = unique_ptr<BSTNode<int>>(new BSTNode<int>{2, nullptr});
48+
root->left->left = unique_ptr<BSTNode<int>>(new BSTNode<int>{1, nullptr});
49+
root->right = unique_ptr<BSTNode<int>>(new BSTNode<int>{5, nullptr});
50+
root->right->left = unique_ptr<BSTNode<int>>(new BSTNode<int>{4, nullptr});
51+
root->right->right = unique_ptr<BSTNode<int>>(new BSTNode<int>{6, nullptr});
5752
// should output 1 2 3 4 5 6
5853
print_BST_in_sorted_order(root);
5954
vector<int> golden_res = {1, 2, 3, 4, 5, 6};

BST_to_sorted_doubly_list.cpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,43 @@ using std::make_shared;
1313
using std::numeric_limits;
1414
using std::shared_ptr;
1515

16+
shared_ptr<BSTNode<int>> BST_to_doubly_list_helper(
17+
const shared_ptr<BSTNode<int>>& T);
18+
1619
// @include
17-
// Transform a BST into a circular sorted doubly linked list in-place,
18-
// return the head of the list.
1920
shared_ptr<BSTNode<int>> BST_to_doubly_list(
20-
const shared_ptr<BSTNode<int>>& n) {
21+
const shared_ptr<BSTNode<int>>& T) {
22+
auto res = BST_to_doubly_list_helper(T);
23+
res->left->right = nullptr; // breaks the link from tail to head.
24+
res->left = nullptr; // breaks the link from head to tail.
25+
return res;
26+
}
27+
28+
// Transforms a BST into a circular sorted circular doubly linked list
29+
// in-place, and return the head of the list.
30+
shared_ptr<BSTNode<int>> BST_to_doubly_list_helper(
31+
const shared_ptr<BSTNode<int>>& T) {
2132
// Empty subtree.
22-
if (!n) {
33+
if (!T) {
2334
return nullptr;
2435
}
2536

2637
// Recursively build the list from left and right subtrees.
27-
auto l_head(BST_to_doubly_list(n->left));
28-
auto r_head(BST_to_doubly_list(n->right));
38+
auto l_head(BST_to_doubly_list_helper(T->left));
39+
auto r_head(BST_to_doubly_list_helper(T->right));
2940

30-
// Append n to the list from left subtree.
41+
// Append T to the list from left subtree.
3142
shared_ptr<BSTNode<int>> l_tail = nullptr;
3243
if (l_head) {
3344
l_tail = l_head->left;
34-
l_tail->right = n;
35-
n->left = l_tail;
36-
l_tail = n;
45+
l_tail->right = T;
46+
T->left = l_tail;
47+
l_tail = T;
3748
} else {
38-
l_head = l_tail = n;
49+
l_head = l_tail = T;
3950
}
4051

41-
// Append the list from right subtree to n.
52+
// Append the list from right subtree to T.
4253
shared_ptr<BSTNode<int>> r_tail = nullptr;
4354
if (r_head) {
4455
r_tail = r_head->left;
@@ -71,6 +82,6 @@ int main(int argc, char* argv[]) {
7182
cout << curr->data << endl;
7283
pre = curr->data;
7384
curr = curr->right;
74-
} while (curr != L);
85+
} while (curr);
7586
return 0;
7687
}

Balanced_binary_tree.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,29 @@ using std::endl;
1313
using std::max;
1414
using std::unique_ptr;
1515

16-
int get_height(const unique_ptr<BinaryTreeNode<int>>& n);
16+
int get_height(const unique_ptr<BinaryTreeNode<int>>& T);
1717

1818
// @include
19-
bool is_balanced_binary_tree(const unique_ptr<BinaryTreeNode<int>>& n) {
20-
return get_height(n) != -2;
19+
bool is_balanced_binary_tree(const unique_ptr<BinaryTreeNode<int>>& T) {
20+
return get_height(T) != -2;
2121
}
2222

23-
int get_height(const unique_ptr<BinaryTreeNode<int>>& n) {
24-
if (!n) {
23+
int get_height(const unique_ptr<BinaryTreeNode<int>>& T) {
24+
if (!T) {
2525
return -1; // base case.
2626
}
2727

28-
int l_height = get_height(n->left);
28+
int l_height = get_height(T->left);
2929
if (l_height == -2) {
3030
return -2; // left subtree is not balanced.
3131
}
32-
int r_height = get_height(n->right);
32+
int r_height = get_height(T->right);
3333
if (r_height == -2) {
3434
return -2; // right subtree is not balanced.
3535
}
3636

3737
if (abs(l_height - r_height) > 1) {
38-
return -2; // current node n is not balanced.
38+
return -2; // current node T is not balanced.
3939
}
4040
return max(l_height, r_height) + 1; // return the height.
4141
}
@@ -51,8 +51,10 @@ int main(int argc, char* argv[]) {
5151
root->left = unique_ptr<BinaryTreeNode<int>>(new BinaryTreeNode<int>());
5252
root->left->left = unique_ptr<BinaryTreeNode<int>>(new BinaryTreeNode<int>());
5353
root->right = unique_ptr<BinaryTreeNode<int>>(new BinaryTreeNode<int>());
54-
root->right->left = unique_ptr<BinaryTreeNode<int>>(new BinaryTreeNode<int>());
55-
root->right->right = unique_ptr<BinaryTreeNode<int>>(new BinaryTreeNode<int>());
54+
root->right->left =
55+
unique_ptr<BinaryTreeNode<int>>(new BinaryTreeNode<int>());
56+
root->right->right =
57+
unique_ptr<BinaryTreeNode<int>>(new BinaryTreeNode<int>());
5658
assert(is_balanced_binary_tree(root) == true);
5759
cout << boolalpha << is_balanced_binary_tree(root) << endl;
5860
// Non-balanced binary tree test.

Binary_search_unknown_length.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ using std::uniform_int_distribution;
1515
using std::vector;
1616

1717
// @include
18-
int binary_search_unknown_len(const vector<int> &A, int k) {
18+
int binary_search_unknown_len(const vector<int>& A, int k) {
1919
// Find the possible range where k exists.
2020
int p = 0;
2121
while (true) {
@@ -63,7 +63,7 @@ void small_test() {
6363
assert(binary_search_unknown_len(A, 4) == -1);
6464
}
6565

66-
int main(int argc, char *argv[]) {
66+
int main(int argc, char* argv[]) {
6767
small_test();
6868
int n, k;
6969
default_random_engine gen((random_device())());

Binary_tree_level_order.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ using std::queue;
1212
using std::unique_ptr;
1313

1414
// @include
15-
void print_binary_tree_level_order(const unique_ptr<BinaryTreeNode<int>>& n) {
16-
// Prevent empty tree.
17-
if (!n) {
15+
void print_binary_tree_level_order(const unique_ptr<BinaryTreeNode<int>>& r) {
16+
// Prevents empty tree.
17+
if (!r) {
1818
return;
1919
}
2020

2121
queue<BinaryTreeNode<int>*> q;
22-
q.emplace(n.get());
22+
q.emplace(r.get());
2323
size_t count = q.size();
2424
while (!q.empty()) {
2525
cout << q.front()->data << ' ';
@@ -30,7 +30,7 @@ void print_binary_tree_level_order(const unique_ptr<BinaryTreeNode<int>>& n) {
3030
q.emplace(q.front()->right.get());
3131
}
3232
q.pop();
33-
if (--count == 0) {
33+
if (--count == 0) { // Finish printing nodes in the current level.
3434
cout << endl;
3535
count = q.size();
3636
}
@@ -42,18 +42,18 @@ int main(int argc, char* argv[]) {
4242
// 3
4343
// 2 5
4444
// 1 4 6
45-
unique_ptr<BinaryTreeNode<int>> root =
46-
unique_ptr<BinaryTreeNode<int>>(new BinaryTreeNode<int>{3, nullptr, nullptr});
47-
root->left =
48-
unique_ptr<BinaryTreeNode<int>>(new BinaryTreeNode<int>{2, nullptr, nullptr});
49-
root->left->left =
50-
unique_ptr<BinaryTreeNode<int>>(new BinaryTreeNode<int>{1, nullptr, nullptr});
51-
root->right =
52-
unique_ptr<BinaryTreeNode<int>>(new BinaryTreeNode<int>{5, nullptr, nullptr});
53-
root->right->left =
54-
unique_ptr<BinaryTreeNode<int>>(new BinaryTreeNode<int>{4, nullptr, nullptr});
55-
root->right->right =
56-
unique_ptr<BinaryTreeNode<int>>(new BinaryTreeNode<int>{6, nullptr, nullptr});
45+
unique_ptr<BinaryTreeNode<int>> root = unique_ptr<BinaryTreeNode<int>>(
46+
new BinaryTreeNode<int>{3, nullptr, nullptr});
47+
root->left = unique_ptr<BinaryTreeNode<int>>(
48+
new BinaryTreeNode<int>{2, nullptr, nullptr});
49+
root->left->left = unique_ptr<BinaryTreeNode<int>>(
50+
new BinaryTreeNode<int>{1, nullptr, nullptr});
51+
root->right = unique_ptr<BinaryTreeNode<int>>(
52+
new BinaryTreeNode<int>{5, nullptr, nullptr});
53+
root->right->left = unique_ptr<BinaryTreeNode<int>>(
54+
new BinaryTreeNode<int>{4, nullptr, nullptr});
55+
root->right->right = unique_ptr<BinaryTreeNode<int>>(
56+
new BinaryTreeNode<int>{6, nullptr, nullptr});
5757
// should output 3
5858
// 2 5
5959
// 1 4 6

Binary_tree_to_doubly_linked_list.cpp

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,23 @@
22

33
using namespace std;
44

5-
class BinaryTree {
6-
public:
7-
int data;
8-
BinaryTree *left, *right;
9-
BinaryTree(
10-
int val = 0,
11-
BinaryTree* l = NULL,
12-
BinaryTree* r = NULL)
5+
struct BinaryTree {
6+
int data;
7+
BinaryTree* left, *right;
8+
BinaryTree(int val = 0, BinaryTree* l = nullptr, BinaryTree* r = nullptr)
139
: data(val), left(l), right(r) {}
1410
};
1511

1612
// @include
1713
BinaryTree* convert_tree_to_doubly_list(BinaryTree* n) {
18-
if (n == NULL) {
19-
return NULL;
14+
if (n == nullptr) {
15+
return nullptr;
2016
}
2117

2218
BinaryTree* L = convert_tree_to_doubly_list(n->left);
2319
BinaryTree* R = convert_tree_to_doubly_list(n->right);
2420
// Join L and n as a doubly linked list
25-
BinaryTree* L_tail = NULL;
21+
BinaryTree* L_tail = nullptr;
2622
if (L) {
2723
L_tail = L->left;
2824
L_tail->right = n, n->left = L_tail;
@@ -32,7 +28,7 @@ BinaryTree* convert_tree_to_doubly_list(BinaryTree* n) {
3228
}
3329

3430
// Join L and R as a doubly linked list
35-
BinaryTree* R_tail = NULL;
31+
BinaryTree* R_tail = nullptr;
3632
if (R) {
3733
R_tail = R->left;
3834
L_tail->right = R, R->left = L_tail;
@@ -44,19 +40,19 @@ BinaryTree* convert_tree_to_doubly_list(BinaryTree* n) {
4440
}
4541
// @exclude
4642

47-
int main(int argc, char *argv[]) {
43+
int main(int argc, char* argv[]) {
4844
// 3
4945
// 2 5
5046
// 1 4 6
51-
BinaryTree *root = new BinaryTree(3);
47+
BinaryTree* root = new BinaryTree(3);
5248
root->left = new BinaryTree(2);
5349
root->left->left = new BinaryTree(1);
5450
root->right = new BinaryTree(5);
5551
root->right->left = new BinaryTree(4);
5652
root->right->right = new BinaryTree(6);
5753
// should output 1, 2, 3, 4, 5, 6
58-
BinaryTree *head = convert_tree_to_doubly_list(root);
59-
BinaryTree *temp = head;
54+
BinaryTree* head = convert_tree_to_doubly_list(root);
55+
BinaryTree* temp = head;
6056
do {
6157
cout << temp->data << endl;
6258
temp = temp->right;

0 commit comments

Comments
 (0)