Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tree_path_capacity_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 / \ | | ___ _ __ | |__ __ __
5 / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / /
6 / ___ \| | __/ |_) | | | |_____\ V V /
7 /_/ \_\_|\___| .__/|_| |_| \_/\_/
8 |_|
9
10 This file is part of Aleph-w library
11
12 Copyright (c) 2002-2026 Leandro Rabindranath Leon
13
14 Permission is hereby granted, free of charge, to any person obtaining a copy
15 of this software and associated documentation files (the "Software"), to deal
16 in the Software without restriction, including without limitation the rights
17 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 copies of the Software, and to permit persons to whom the Software is
19 furnished to do so, subject to the following conditions:
20
21 The above copyright notice and this permission notice shall be included in all
22 copies or substantial portions of the Software.
23
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 SOFTWARE.
31*/
32
45#include <gtest/gtest.h>
46
47#include <memory>
48#include <vector>
49
50#include <tpl_avl.H>
51#include <tpl_binNodeUtils.H>
52#include <tpl_rb_tree.H>
53
54using namespace Aleph;
55
56// Nodes whose MaxHeight a few keys reach.
59
60namespace
61{
62 // Owns the nodes of a tree, which does not free them.
63 template <class Node>
64 struct Node_Pool
65 {
66 std::vector<std::unique_ptr<Node>> nodes;
67
68 Node * make(const int key)
69 {
70 nodes.push_back(std::make_unique<Node>(key));
71 return nodes.back().get();
72 }
73 };
74
75 // The keys 1 ... 15 in the level order of a perfect tree: inserted in
76 // this order they keep a balanced tree of height 4 with no rotation.
77 constexpr int level_order[] = {8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15};
78
79 // Its leaves, at depth 3.
80 constexpr int leaves[] = {1, 3, 5, 7, 9, 11, 13, 15};
81}
82
83TEST(TreePathCapacity, AvlTreeAsTallAsMaxHeight)
84{
86 static_assert(Tree::Node::MaxHeight == 4);
87
89 Tree tree;
90 for (const int key : level_order)
91 ASSERT_NE(tree.insert(pool.make(key)), nullptr);
92 ASSERT_EQ(computeHeightRec(tree.getRoot()), 4u);
93 ASSERT_TRUE(tree.verify());
94
95 for (const int key : leaves)
96 {
97 ASSERT_NE(tree.remove(key), nullptr) << "key " << key;
98 ASSERT_TRUE(tree.verify());
99 }
100 EXPECT_EQ(size(tree.getRoot()), 7u);
101}
102
103TEST(TreePathCapacity, RbTreeAsTallAsMaxHeight)
104{
106 static_assert(Tree::Node::MaxHeight == 4);
107
109 Tree tree;
110 for (const int key : level_order)
111 ASSERT_NE(tree.insert(pool.make(key)), nullptr);
112 ASSERT_EQ(computeHeightRec(tree.getRoot()), 4u);
113 ASSERT_TRUE(tree.verify());
114
115 for (const int key : leaves)
116 {
117 ASSERT_NE(tree.remove(key), nullptr) << "key " << key;
118 ASSERT_TRUE(tree.verify());
119 }
120 EXPECT_EQ(tree.size(), 7u);
121}
WeightedDigraph::Node Node
AVL balanced binary search tree.
Definition tpl_avl.H:105
Red-black binary search tree implementation (bottom-up).
Data portion of an AVL tree node.
Definition avlNode.H:77
Conjunto de nodos a reusarse.
QuadTree - Hierarchical spatial index for 2D points.
Definition quadtree.H:126
void remove(const Point &p)
Remove a point from the tree.
Definition quadtree.H:565
Point * insert(Node *&r, const Point &p)
Recursive insert helper.
Definition quadtree.H:281
Data portion of a Red-Black tree node.
Definition rbNode.H:91
#define TEST(name)
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
#define DECLARE_BINNODE_SENTINEL(Name, height, Control_Data)
Specify tree node for a binary tree.
#define DECLARE_BINNODE(Name, height, Control_Data)
Specify tree node for a binary tree.
size_t computeHeightRec(Node *root) noexcept
Compute the height of root
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
AVL tree implementation (height-balanced BST).
Utility functions for binary tree operations.
Red-Black tree implementation (bottom-up balancing).
void level_order(int v[], int n)
Write level-order traversal for LaTeX output.
Definition writeHeap.C:262