To implement a function that finds the inorder successor of a specified students name in the BST structure representing the school's student database. Program: #include <iostream> #include <string> using namespace std; struct Node { string key; // Student's name Node* left; Node* right; Node(string val ) : key( val ), left( nullptr ), right( nullptr ) {} };
if (target->right) { return findMin (target->right); } Node* successor = nullptr ; Node* current = root; while (current) { if (target->key < current->key) { successor = current; current = current->left; } else if (target->key > current->key) { current = current->right; } else { break; } } return successor; }
int main() { Node* root = nullptr ; root = insert(root, "Alice"); root = insert(root, "Bob"); root = insert(root, "Charlie"); root = insert(root, "Diana"); root = insert(root, "Eve"); Node* target = root->left; // Example: "Bob" Node* successor = findInorderSuccessor (root, target); if (successor) cout << " Inorder Successor of " << target->key << " is " << successor->key << endl ; else cout << "No Inorder Successor for " << target->key << endl ; return 0; }
Question: Develop a library book management system using an AVL tree to keep track of books.Each book has a unique ISBN, title, and author.Implement the AVL tree operations for the Dictionary ADT to manage book records.Insert multiple book records into the AVL tree and ensure it remains balanced.Search for a book by ISBN and display its details.Update a book's author and ensure the tree remains balanced.Sample Input:Insert : ("978-3-16-148410-0", "Book A", "Author A"), ("978-0-306-40615-7", "Book B","Author B"), ("978-1-4028-9462-6", "Book C", "Author C")Search: "978-0-306-40615-7"Update: (978-0-306-40615-7", "Book B", "Author X*)Sample Output:Inserted : ("978-3-16-148410-0", "Book A", "Author A")Inserted: ("978-0-306-40615-7", "Book B", "Author B")Inserted: "978-1-4028-9462-6"Found: ("978-0-306-40615-7","Book C", "Author C"), "Book B", "Author B')Updated: (978-0-306-40615-7", "Book B", "Author X)
Program: #include <iostream> #include <string> using namespace std; struct BookNode { string isbn ; string title; string author; int height; BookNode * left; BookNode * right; BookNode (string i , string t, string a) : isbn ( i ), title(t), author(a), height(1), left( nullptr ), right( nullptr ) {} }; int getHeight ( BookNode * node) { return node ? node->height : 0; }
Question: Given a graph G = (V,E) with n vertices and m edges, and a subset T of I vertices calledterminals , the Edge (respectively, Vertex) Multiterminal Cut problem is to find a set of k edges (non-terminal vertices), whose removal from G separates each terminal from all the others
#include <iostream> #include <vector> #include <queue> #include <limits> #include <set> #include <map> using namespace std; struct Edge { int to, capacity, flow, rev; }; class Graph { private: int V; vector<vector<Edge>> adj ; public: Graph(int vertices) : V(vertices), adj (vertices) {}
void addEdge (int u, int v, int capacity) { Edge a{v, capacity, 0, (int) adj [v].size()}; Edge b{u, 0, 0, (int) adj [u].size()}; adj [u]. push_back (a); adj [v]. push_back (b); } bool bfs (int s, int t, vector<int>& parent) { vector<bool> visited(V, false); queue<int> q; q.push (s); visited[s] = true; while (! q.empty ()) { int u = q.front (); q.pop ();
for (int i = 0; i < adj[u].size(); i++) { Edge& e = adj [u][ i ]; if (!visited[e.to] && e.capacity > e.flow ) { parent[e.to] = u; visited[e.to] = true; if (e.to == t) return true; q.push (e.to); } } } return false; } int fordFulkerson (int s, int t) { vector<int> parent(V); int maxFlow = 0;
while (bfs(s, t, parent)) { int pathFlow = numeric_limits <int>::max(); for (int v = t; v != s; v = parent[v]) { int u = parent[v]; for (Edge& e : adj[u]) { if (e.to == v && e.capacity > e.flow) { pathFlow = min(pathFlow, e.capacity - e.flow); break; } } } for (int v = t; v != s; v = parent[v]) { int u = parent[v]; for (Edge& e : adj[u]) { if (e.to == v) { e.flow += pathFlow; adj[v][e.rev].flow -= pathFlow; break; } } }