Skip to content

Commit 7502dc0

Browse files
committed
.
1 parent ae23cd7 commit 7502dc0

File tree

7 files changed

+233
-1
lines changed

7 files changed

+233
-1
lines changed

.vscode/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"files.associations": {
3+
"iostream": "cpp",
4+
"ostream": "cpp",
5+
"new": "cpp"
6+
}
7+
}

.vscode/tasks.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"tasks": [
3+
{
4+
"type": "cppbuild",
5+
"label": "C/C++: g++.exe build active file",
6+
"command": "C:\\MinGW\\bin\\g++.exe",
7+
"args": [
8+
"-fdiagnostics-color=always",
9+
"-g",
10+
"${file}",
11+
"-o",
12+
"${fileDirname}\\${fileBasenameNoExtension}.exe"
13+
],
14+
"options": {
15+
"cwd": "${fileDirname}"
16+
},
17+
"problemMatcher": [
18+
"$gcc"
19+
],
20+
"group": {
21+
"kind": "build",
22+
"isDefault": true
23+
},
24+
"detail": "Task generated by Debugger."
25+
}
26+
],
27+
"version": "2.0.0"
28+
}

Assignments/7_BST_Menu.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ int main()
162162
}
163163
case 2:
164164
{
165-
cout << "Enter the key to search: ";
165+
cout << "Enter the key to search: ";
166166
cin >> data;
167167
struct node *result = search(root, data);
168168
if (result == NULL)

Final/Max_Heap.txt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
#include <limits.h>
3+
#include <stdio.h>
4+
#include <stdbool.h>
5+
6+
// Returns true if arr[i..n-1] represents a max-heap
7+
bool isHeap(int arr[], int i, int n)
8+
{
9+
// Base case: If a node is a leaf node, return true
10+
if (i >= (n - 2) / 2)
11+
return true;
12+
13+
// Compare the current node with its children
14+
// If the current node is greater than or equal to its children
15+
// and the left and right subtrees are also max-heaps, return true
16+
if (arr[i] >= arr[2 * i + 1] &&
17+
arr[i] >= arr[2 * i + 2] &&
18+
isHeap(arr, 2 * i + 1, n) &&
19+
isHeap(arr, 2 * i + 2, n))
20+
return true;
21+
22+
// If any of the above conditions fails, return false
23+
return false;
24+
}
25+
26+
int main()
27+
{
28+
int arr[] = {90, 15, 10, 7, 12, 2, 7, 3};
29+
int n = sizeof(arr) / sizeof(int) - 1;
30+
31+
isHeap(arr, 0, n) ? printf("Yes") : printf("No");
32+
33+
return 0;
34+
}
35+
36+
1. Initially, `isHeap(arr, 0, n)` is called with `i` as 0 and `n` as 7 (since `n` is the size of the array minus 1).
37+
2. In the first recursive call, it checks the condition for the root node (index 0):
38+
39+
- `arr[0]` is 90
40+
- `arr[2 * 0 + 1]` is 15
41+
- `arr[2 * 0 + 2]` is 10
42+
43+
Since 90 is greater than or equal to both its children, it proceeds to check the left and right subtrees.
44+
3. In the left subtree (`isHeap(arr, 2 * 0 + 1, n)`), it checks the condition for the node at index 1:
45+
46+
- `arr[1]` is 15
47+
- `arr[2 * 1 + 1]` is 7
48+
- `arr[2 * 1 + 2]` is 12
49+
50+
Since 15 is greater than or equal to both its children, it proceeds to check their subtrees.
51+
4. The recursion continues in a similar manner for the left subtree and then the right subtree.
52+
53+
Now, let's calculate the output:
54+
55+
- All nodes satisfy the max-heap property, so it returns `true`.
56+
57+
As a result, the program will print "Yes" because the given array represents a binary max-heap.
58+
59+
The output of the program is: "Yes".
1.87 MB
Binary file not shown.
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
class RadixTreeNode {
5+
public:
6+
std::unordered_map<char, RadixTreeNode*> children;
7+
bool isEndOfWord;
8+
9+
RadixTreeNode() : isEndOfWord(false) {}
10+
11+
~RadixTreeNode() {
12+
for (auto& pair : children) {
13+
delete pair.second;
14+
}
15+
}
16+
};
17+
18+
class RadixTree {
19+
private:
20+
RadixTreeNode* root;
21+
22+
public:
23+
RadixTree() {
24+
root = new RadixTreeNode();
25+
}
26+
27+
~RadixTree() {
28+
delete root;
29+
}
30+
31+
// Insert a string into the radix tree
32+
void insert(const std::string& key) {
33+
RadixTreeNode* node = root;
34+
35+
for (char c : key) {
36+
if (node->children.find(c) == node->children.end()) {
37+
node->children[c] = new RadixTreeNode();
38+
}
39+
node = node->children[c];
40+
}
41+
42+
node->isEndOfWord = true;
43+
}
44+
45+
// Search for a string in the radix tree
46+
bool search(const std::string& key) {
47+
RadixTreeNode* node = root;
48+
49+
for (char c : key) {
50+
if (node->children.find(c) == node->children.end()) {
51+
return false; // Character not found in the tree
52+
}
53+
node = node->children[c];
54+
}
55+
56+
return node->isEndOfWord; // Return true if it's an end-of-word node
57+
}
58+
59+
// Delete a string from the radix tree
60+
void remove(const std::string& key) {
61+
removeHelper(root, key, 0);
62+
}
63+
64+
private:
65+
// Recursive helper function for deletion
66+
bool removeHelper(RadixTreeNode* node, const std::string& key, int depth) {
67+
if (node == nullptr) {
68+
return false; // Key not found
69+
}
70+
71+
if (depth == key.length()) {
72+
if (node->isEndOfWord) {
73+
node->isEndOfWord = false; // Mark as non-end-of-word
74+
return true; // Key found and marked as deleted
75+
}
76+
return false; // Key not found
77+
}
78+
79+
char currentChar = key[depth];
80+
if (removeHelper(node->children[currentChar], key, depth + 1)) {
81+
// If child node is marked as deleted, check if it can be removed
82+
if (node->children[currentChar]->children.empty() && !node->children[currentChar]->isEndOfWord) {
83+
delete node->children[currentChar];
84+
node->children.erase(currentChar);
85+
}
86+
return true;
87+
}
88+
return false;
89+
}
90+
};
91+
92+
int main() {
93+
RadixTree tree;
94+
std::string choice;
95+
96+
while (true) {
97+
std::cout << "\nRadix Tree Menu:\n";
98+
std::cout << "1. Insert a string\n";
99+
std::cout << "2. Delete a string\n";
100+
std::cout << "3. Search for a string\n";
101+
std::cout << "4. Exit\n";
102+
std::cout << "Enter your choice: ";
103+
std::cin >> choice;
104+
105+
if (choice == "1") {
106+
std::string key;
107+
std::cout << "Enter a string to insert: ";
108+
std::cin >> key;
109+
tree.insert(key);
110+
std::cout << "'" << key << "' inserted into the tree.\n";
111+
} else if (choice == "2") {
112+
std::string key;
113+
std::cout << "Enter a string to delete: ";
114+
std::cin >> key;
115+
if (tree.search(key)) {
116+
tree.remove(key);
117+
std::cout << "'" << key << "' deleted from the tree.\n";
118+
} else {
119+
std::cout << "'" << key << "' not found in the tree.\n";
120+
}
121+
} else if (choice == "3") {
122+
std::string key;
123+
std::cout << "Enter a string to search: ";
124+
std::cin >> key;
125+
if (tree.search(key)) {
126+
std::cout << "'" << key << "' found in the tree.\n";
127+
} else {
128+
std::cout << "'" << key << "' not found in the tree.\n";
129+
}
130+
} else if (choice == "4") {
131+
break; // Exit the program
132+
} else {
133+
std::cout << "Invalid choice. Please try again.\n";
134+
}
135+
}
136+
137+
return 0;
138+
}
146 KB
Binary file not shown.

0 commit comments

Comments
 (0)