Hello I have never used pointers to build a binary tree , I always used arrays . I want to build an interval (where elements can be added between queries , not segment ) tree with pointers and need your help . I can't understand why people write any functions or procedures in the structure of pointer. If you know , please link any tutorial which can help me to study the syntax of pointers .
The simplicity of the interval trees you mention is exactly in avoiding the use of a data structure with pointers to children and parent. Though I've heard about a structure called Dynamic Interval Tree, which allows you to build an interval tree spanning a very large range and only create those nodes that are queried in the process; in this case pointers would be convenient (I haven't implemented such a structure myself).
What do you mean by "write any functions or procedures in the structure of pointer"?
yes, Dynamic interval tree is exactly what I want to write . I once wrote it with arrays in 1D , and now I want to write with pointers in 2D .
for example : struct vertex { vertex * l, * r; int sum;
};
I cant understand after "struct vertex { vertex * l, * r; int sum; "
its a code at the end of this page http://e-maxx.ru/algo/segment_tree and also don't know what " new " function does in that code.
Dynamic interval tree in 2D was on IOI 2013 .
Well, then your problem is not with interval trees, but with C++ syntax and concepts itself :)
Those two "vertex" methods within "vertex" structure are constructors — the methods that are executed when a new object of vertex class is created. Since you have two constructors for this class, you can either provide a value when you create a new object, or provide the two children of this node. For example, if you write
vertex a(100);
, the first method is invoked. Ifl(NULL), r(NULL), sum(val)
confuses you, that's a shorthand forl = NULL, r = NULL, sum = val;
(only works for the constructor).The
new
keyword (as well as malloc in plain C) lets you create an object dynamically. It is stored in "heap" memory, contrary to any local objects within functions, which are stored in "stack" memory. When you writenew class_name
, an object of the classclass_name
is created; the expression itself returns a pointer to that object. That's why you can assign the result of this expression to any variable of typeclass_name*
.I guess you should refer to some C++ textbook for further reading.
I understood everything in this code , but I have question in the second code. what "this" means in void recalc() ?
When a method is executed, you might need a pointer to the object whose method is executed. This pointer is
this
. So you can actually ignore all 'this' instances but the first andRecalc()
reads as:As for
!this
, it seems the same asthis != NULL
, but I don't quite understand how one can call recalc for a nonexisting object at all.thank you very much :)
Just in case, note that
this
can not be always ignored like that. If you have a local variable with the same name as a field of the class, the field is hidden and you can only access it throughthis -> field_name
.include
include
using namespace std;
struct treap{
};
void split(treap * t, int x, treap* &l, treap* &r){ if (!t){ l = NULL; r = NULL; return; }
}
void merge(treap* l, treap* r, treap* &t){
}
int max(int x, int y, treap* t){
}
void insert(int x, int value, treap* &t){
}
int main() { srand((int)time(0)); treap* t = NULL; int n, m;
}
this is full code.
this is the second example from Cartesian tree
struct treap{
};