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