-
Notifications
You must be signed in to change notification settings - Fork 0
/
Element_of_given_rank.cpp
121 lines (107 loc) · 3.26 KB
/
Element_of_given_rank.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
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
#include <iostream>
using namespace std;
// Definition of the Node class
class Node {
public:
int data;
Node* left;
Node* right;
int count; // Count of nodes in the subtree rooted at this node
// Constructor to initialize a node with given data
Node(int d) {
this->data = d;
this->right = NULL;
this->left = NULL;
this->count = 1; // Initialize count as 1 for the current node
}
};
// Function to insert a node into the BST and update the node counts
Node* insertintoBST(Node* root, int d) {
if (root == NULL) {
return new Node(d); // Create a new node if the tree is empty
}
// Insert the node in the right subtree if the data is greater than the root's data
if (d > root->data) {
root->right = insertintoBST(root->right, d);
}
// Insert the node in the left subtree if the data is less than or equal to the root's data
else {
root->left = insertintoBST(root->left, d);
}
// Increment the count of nodes in the subtree
root->count++;
return root;
}
// Function to count the total number of nodes in the subtree rooted at 'root'
int countNodes(Node* root) {
if (root == NULL) {
return 0;
}
// Recursively count nodes in the left and right subtrees
int l1 = countNodes(root->left);
int l2 = countNodes(root->right);
// Return the sum of counts in both subtrees plus one for the current node
return 1 + l1 + l2;
}
// Function to find the node with the given rank 'x'
Node* elementofrankx(Node* root, int x) {
if (root == NULL) {
return NULL;
}
while (root) {
// If the right subtree is not NULL
if (root->right != NULL) {
// Check if the rank of the current node matches 'x'
if (x == 1 + root->right->count) {
return root;
}
// If 'x' is less than the rank of the current node, move to the right subtree
if (x < 1 + root->right->count) {
root = root->right;
}
// Otherwise, move to the left subtree and adjust 'x'
else {
x = x - 1 - root->right->count;
root = root->left;
}
}
// If the right subtree is NULL
else {
// Check if 'x' is the rank of the current node
if (x == 1) {
return root;
}
// Decrement 'x' to move to the left subtree
x--;
root = root->left;
}
}
return root;
}
// Function to take input from the user and create a BST
void takeInput(Node* &root) {
int data;
cin >> data;
while (data != -1) {
root = insertintoBST(root, data);
cin >> data;
}
}
int main() {
Node* root = NULL;
// Prompt the user to enter data to create the BST
cout << "Enter data to create a Binary Search Tree" << endl;
takeInput(root);
// Prompt the user to enter the rank
cout << "Given Rank" << endl;
int r;
cin >> r;
// Find and print the element with the given rank
cout << "Element:" << endl;
Node* result = elementofrankx(root, r);
if (result)
cout << result->data << endl;
else
cout << "Rank out of bounds" << endl;
return 0;
}