Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
treapObs.C
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
33# include <iostream>
34# include <fstream>
35# include <time.h>
36# include <aleph.H>
37# include <tpl_binNodeUtils.H>
38
39
40using namespace std;
41using namespace Aleph;
42
43
44unsigned long randomLong()
45{
46 return (long) (100.0*rand()/RAND_MAX);
47}
48
49
50fstream * file_ptr;
51
52
53# include <tpl_treap.H>
54
55
56void print_key(TreapTreeVtl<int>::Node *node)
57{
58 *file_ptr << node->get_key() << " ";
59}
60
61void print_prio(TreapTreeVtl<int>::Node *node)
62{
63 *file_ptr << node->getPriority() << " ";
64}
65
66
67fstream file[12];
68
69
70int main(int argn, char *argc[])
71{
72 int n = 15;
73 unsigned int t = time(0);
74 int value;
75 int val_del = 0;
76
77 if (argn > 1)
78 n = atoi(argc[1]);
79
80 if (argn > 2)
81 t = atoi(argc[2]);
82
83 if (argn > 3)
84 val_del = atoi(argc[3]);
85
86 srand(t);
87
88 cout << "treapObs " << n << " " << t << endl;
89
91 TreapTreeVtl<int>::Node *node;
92 int i;
93
94 cout << "Inserting " << n << " random values in treee ...\n";
95
96 for (i = 0; i < n; i++)
97 {
98 value = 1+(int) (n*5.0*rand()/(RAND_MAX+1.0));
99 node = tree.search(value);
100 if (node == nullptr)
101 {
102 node = new TreapTreeVtl<int>::Node (value);
103 tree.insert(node);
104 }
105 }
106
107 file[0].open("treap-00.treap", ios::out);
108 file[1].open("treap-01.treap", ios::out);
109 file[2].open("treap-02.treap", ios::out);
110 file[3].open("treap-03.treap", ios::out);
111 file[4].open("treap-04.treap", ios::out);
112 file[5].open("treap-05.treap", ios::out);
113 file[6].open("treap-06.treap", ios::out);
114 file[7].open("treap-07.treap", ios::out);
115 file[8].open("treap-08.treap", ios::out);
116 file[9].open("treap-09.treap", ios::out);
117 file[10].open("treap-10.treap", ios::out);
118 file[11].open("treap-11.treap", ios::out);
119
120 file_ptr = &file[0];
121 *file_ptr << "-b ";
122 preOrderRec(tree.getRoot(), print_key);
123 *file_ptr << "-D ";
124 preOrderRec(tree.getRoot(), print_prio);
125 file_ptr->close();
126
127 cout << endl << "verifying TreapTreeVtl after insertions ... "
128 << endl;
129 assert(is_treap(tree.getRoot()));
130 cout << " done" << endl;
131
132 tree.remove(val_del);
133
134 cout << "The path length is " << compute_path_length(tree.getRoot())
135 << endl;
136
137 destroyRec(tree.getRoot());
138
139 cout << "treapObs " << n << " " << t << endl;
140}
int main()
Core header for the Aleph-w library.
T remove()
Remove the first item of the list.
Definition htlist.H:1611
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
bool is_treap(Node *root) noexcept
Validate that a tree satisfies treap (heap) property.
Definition treapNode.H:144
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
Utility functions for binary tree operations.
Treap: randomized BST combining tree and heap properties.
void print_prio(TreapTreeVtl< int >::Node *node)
Definition treapObs.C:61
fstream file[12]
Definition treapObs.C:67
fstream * file_ptr
Definition treapObs.C:50
unsigned long randomLong()
Definition treapObs.C:44
void print_key(TreapTreeVtl< int >::Node *node)
Definition treapObs.C:56