Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testAvl.C
Go to the documentation of this file.
1
2/* Aleph-w
3
4 / \ | | ___ _ __ | |__ __ __
5 / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6 / ___ \| | __/ |_) | | | |_____\ V V / version 1.9c
7 /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8 |_|
9
10 This file is part of Aleph-w library
11
12 Copyright (c) 2002-2018 Leandro Rabindranath Leon
13
14 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <https://www.gnu.org/licenses/>.
26*/
27
28# include <cstdlib>
29# include <ctime>
30# include <iostream>
31# include <string>
32# include <aleph.H>
33# include <tpl_avl.H>
34# include <tpl_binNodeUtils.H>
35
36# include <cassert>
37using namespace std;
38# include <cassert>
39using namespace Aleph;
40
41static void print_pair(Avl_Tree<int>::Node *p, int, int)
42{
43 cout << "(" << p->get_key() << "," << (int) (p->getDiff()) << ") ";
44}
45
46static void print(Avl_Tree<int>::Node *p, int, int)
47{
48 cout << p->get_key() << " ";
49}
50
51int main(int argc, char *argv[])
52{
53 int n = 1000;
54 unsigned int t = std::time(0);
55 int value;
56
57 if (argc > 1)
58 {
59 try
60 {
61 n = stoi(argv[1]);
62 }
63 catch (...)
64 {
65 n = 1000;
66 }
67 }
68
69 if (n <= 0)
70 {
71 cerr << "Error: n must be a positive integer." << endl;
72 return 1;
73 }
74
75 if (argc > 2)
76 {
77 try
78 {
79 t = stoi(argv[2]);
80 }
81 catch (...)
82 {
83 t = std::time(0);
84 }
85 }
86
87 srand(t);
88
89 cout << "testAvl " << n << " " << t << endl;
90
91 Avl_Tree<int> tree;
94 int i;
95
96 cout << "Inserting " << n << " random values in tree ...\n";
97
98 for (i = 0; i < n; i++)
99 {
100 do
101 {
102 value = (int) (n*10.0*rand()/(RAND_MAX+1.0));
103 node = tree.search(value);
104 }
105 while (node not_eq nullptr);
106
107 cout << value << " ";
108 node = new Avl_Tree<int>::Node (value);
109 tree.insert(node);
110 keys.append(value);
111 }
112 cout << endl << "verifying avl tree after insertions ... "
113 << endl;
114 assert(is_avl(tree.getRoot()));
115 cout << " done" << endl;
116
117 cout << "start-prefix ";
118 preOrderRec(tree.getRoot(), print);
119 cout << endl << endl;
120
121 cout << endl
122 << "start-infix ";
124 cout << endl << endl;
125
126
127 for (i = 0; i < n/2; i++)
128 {
129 value = keys[i];
130 node = tree.remove(value);
131 assert(node != nullptr);
132
133 cout << value << " ";
134 delete node;
135 }
136
137 cout << endl
138 << "start-prefix ";
139 preOrderRec(tree.getRoot(), print);
140 cout << endl << endl;
141
142 cout << endl << "verifying avl tree after deletions ... " ;
143 assert(is_avl(tree.getRoot()));
144 cout << " done" << endl;
145
146 destroyRec(tree.getRoot());
147}
148
Core header for the Aleph-w library.
bool is_avl(Node *p)
Validate that a tree satisfies AVL properties.
Definition avlNode.H:123
Node * search(const Key &key) const noexcept
Search a node containing key; if found, then a pointer to the node containing it is returned; otherwi...
Definition tpl_avl.H:533
constexpr Node *& getRoot() noexcept
Return a modifiable reference to tree's root.
Definition tpl_avl.H:526
Node * insert(Node *p) noexcept
Insert the node pointed by p in the tree.
Definition tpl_avl.H:561
Node * remove(const Key &key) noexcept
Remove from an AVL tree the node containing key key.
Definition tpl_avl.H:640
int preOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively in preorder a binary tree.
int inOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively inorder a binary tree.
void destroyRec(Node *&root) noexcept
Free recursively all the memory occupied by the tree root
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
STL namespace.
AVL binary search tree with nodes without virtual destructor.
Definition tpl_avl.H:737
int keys[]
static void print(Avl_Tree< int >::Node *p, int, int)
Definition testAvl.C:46
static void print_pair(Avl_Tree< int >::Node *p, int, int)
Definition testAvl.C:41
AVL tree implementation (height-balanced BST).
Utility functions for binary tree operations.