Size: 5304 bytes.


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// cs/q/trees/view/from_right_test.cc
// cs/q/trees/view_from_right_test.cc
#include <string>
#include <vector>

#include "cs/q/queue/queue.hh"
#include "cs/q/trees/view.hh"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

using namespace cs::q::trees;
using ::testing::Eq;

// Helper: free tree memory
template <typename T>
void DeleteTree(Node<T>* root) {
  if (!root) return;
  DeleteTree(root->left);
  DeleteTree(root->right);
  delete root;
}

// Helper: drain queue into vector
template <typename T>
std::vector<T> QueueToVector(cs::q::queue::Queue<T> q) {
  std::vector<T> out;
  while (q.Size() > 0) {
    out.push_back(q.PopFront().value());
  }
  return out;
}

// Empty tree -> empty result
TEST(RightViewBFS, HandlesEmptyTree) {
  Node<int>* root = nullptr;
  auto q = RightViewBFS<int>(root);
  EXPECT_EQ(q.Size(), 0u);
  auto v = QueueToVector(std::move(q));
  EXPECT_TRUE(v.empty());
}

// Single node
TEST(RightViewBFS, SingleNode) {
  Node<int>* root = new Node<int>(42);
  auto q = RightViewBFS<int>(root);
  auto v = QueueToVector(std::move(q));
  EXPECT_EQ(v.size(), 1u);
  ASSERT_EQ(v[0], 42);
  DeleteTree(root);
}

/*
Left-skewed: nodes at each level only on left
  1
 /
2
/
//3*/
TEST(RightViewBFS, LeftSkewedTree) {
  Node<int>* root = new Node<int>(1);
  root->left = new Node<int>(2);
  root->left->left = new Node<int>(3);

  auto q = RightViewBFS<int>(root);
  auto v = QueueToVector(std::move(q));
  // Right view of a strictly left-skewed tree is [1,2,3]
  std::vector<int> expected = {1, 2, 3};
  EXPECT_EQ(v, expected);

  DeleteTree(root);
}

/*
Right-skewed: nodes at each level only on right
1
 \
  2
   \
    3
*/
TEST(RightViewBFS, RightSkewedTree) {
  Node<int>* root = new Node<int>(1);
  root->right = new Node<int>(2);
  root->right->right = new Node<int>(3);

  auto q = RightViewBFS<int>(root);
  auto v = QueueToVector(std::move(q));
  std::vector<int> expected = {1, 2, 3};
  EXPECT_EQ(v, expected);

  DeleteTree(root);
}

/*
Balanced-ish tree with right nodes that hide left nodes
on some levels:
     1
    / \
   2   3
  / \   \
 4   5   6
*/
TEST(RightViewBFS, MixedTree) {
  Node<int>* root = new Node<int>(1);
  root->left = new Node<int>(2);
  root->right = new Node<int>(3);
  root->left->left = new Node<int>(4);
  root->left->right = new Node<int>(5);
  root->right->right = new Node<int>(6);

  auto q = RightViewBFS<int>(root);
  auto v = QueueToVector(std::move(q));
  // Rightmost at level 0: 1
  // Rightmost at level 1: 3
  // Rightmost at level 2: 6
  std::vector<int> expected = {1, 3, 6};
  EXPECT_EQ(v, expected);

  DeleteTree(root);
}

/*
Full binary tree
       1
     /   \
    2     3
   / \   / \
  4  5  6   7
*/
TEST(RightViewBFS, FullBinaryTree) {
  Node<int>* root = new Node<int>(1);
  root->left = new Node<int>(2);
  root->right = new Node<int>(3);
  root->left->left = new Node<int>(4);
  root->left->right = new Node<int>(5);
  root->right->left = new Node<int>(6);
  root->right->right = new Node<int>(7);

  auto q = RightViewBFS<int>(root);
  auto v = QueueToVector(std::move(q));
  std::vector<int> expected = {1, 3, 7};
  EXPECT_EQ(v, expected);

  DeleteTree(root);
}

// Duplicate values
// Tree shape similar to MixedTree but values repeat.
TEST(RightViewBFS, Duplicates) {
  Node<int>* root = new Node<int>(1);
  root->left = new Node<int>(1);
  root->right = new Node<int>(1);
  root->left->right = new Node<int>(1);
  root->right->right = new Node<int>(1);

  auto q = RightViewBFS<int>(root);
  auto v = QueueToVector(std::move(q));
  // rightmost values per level
  std::vector<int> expected = {1, 1, 1};
  EXPECT_EQ(v, expected);

  DeleteTree(root);
}

/*
Irregular structure that ensures algorithm correctly
skips nulls on a level
     10
    /  \
   5    2
    \    \
     7    3
Right view: [10,2,3]
*/
TEST(RightViewBFS, MissingChildrenPerLevel) {
  Node<int>* root = new Node<int>(10);
  root->left = new Node<int>(5);
  root->right = new Node<int>(2);
  root->left->right = new Node<int>(7);
  root->right->right = new Node<int>(3);

  auto q = RightViewBFS<int>(root);
  auto v = QueueToVector(std::move(q));
  std::vector<int> expected = {10, 2, 3};
  EXPECT_EQ(v, expected);

  DeleteTree(root);
}

// Larger depth test (right-leaning chain) to exercise
// multiple levels
TEST(RightViewBFS, DeepRightChain) {
  const int depth = 12;
  Node<int>* root = new Node<int>(0);
  Node<int>* cur = root;
  for (int i = 1; i < depth; ++i) {
    cur->right = new Node<int>(i);
    cur = cur->right;
  }

  auto q = RightViewBFS<int>(root);
  auto v = QueueToVector(std::move(q));

  // Expect [0,1,2,...,depth-1]
  std::vector<int> expected;
  for (int i = 0; i < depth; ++i) expected.push_back(i);
  EXPECT_EQ(v, expected);

  DeleteTree(root);
}

// Non-integer test to verify template works with other
// types (std::string)
TEST(RightViewBFS, StringValues) {
  Node<std::string>* root = new Node<std::string>("root");
  root->left = new Node<std::string>("L");
  root->right = new Node<std::string>("R");
  root->left->left = new Node<std::string>("LL");
  root->right->right = new Node<std::string>("RR");

  auto q = RightViewBFS<std::string>(root);
  auto v = QueueToVector(std::move(q));
  std::vector<std::string> expected = {"root", "R", "RR"};
  EXPECT_EQ(v, expected);

  DeleteTree(root);
}
v0 (commit) © 2025 @p13i.io | Load balancer proxied to: cs-code-viewer-2:8080 in 5ms.