-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rank_of_element_in_BST.cpp
107 lines (95 loc) · 2.83 KB
/
Rank_of_element_in_BST.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
#include <bits/stdc++.h>
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 = 0; // Initialize count as 0
}
};
// Function to insert a node into the BST
Node* insertintoBST(Node* root, int d) {
// Base case: if the tree is empty, create a new node
if (root == NULL) {
root = new Node(d);
return root;
}
// 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);
}
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;
}
// Initialize the count of the current node
root->count = 1;
// Recursively count nodes in the left and right subtrees
int l1 = countNodes(root->left);
int l2 = countNodes(root->right);
// Update the count of the current node
root->count += l1 + l2;
return root->count;
}
// 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;
}
}
// Function to find the rank of a node with the given value 'x'
int rankofx(Node* root, int x) {
int r = 1;
while (root) {
if (root->data == x) {
// If the node's data matches 'x', add the count of nodes in the right subtree
r += countNodes(root->right);
return r;
}
// If the node's data is greater than 'x', move to the left subtree
else if (root->data > x) {
r += 1 + countNodes(root->right);
root = root->left;
}
// If the node's data is less than 'x', move to the right subtree
else {
root = root->right;
}
}
return r;
}
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);
// Count the nodes in the BST to update the count values
countNodes(root);
// Prompt the user to enter the value to find its rank
cout << "Rank of x" << endl;
int x;
cin >> x;
// Find and print the rank of the given value
cout << "RANK:" << endl;
int rank = rankofx(root, x);
cout << rank << endl;
return 0;
}