-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCartesianTree.cpp
85 lines (73 loc) · 1.21 KB
/
CartesianTree.cpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<iostream>
#include<algorithm>
#include<vector>
#define MAX 10000;
using namespace std;
typedef struct node
{
int val;
int count;
int left_size;
int right_size;
int size;
}node;
std::vector<node> tree;
node makeNode(int val)
{
node t;
t.val=val;
return t;
}
int buildTree(int s[], int as, int ae, int t)
{
if(as>ae)
return 0;
else
{
int mid=(as+ae)/2;
node x=makeNode(s[mid]);
x.left_size=buildTree(s,as,mid-1,2*t);
x.right_size=buildTree(s,mid+1,ae,2*t+1);
x.count=1;
x.size=x.left_size+x.right_size+x.count;
tree[t]=x;
return x.size;
}
}
int makeTree(int s[], int n)
{
tree.resize(1000);
return(buildTree(s,0,n-1,1));
}
int query(int k, int t)
{
if(k<=tree[t].left_size)
{
return(query(k,2*t));
}
else if(k>(tree[t].left_size+tree[t].count))
{
return(query(k-tree[t].left_size-tree[t].count,2*t+1));
}
else
{
tree[t].count=0;
return tree[t].val;
}
}
int findKSmallest(int k)
{
return query(k,1);
}
int main()
{
int s[]={1,2,3,4,5};
makeTree(s,5);
for(int i=1;i<=7;i++)
cout<<tree[i].size<<" ";
cout<<endl;
cout<<findKSmallest(1);
cout<<endl;
cout<<findKSmallest(1);
return 0;
}