20 #include <gtest/gtest.h> 31 const int SortedArrayInt[] = {-3, -2, 0, 2, 8, 15, 36, 212, 366};
33 const int RandomArrayInt[] = {4, 3, 5, 2, -18, 3, 2, 3, 4, 5, -5};
35 const int RandomArrayInterInt[] = {5 , 5, -5, 3, -18, 10, 15};
37 const int SmallIntArray[] = {2, 1, 3};
39 const int SmallIntArraySorted[] = {1, 2, 3};
44 bool operator() (
const T& a,
const T& b)
const 45 {
return std::abs(a - b) < std::numeric_limits<T>::epsilon(); }
51 bool operator() (
const T& a,
const T& b)
const {
return a == b; }
54 typedef std::vector<int> Container;
55 typedef Container::value_type Value;
56 typedef Container::iterator IT;
57 typedef Container::const_iterator Const_IT;
59 typedef std::unique_ptr<Const_BST> Const_Own_BST;
68 const Container kEmptyCollection = Container();
69 Const_Own_BST tree = Const_BST::Build(kEmptyCollection.begin(), kEmptyCollection.end());
75 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
76 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.end(), kSmallIntArray.begin());
82 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
83 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.begin(), kSmallIntArray.begin() + 1);
84 EXPECT_EQ(2, tree->GetData());
85 EXPECT_TRUE(tree->GetLeftChild() ==
nullptr);
86 EXPECT_TRUE(tree->GetRightChild() ==
nullptr);
91 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
92 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.begin(), kSmallIntArray.end());
94 EXPECT_EQ(2, tree->GetData());
95 EXPECT_EQ(1, tree->GetLeftChild()->GetData());
96 EXPECT_EQ(3, tree->GetRightChild()->GetData());
97 EXPECT_TRUE(tree->GetLeftChild()->GetLeftChild() ==
nullptr);
98 EXPECT_TRUE(tree->GetLeftChild()->GetRightChild() ==
nullptr);
99 EXPECT_TRUE(tree->GetRightChild()->GetLeftChild() ==
nullptr);
100 EXPECT_TRUE(tree->GetRightChild()->GetRightChild() ==
nullptr);
105 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
106 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.begin(), kSmallIntArray.end());
108 EXPECT_EQ(2, tree->GetData());
109 EXPECT_EQ(1, tree->GetLeftChild()->GetData());
110 EXPECT_EQ(3, tree->GetRightChild()->GetData());
111 EXPECT_TRUE(tree->GetLeftChild()->GetLeftChild() ==
nullptr);
112 EXPECT_TRUE(tree->GetLeftChild()->GetRightChild() ==
nullptr);
113 EXPECT_TRUE(tree->GetRightChild()->GetLeftChild() ==
nullptr);
114 EXPECT_TRUE(tree->GetRightChild()->GetRightChild() ==
nullptr);
123 const Container kEmptyCollection = Container();
124 Const_Own_BST tree = Const_BST::Build(kEmptyCollection.begin(), kEmptyCollection.end());
130 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
131 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.end(), kSmallIntArray.begin());
137 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
138 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.begin(), kSmallIntArray.begin() + 1);
139 EXPECT_EQ(2, tree->GetData());
140 EXPECT_TRUE(tree->GetLeftChild() ==
nullptr);
141 EXPECT_TRUE(tree->GetRightChild() ==
nullptr);
146 const Container kSmallSorted(SmallIntArraySorted,
147 SmallIntArraySorted +
sizeof(SmallIntArraySorted) /
sizeof(Value));
148 Const_Own_BST tree = Const_BST::BuildFromSorted(kSmallSorted.begin(), kSmallSorted.end());
150 EXPECT_EQ(2, tree->GetData());
151 EXPECT_EQ(1, tree->GetLeftChild()->GetData());
152 EXPECT_EQ(3, tree->GetRightChild()->GetData());
153 EXPECT_TRUE(tree->GetLeftChild()->GetLeftChild() ==
nullptr);
154 EXPECT_TRUE(tree->GetLeftChild()->GetRightChild() ==
nullptr);
155 EXPECT_TRUE(tree->GetRightChild()->GetLeftChild() ==
nullptr);
156 EXPECT_TRUE(tree->GetRightChild()->GetRightChild() ==
nullptr);
165 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
166 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.begin(), kSmallIntArray.begin() + 1);
167 EXPECT_TRUE(tree->IsValid());
172 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
173 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.begin(), kSmallIntArray.end());
174 EXPECT_TRUE(tree->IsValid());
179 const Container kSmallSorted(SmallIntArraySorted,
180 SmallIntArraySorted +
sizeof(SmallIntArraySorted) /
sizeof(Value));
181 Const_Own_BST tree = Const_BST::BuildFromSorted(kSmallSorted.begin(), kSmallSorted.end());
182 EXPECT_TRUE(tree->IsValid());
187 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
188 Const_Own_BST tree = Const_BST::BuildFromSorted(kSmallIntArray.begin(), kSmallIntArray.end());
189 EXPECT_FALSE(tree->IsValid());
194 const Container kRandIntArray(RandomArrayInt, RandomArrayInt +
sizeof(RandomArrayInt) /
sizeof(Value));
195 Const_Own_BST tree = Const_BST::Build(kRandIntArray.begin(), kRandIntArray.end());
196 EXPECT_TRUE(tree->IsValid());
205 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
206 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.begin(), kSmallIntArray.begin() + 1);
207 EXPECT_EQ(2, tree->GetData());
209 EXPECT_EQ(10, tree->GetRightChild()->GetData());
211 EXPECT_EQ(15, tree->GetRightChild()->GetRightChild()->GetData());
213 EXPECT_EQ(-10, tree->GetLeftChild()->GetData());
215 EXPECT_EQ(0, tree->GetLeftChild()->GetRightChild()->GetData());
224 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
225 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.begin(), kSmallIntArray.begin() + 1);
226 EXPECT_EQ(1, tree->Size());
231 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
232 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.begin(), kSmallIntArray.end());
233 EXPECT_EQ(kSmallIntArray.size(), tree->Size());
238 const Container kSmallSorted(SmallIntArraySorted,
239 SmallIntArraySorted +
sizeof(SmallIntArraySorted) /
sizeof(Value));
240 Const_Own_BST tree = Const_BST::BuildFromSorted(kSmallSorted.begin(), kSmallSorted.end());
241 EXPECT_EQ(kSmallSorted.size(), tree->Size());
246 const Container kRandIntArray(RandomArrayInt, RandomArrayInt +
sizeof(RandomArrayInt) /
sizeof(Value));
247 Const_Own_BST tree = Const_BST::Build(kRandIntArray.begin(), kRandIntArray.end());
248 EXPECT_EQ(kRandIntArray.size(), tree->Size());
257 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
258 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.begin(), kSmallIntArray.begin() + 1);
259 EXPECT_EQ(1, tree->MinHeight());
264 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
265 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.begin(), kSmallIntArray.end());
266 EXPECT_EQ(2, tree->MinHeight());
271 const Container kSmallSorted(SmallIntArraySorted,
272 SmallIntArraySorted +
sizeof(SmallIntArraySorted) /
sizeof(Value));
273 Const_Own_BST tree = Const_BST::BuildFromSorted(kSmallSorted.begin(), kSmallSorted.end());
274 EXPECT_EQ(2, tree->MinHeight());
279 const Container kRandIntArray(RandomArrayInt, RandomArrayInt +
sizeof(RandomArrayInt) /
sizeof(Value));
280 Const_Own_BST tree = Const_BST::Build(kRandIntArray.begin(), kRandIntArray.end());
281 EXPECT_EQ(2, tree->MinHeight());
290 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
291 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.begin(), kSmallIntArray.begin() + 1);
292 EXPECT_EQ(1, tree->MaxHeight());
297 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
298 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.begin(), kSmallIntArray.end());
299 EXPECT_EQ(2, tree->MaxHeight());
304 const Container kSmallSorted(SmallIntArraySorted,
305 SmallIntArraySorted +
sizeof(SmallIntArraySorted) /
sizeof(Value));
306 Const_Own_BST tree = Const_BST::BuildFromSorted(kSmallSorted.begin(), kSmallSorted.end());
307 EXPECT_EQ(2, tree->MaxHeight());
312 const Container kRandIntArray(RandomArrayInt, RandomArrayInt +
sizeof(RandomArrayInt) /
sizeof(Value));
313 Const_Own_BST tree = Const_BST::Build(kRandIntArray.begin(), kRandIntArray.end());
314 EXPECT_EQ(6, tree->MaxHeight());
323 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
324 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.begin(), kSmallIntArray.begin() + 1);
325 EXPECT_TRUE(tree->IsBlanced());
330 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
331 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.begin(), kSmallIntArray.end());
332 EXPECT_TRUE(tree->IsBlanced());
337 const Container kSmallSorted(SmallIntArraySorted,
338 SmallIntArraySorted +
sizeof(SmallIntArraySorted) /
sizeof(Value));
339 Const_Own_BST tree = Const_BST::BuildFromSorted(kSmallSorted.begin(), kSmallSorted.end());
340 EXPECT_TRUE(tree->IsBlanced());
345 const Container kSortedArrayInt(SortedArrayInt, SortedArrayInt +
sizeof(SortedArrayInt) /
sizeof(Value));
346 Const_Own_BST tree = Const_BST::Build(kSortedArrayInt.begin(), kSortedArrayInt.end());
347 EXPECT_FALSE(tree->IsBlanced());
356 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
357 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.begin(), kSmallIntArray.begin() + 1);
358 EXPECT_EQ(2, tree->Find(2)->GetData());
359 EXPECT_FALSE(tree->Find(0));
360 EXPECT_FALSE(tree->Find(5));
365 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
366 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.begin(), kSmallIntArray.end());
367 EXPECT_EQ(1, tree->Find(1)->GetData());
368 EXPECT_EQ(2, tree->Find(2)->GetData());
369 EXPECT_EQ(3, tree->Find(3)->GetData());
370 EXPECT_FALSE(tree->Find(0));
371 EXPECT_FALSE(tree->Find(5));
376 const Container kSmallSorted(SmallIntArraySorted,
377 SmallIntArraySorted +
sizeof(SmallIntArraySorted) /
sizeof(Value));
378 Const_Own_BST tree = Const_BST::BuildFromSorted(kSmallSorted.begin(), kSmallSorted.end());
379 EXPECT_EQ(1, tree->Find(1)->GetData());
380 EXPECT_EQ(2, tree->Find(2)->GetData());
381 EXPECT_EQ(3, tree->Find(3)->GetData());
382 EXPECT_FALSE(tree->Find(0));
383 EXPECT_FALSE(tree->Find(5));
388 const Container kRandIntArray(RandomArrayInt, RandomArrayInt +
sizeof(RandomArrayInt) /
sizeof(Value));
389 Const_Own_BST tree = Const_BST::Build(kRandIntArray.begin(), kRandIntArray.end());
390 EXPECT_EQ(-18, tree->Find(-18)->GetData());
391 EXPECT_EQ(-5, tree->Find(-5)->GetData());
392 EXPECT_EQ(5, tree->Find(5)->GetData());
393 EXPECT_FALSE(tree->Find(1));
394 EXPECT_FALSE(tree->Find(6));
403 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
404 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.begin(), kSmallIntArray.begin());
405 ASSERT_TRUE(tree ==
nullptr);
406 const Const_BST* returnTreePtr = Const_BST::Remove(tree, 2);
407 EXPECT_TRUE(returnTreePtr ==
nullptr);
408 EXPECT_TRUE(tree ==
nullptr);
413 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
414 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.begin(), kSmallIntArray.begin() + 1);
415 const Const_BST* returnTreePtr = Const_BST::Remove(tree, 2);
416 EXPECT_TRUE(returnTreePtr ==
nullptr);
417 EXPECT_TRUE(tree ==
nullptr);
422 const Container kSmallIntArray(5, 4);
423 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.begin(), kSmallIntArray.end());
424 const Const_BST* returnTreePtr = Const_BST::Remove(tree, 4);
425 EXPECT_TRUE(returnTreePtr ==
nullptr);
426 EXPECT_TRUE(tree ==
nullptr);
431 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
432 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.begin(), kSmallIntArray.end());
433 const Const_BST* returnTreePtr = Const_BST::Remove(tree, 3);
436 EXPECT_EQ(tree.get(), returnTreePtr);
437 EXPECT_EQ(2, tree->GetData());
438 ASSERT_TRUE(tree->GetLeftChild() !=
nullptr);
439 EXPECT_EQ(1, tree->GetLeftChild()->GetData());
440 EXPECT_TRUE(tree->GetRightChild() ==
nullptr);
445 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
446 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.begin(), kSmallIntArray.begin() + 2);
447 const Const_BST* returnTreePtr = Const_BST::Remove(tree, 2);
450 EXPECT_EQ(tree.get(), returnTreePtr);
451 EXPECT_EQ(1, tree->GetData());
452 EXPECT_TRUE(tree->GetRightChild() ==
nullptr);
453 EXPECT_TRUE(tree->GetLeftChild() ==
nullptr);
458 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
459 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.begin() + 1, kSmallIntArray.end());
460 const Const_BST* returnTreePtr = Const_BST::Remove(tree, 1);
463 EXPECT_EQ(tree.get(), returnTreePtr);
464 EXPECT_EQ(3, tree->GetData());
465 EXPECT_TRUE(tree->GetRightChild() ==
nullptr);
466 EXPECT_TRUE(tree->GetLeftChild() ==
nullptr);
471 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
472 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.begin(), kSmallIntArray.begin() + 1);
478 const Const_BST* returnTreePtr = Const_BST::Remove(tree, 2);
481 EXPECT_EQ(tree.get(), returnTreePtr);
482 EXPECT_EQ(0, tree->GetData());
483 ASSERT_TRUE(tree->GetRightChild());
484 EXPECT_EQ(1, tree->GetRightChild()->GetData());
485 ASSERT_TRUE(tree->GetLeftChild());
486 EXPECT_EQ(-2, tree->GetLeftChild()->GetData());
487 EXPECT_EQ(3, tree->GetLeftChild()->Size());
492 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
493 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.begin(), kSmallIntArray.begin() + 1);
499 const Const_BST* returnTreePtr = Const_BST::Remove(tree, 2);
502 EXPECT_EQ(tree.get(), returnTreePtr);
503 EXPECT_EQ(4, tree->GetData());
504 ASSERT_TRUE(tree->GetLeftChild());
505 EXPECT_EQ(3, tree->GetLeftChild()->GetData());
506 ASSERT_TRUE(tree->GetRightChild());
507 EXPECT_EQ(6, tree->GetRightChild()->GetData());
508 EXPECT_EQ(3, tree->GetRightChild()->Size());
513 const Container kSmallIntArray(SmallIntArray, SmallIntArray +
sizeof(SmallIntArray) /
sizeof(Value));
514 Const_Own_BST tree = Const_BST::Build(kSmallIntArray.begin(), kSmallIntArray.end());
515 const Const_BST* returnTreePtr = Const_BST::Remove(tree, 2);
518 EXPECT_EQ(tree.get(), returnTreePtr);
519 EXPECT_EQ(1, tree->GetData());
520 ASSERT_TRUE(tree->GetRightChild());
521 EXPECT_EQ(3, tree->GetRightChild()->GetData());
522 EXPECT_TRUE(tree->GetLeftChild() ==
nullptr);
527 const Container kContainerValue(1, 10);
528 Const_Own_BST tree = Const_BST::Build(kContainerValue.begin(), kContainerValue.end());
537 const Const_BST* returnTreePtr = Const_BST::Remove(tree, 10);
540 EXPECT_EQ(tree.get(), returnTreePtr);
541 EXPECT_EQ(8, tree->GetData());
542 EXPECT_EQ(8, tree->Size());
543 ASSERT_TRUE(tree->GetRightChild());
544 EXPECT_EQ(3, tree->GetRightChild()->Size());
545 ASSERT_TRUE(tree->GetLeftChild()->GetRightChild());
546 ASSERT_EQ(7, tree->GetLeftChild()->GetRightChild()->GetData());