Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
writeAvl.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 <fstream>
32# include <string>
33# include <aleph.H>
34# include <tpl_avl.H>
35# include <tpl_binNodeUtils.H>
36
37# include <cassert>
38using namespace std;
39# include <cassert>
40using namespace Aleph;
41
42ofstream file("avl-tree-aux.Tree", ios::out);
43
44
45static void print_key(Avl_Tree<int>::Node *p, int, int)
46{
47 file << p->get_key() << " ";
48}
49
50
51int main(int argc, char *argv[])
52{
53 int n = 1000;
54 if (argc > 1)
55 {
56 try { n = stoi(argv[1]); } catch (...) { n = 1000; }
57 }
58
59 if (n <= 0)
60 {
61 cerr << "n must be positive" << endl;
62 return 1;
63 }
64
65 unsigned int t = std::time(0);
66 if (argc > 2)
67 {
68 try { t = stoul(argv[2]); } catch (...) { t = std::time(0); }
69 }
70
71 srand(t);
72
73 cout << "writeAvl " << n << " " << t << endl;
74
75 Avl_Tree<int> tree;
77 int i, value;
78
79 cout << "Inserting " << n << " random values in treee ...\n";
80
81 for (i = 0; i < n; i++)
82 {
83 do
84 {
85
86 value = (int) (n*10.0*rand()/(RAND_MAX+1.0));
87 node = tree.search(value);
88 } while (node not_eq NULL);
89 node = new Avl_Tree<int>::Node (value);
90 tree.insert(node);
91 }
92 cout << endl << "verifying avl tree after insertions ... "
93 << endl;
94 assert(is_avl(tree.getRoot()));
95 cout << " done" << endl;
96
98
99 destroyRec(tree.getRoot());
100}
101
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
int preOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively in preorder 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
AVL tree implementation (height-balanced BST).
Utility functions for binary tree operations.
static void print_key(Avl_Tree< int >::Node *p, int, int)
Definition writeAvl.C:45
ofstream file
Definition writeRb.C:46