Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ae7eb49

Browse files
Add code for treaps
1 parent ae87774 commit ae7eb49

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed

‎treap.cpp

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define MOD 1000000007
4+
using namespace std;
5+
6+
// structure representing a treap node
7+
struct node {
8+
ll key;
9+
ll priority;
10+
node* left;
11+
node* right;
12+
node* parent;
13+
14+
node(ll data) {
15+
key = data;
16+
priority = (1LL * rand()) % MOD;
17+
left = right = parent = NULL;
18+
}
19+
};
20+
21+
// function to left-rotate the subtree rooted at x
22+
void left_rotate(node* &root, node* x) {
23+
node* y = x->right;
24+
x->right = y->left;
25+
26+
if (y->left != NULL) {
27+
y->left->parent = x;
28+
}
29+
y->parent = x->parent;
30+
if (x->parent == NULL) {
31+
root = y;
32+
}
33+
else if (x->key > x->parent->key) {
34+
x->parent->right = y;
35+
}
36+
else {
37+
x->parent->left = y;
38+
}
39+
40+
y->left = x;
41+
x->parent = y;
42+
}
43+
44+
// function to right-rotate the subtree rooted at x
45+
void right_rotate(node* &root, node* x) {
46+
node* y = x->left;
47+
x->left = y->right;
48+
49+
if (y->right != NULL) {
50+
y->right->parent = x;
51+
}
52+
y->parent = x->parent;
53+
if (x->parent == NULL) {
54+
root = y;
55+
}
56+
else if (x->key > x->parent->key) {
57+
x->parent->right = y;
58+
}
59+
else {
60+
x->parent->left = y;
61+
}
62+
63+
y->right = x;
64+
x->parent = y;
65+
}
66+
67+
// function to restore min-heap property by rotations
68+
void treap_insert_fixup(node* &root, node* z) {
69+
while (z->parent != NULL && z->parent->priority > z->priority) {
70+
// if z is a right child
71+
if (z->key > z->parent->key) {
72+
left_rotate(root, z->parent);
73+
}
74+
// if z is a left child
75+
else {
76+
right_rotate(root, z->parent);
77+
}
78+
}
79+
}
80+
81+
// function to insert a node into the treap
82+
// performs simple BST insert and calls treap_insert_fixup
83+
void insert(node* &root, ll data) {
84+
node *x = root, *y = NULL;
85+
while (x != NULL) {
86+
y = x;
87+
if (data < x->key) {
88+
x = x->left;
89+
}
90+
else {
91+
x = x->right;
92+
}
93+
}
94+
95+
node* z = new node(data);
96+
z->parent = y;
97+
if (y == NULL) {
98+
root = z;
99+
}
100+
else if (z->key > y->key) {
101+
y->right = z;
102+
}
103+
else {
104+
y->left = z;
105+
}
106+
107+
treap_insert_fixup(root, z);
108+
}
109+
110+
void preorder(node* root) {
111+
if (root) {
112+
cout << root->key << " ";
113+
preorder(root->left);
114+
preorder(root->right);
115+
}
116+
}
117+
118+
// free the allocated memory
119+
void delete_treap(node* root) {
120+
if (root) {
121+
delete_treap(root->left);
122+
delete_treap(root->right);
123+
delete root;
124+
}
125+
}
126+
127+
int main() {
128+
node* root = NULL;
129+
int choice;
130+
ll key;
131+
while (true) {
132+
cout << "1. Insert 2. Preorder 3. Quit\n";
133+
cin >> choice;
134+
if (choice == 1) {
135+
cout << "Enter key : ";
136+
cin >> key;
137+
insert(root, key);
138+
}
139+
else if (choice == 2) {
140+
preorder(root);
141+
cout << endl;
142+
}
143+
else {
144+
break;
145+
}
146+
}
147+
delete_treap(root);
148+
return 0;
149+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /