Advertisement
aqibm

Untitled

Mar 20th, 2025
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using namespace std;
  5.  
  6. struct Node {
  7. string Name;
  8. string Value;
  9. vector<Node*> childNodes;
  10. };
  11.  
  12. // Merge two nodes that have the same Name.
  13. // The merged node takes its Value from t2 and its children are merged recursively.
  14. Node* merge(Node* t1, Node* t2) {
  15. if (!t1 && !t2) return nullptr;
  16. if (!t1) return t2;
  17. if (!t2) return t1;
  18.  
  19. // Create a new merged node.
  20. Node* merged = new Node;
  21. merged->Name = t1->Name; // (Assumed equal to t2->Name)
  22. merged->Value = t2->Value; // Use the value from t2
  23.  
  24. vector<Node*> mergedChildren;
  25. // We'll mark which children in t2 have been used (merged with a t1 child).
  26. vector<bool> used(t2->childNodes.size(), false);
  27.  
  28. // First, go through t1's children in order.
  29. for (Node* child1 : t1->childNodes) {
  30. int matchIndex = -1;
  31. // Look for a matching child in t2 (by Name) that has not yet been used.
  32. for (int i = 0; i < t2->childNodes.size(); i++) {
  33. if (!used[i] && t2->childNodes[i]->Name == child1->Name) {
  34. matchIndex = i;
  35. break;
  36. }
  37. }
  38. if (matchIndex != -1) {
  39. // Merge the matching children.
  40. used[matchIndex] = true;
  41. Node* mergedChild = merge(child1, t2->childNodes[matchIndex]);
  42. mergedChildren.push_back(mergedChild);
  43. } else {
  44. // No match from t2: include the t1 child as is.
  45. mergedChildren.push_back(child1);
  46. }
  47. }
  48.  
  49. // Then, append any unmatched children from t2 (in their original order).
  50. for (int i = 0; i < t2->childNodes.size(); i++) {
  51. if (!used[i]) {
  52. mergedChildren.push_back(t2->childNodes[i]);
  53. }
  54. }
  55.  
  56. merged->childNodes = mergedChildren;
  57. return merged;
  58. }
  59.  
  60. // A helper function to print the tree (for demonstration).
  61. void printTree(Node* root, int depth = 0) {
  62. if (!root)
  63. return;
  64. for (int i = 0; i < depth; i++) cout << " ";
  65. cout << root->Name << " : " << root->Value << "\n";
  66. for (Node* child : root->childNodes)
  67. printTree(child, depth + 1);
  68. }
  69.  
  70. int main(){
  71. // Build an example based on the given sample.
  72. // Document t1:
  73. // Root : t1
  74. // ├── A : alpha
  75. // │ ├── B : beta
  76. // │ └── C : Ceta
  77. // ├── C : xz
  78. // ├── D : zy
  79. // └── A : dolce
  80. Node* t1 = new Node{"Root", "t1", {}};
  81. Node* A1 = new Node{"A", "alpha", {}};
  82. Node* B = new Node{"B", "beta", {}};
  83. Node* C1 = new Node{"C", "Ceta", {}};
  84. A1->childNodes.push_back(B);
  85. A1->childNodes.push_back(C1);
  86. Node* C2 = new Node{"C", "xz", {}};
  87. Node* D = new Node{"D", "zy", {}};
  88. Node* A2 = new Node{"A", "dolce", {}};
  89. t1->childNodes.push_back(A1);
  90. t1->childNodes.push_back(C2);
  91. t1->childNodes.push_back(D);
  92. t1->childNodes.push_back(A2);
  93.  
  94. // Document t2:
  95. // Root : t2
  96. // ├── A : beta
  97. // │ └── X : lk
  98. // └── E : theta
  99. Node* t2 = new Node{"Root", "t2", {}};
  100. Node* A3 = new Node{"A", "beta", {}};
  101. Node* X = new Node{"X", "lk", {}};
  102. A3->childNodes.push_back(X);
  103. Node* E = new Node{"E", "theta", {}};
  104. t2->childNodes.push_back(A3);
  105. t2->childNodes.push_back(E);
  106.  
  107. // Merge the two documents at the root.
  108. // We assume the root nodes are merged by merging their children.
  109. Node* mergedRoot = merge(t1, t2);
  110.  
  111. // Print the merged tree.
  112. printTree(mergedRoot);
  113.  
  114. return 0;
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement