-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2xt.cpp
More file actions
61 lines (59 loc) · 972 Bytes
/
2xt.cpp
File metadata and controls
61 lines (59 loc) · 972 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <cstring>
using namespace std;
class node
{
node *lson;
node *rson;
node *parent;
int value;
public:
node(int val,node *p)
{
value=val;
lson=NULL;
rson=NULL;
parent=p;
}
node* getlson(){return lson;}
node* getrson(){return rson;}
int getvalue(){return value;}
void setlson(node * l){lson=l;}
void setrson(node * r){rson=r;}
void insert(int val)
{
if(val<value)
{
if(lson==NULL) lson=new node(val,this);
else lson->insert(val);
}
else if(val==value) return;
else
{
if(rson==NULL) rson=new node(val,this);
else rson->insert(val);
}
}
void mprint()
{
if(lson!=NULL) lson->mprint();
cout<<" "<<value<<" ";
if(rson!=NULL) rson->mprint();
}
};
int main()
{
srand(time(0));
node *root=NULL,*t=NULL;
int x;
for(int i=0;i<10;i++)
{
x=rand()%100;
if(root==NULL) root=new node(x,NULL);
else root->insert(x);
}
root->mprint();
cout<<endl;
}