-
Notifications
You must be signed in to change notification settings - Fork 368
/
treap_algo.java
147 lines (128 loc) · 4.37 KB
/
treap_algo.java
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import java.util.Scanner;
class Node {
int key;
int priority;
Node left, right;
// Function to create a new node with the given key
// and a randomly generated priority value
public Node(int key, int priority) {
this.key = key;
this.priority = priority;
this.left = this.right = null;
}
}
class treap_algo {
Node root;
// Function to perform a right rotation on the given root node
private Node rightRotate(Node root) {
Node newRoot = root.left;
Node temp = newRoot.right;
newRoot.right = root;
root.left = temp;
return newRoot;
}
// Function to perform a left rotation on the given root node
private Node leftRotate(Node root) {
Node newRoot = root.right;
Node temp = newRoot.left;
newRoot.left = root;
root.right = temp;
return newRoot;
}
// Function to insert a new node with the given key into the tree
private Node insert(Node root, int key, int priority) {
if (root == null)
return new Node(key, priority);
if (key < root.key) {
root.left = insert(root.left, key, priority);
if (root.left.priority > root.priority)
root = rightRotate(root);
} else {
root.right = insert(root.right, key, priority);
if (root.right.priority > root.priority)
root = leftRotate(root);
}
return root;
}
// Function to delete the node with the given key from the tree
private Node deleteNode(Node root, int key) {
if (root == null)
return root;
if (key < root.key)
root.left = deleteNode(root.left, key);
else if (key > root.key)
root.right = deleteNode(root.right, key);
else {
if (root.left == null) {
Node temp = root.right;
root = null;
return temp;
} else if (root.right == null) {
Node temp = root.left;
root = null;
return temp;
}
if (root.left.priority > root.right.priority) {
root = rightRotate(root);
root.right = deleteNode(root.right, key);
} else {
root = leftRotate(root);
root.left = deleteNode(root.left, key);
}
}
return root;
}
// Function to perform an inorder traversal of the tree
private void inorderTraversal(Node root) {
if (root != null) {
inorderTraversal(root.left);
System.out.println("Key: " + root.key + " Priority: " + root.priority);
inorderTraversal(root.right);
}
}
public void insert(int key, int priority) {
root = insert(root, key, priority);
}
public void delete(int key) {
root = deleteNode(root, key);
}
public void printInorder() {
System.out.println("Inorder Traversal:");
inorderTraversal(root);
}
public static void main(String[] args) {
treap_algo treap = new treap_algo();
int choice, key, priority;
Scanner scanner = new Scanner(System.in);
//Main loop to interact with the user and perform operations
while (true) {
System.out.println("1. Insert 2. Delete 3. Print Inorder 4. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter the key to insert: ");
key = scanner.nextInt();
System.out.print("Enter the priority: ");
priority = scanner.nextInt();
treap.insert(key, priority);
break;
case 2:
System.out.print("Enter the key to delete: ");
key = scanner.nextInt();
treap.delete(key);
break;
case 3:
treap.printInorder();
break;
case 4:
scanner.close();
System.exit(0);
default:
System.out.println("Invalid choice. Please try again.");
break;
}
System.out.println();
}
}
}