Aleph-w 7.10.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 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
33# include <cstdlib>
34# include <ctime>
35# include <iostream>
36# include <string>
37# include <aleph.H>
38# include <tpl_avl.H>
39# include <tpl_binNodeUtils.H>
40
41# include <cassert>
42using namespace std;
43# include <cassert>
44using namespace Aleph;
45
46static void print_pair(Avl_Tree<int>::Node *p, int, int)
47{
48 cout << "(" << p->get_key() << "," << (int) (p->getDiff()) << ") ";
49}
50
51static void print(Avl_Tree<int>::Node *p, int, int)
52{
53 cout << p->get_key() << " ";
54}
55
56int main(int argc, char *argv[])
57{
58 int n = 1000;
59 unsigned int t = std::time(0);
60 int value;
61
62 if (argc > 1)
63 {
64 try
65 {
66 n = stoi(argv[1]);
67 }
68 catch (...)
69 {
70 n = 1000;
71 }
72 }
73
74 if (n <= 0)
75 {
76 cerr << "Error: n must be a positive integer." << endl;
77 return 1;
78 }
79
80 if (argc > 2)
81 {
82 try
83 {
84 t = stoi(argv[2]);
85 }
86 catch (...)
87 {
88 t = std::time(0);
89 }
90 }
91
92 srand(t);
93
94 cout << "testAvl " << n << " " << t << endl;
95
96 Avl_Tree<int> tree;
99 int i;
100
101 cout << "Inserting " << n << " random values in tree ...\n";
102
103 for (i = 0; i < n; i++)
104 {
105 do
106 {
107 value = (int) (n*10.0*rand()/(RAND_MAX+1.0));
108 node = tree.search(value);
109 }
110 while (node not_eq nullptr);
111
112 cout << value << " ";
113 node = new Avl_Tree<int>::Node (value);
114 tree.insert(node);
115 keys.append(value);
116 }
117 cout << endl << "verifying avl tree after insertions ... "
118 << endl;
119 assert(is_avl(tree.getRoot()));
120 cout << " done" << endl;
121
122 cout << "start-prefix ";
123 preOrderRec(tree.getRoot(), print);
124 cout << endl << endl;
125
126 cout << endl
127 << "start-infix ";
129 cout << endl << endl;
130
131
132 for (i = 0; i < n/2; i++)
133 {
134 value = keys[i];
135 node = tree.remove(value);
136 assert(node != nullptr);
137
138 cout << value << " ";
139 delete node;
140 }
141
142 cout << endl
143 << "start-prefix ";
144 preOrderRec(tree.getRoot(), print);
145 cout << endl << endl;
146
147 cout << endl << "verifying avl tree after deletions ... " ;
148 assert(is_avl(tree.getRoot()));
149 cout << " done" << endl;
150
151 destroyRec(tree.getRoot());
152}
153
Core header for the Aleph-w library.
bool is_avl(Node *p)
Validate that a tree satisfies AVL properties.
Definition avlNode.H:160
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
Node * insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert the node pointed by p in the tree.
Definition tpl_avl.H:640
Node * remove(const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
Remove from an AVL tree the node containing key key.
Definition tpl_avl.H:738
constexpr Node *& getRoot() noexcept
Return a modifiable reference to tree's root.
Definition tpl_avl.H:586
Node * search(const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
Search a node containing key.
Definition tpl_avl.H:607
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 all the memory occupied by the tree root
Main namespace for Aleph-w library functions.
STL namespace.
AVL binary search tree with nodes without a virtual destructor.
Definition tpl_avl.H:831
int keys[]
static void print(Avl_Tree< int >::Node *p, int, int)
Definition testAvl.C:51
static void print_pair(Avl_Tree< int >::Node *p, int, int)
Definition testAvl.C:46
AVL tree implementation (height-balanced BST).
Utility functions for binary tree operations.