online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
#include <iostream> #include <string> const int MAX_NODES = 10; // Maximum number of nodes in the tree class TreeNode { public: std::string name; TreeNode* _parent; TreeNode* children[MAX_NODES]; // Array to store children int childCount; TreeNode(const std::string& nodeName) : name(nodeName), _parent(nullptr), childCount(0) { for (int i = 0; i < MAX_NODES; i++) { children[i] = nullptr; } } void addChild(TreeNode* child) { if (childCount < MAX_NODES) { children[childCount++] = child; child->_parent = this; } } }; class GeneralTree { public: TreeNode* nodes[MAX_NODES]; int nodeCount; GeneralTree() : nodeCount(0) { for (int i = 0; i < MAX_NODES; i++) { nodes[i] = nullptr; } } TreeNode* findNode(const std::string& name) { for (int i = 0; i < nodeCount; i++) { if (nodes[i]->name == name) { return nodes[i]; } } return nullptr; } void addNode(const std::string& name, const std::string& parentName) { if (nodeCount >= MAX_NODES) { std::cerr << "Maximum number of nodes reached!" << std::endl; return; } TreeNode* node = new TreeNode(name); nodes[nodeCount++] = node; if (parentName != "null") { TreeNode* parent = findNode(parentName); if (parent != nullptr) { parent->addChild(node); } else { std::cerr << "Parent node " << parentName << " not found!" << std::endl; } } } void printTree() { // Find root (node with no parent) TreeNode* root = nullptr; for (int i = 0; i < nodeCount; i++) { if (nodes[i]->_parent == nullptr) { root = nodes[i]; break; } } if (root) { printSubtree(root, 0); } else { std::cout << "No root found (tree might be empty or cyclic)" << std::endl; } } void printSubtree(TreeNode* node, int depth) { for (int i = 0; i < depth; i++) { std::cout << " "; } std::cout << node->name << std::endl; for (int i = 0; i < node->childCount; i++) { printSubtree(node->children[i], depth + 1); } } ~GeneralTree() { for (int i = 0; i < nodeCount; i++) { delete nodes[i]; } } }; int main() { GeneralTree tree; // Build the tree as described tree.addNode("E", "null"); // E is root tree.addNode("D", "E"); tree.addNode("C", "D"); tree.addNode("B", "D"); tree.addNode("A", "D"); // Print the tree structure tree.printTree(); return 0; }
#include <iostream> #include <string> const int MAX_NODES = 10; class Tree { private: struct Node { std::string name; Node* _parent; Node* children[MAX_NODES]; int childCount; Node(const std::string& nodeName) : name(nodeName), _parent(nullptr), childCount(0) { for (int i = 0; i < MAX_NODES; i++) { children[i] = nullptr; } } void addChild(Node* child) { if (childCount < MAX_NODES) { children[childCount++] = child; child->_parent = this; } } }; Node *_root; public: int nodeCount; Node* nodes[MAX_NODES]; Tree() : nodeCount(0) { for (int i = 0; i < MAX_NODES; i++) { nodes[i] = nullptr; } } Node* findNode(const std::string& name) { for (int i = 0; i < nodeCount; i++) { if (nodes[i]->name == name) { return nodes[i]; } } return nullptr; } void addNode(const std::string& name, const std::string& parentName) { if (nodeCount >= MAX_NODES) { std::cerr << "Maximum number of nodes reached!" << std::endl; return; } Node* node = new Node(name); nodes[nodeCount++] = node; if (parentName != "ROOT") { Node* parent = findNode(parentName); if (parent != nullptr) { parent->addChild(node); } else { std::cerr << "Parent node " << parentName << " not found!" << std::endl; } } } void printTree() { // Find root (node with no parent) Node* root = nullptr; for (int i = 0; i < nodeCount; i++) { if (nodes[i]->_parent == nullptr) { root = nodes[i]; break; } } if (root) { printSubtree(root, 0); } else { std::cout << "No root found (tree might be empty or cyclic)" << std::endl; } } void printSubtree(Node* node, int depth) { for (int i = 0; i < depth; i++) { std::cout << " "; } std::cout << node->name << std::endl; for (int i = 0; i < node->childCount; i++) { printSubtree(node->children[i], depth + 1); } } ~Tree() { for (int i = 0; i < nodeCount; i++) { delete nodes[i]; } } }; int main() { Tree tree; // Build the tree as described tree.addNode("E", "ROOT"); // E is root tree.addNode("D", "E"); tree.addNode("C", "D"); tree.addNode("B", "D"); tree.addNode("A", "D"); // Print the tree structure tree.printTree(); return 0; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue