Dado un árbol binario con la estructura de Nodes que contiene una parte de datos, punteros izquierdo y derecho y un puntero arbitrario (abtr). El valor del Node puede ser cualquier entero positivo. El problema es crear bucles pares e impares en un árbol binario. Un ciclo impar es un ciclo que conecta todos los Nodes que tienen números impares y, de manera similar, un ciclo par es para los Nodes que tienen números pares. Para crear dichos bucles, se utiliza el puntero abtr de cada Node. Un puntero abtr de un Node impar (Node que tiene un número impar) apunta a algún otro Node impar en el árbol. Un bucle debe crearse de tal manera que desde cualquier Node podamos recorrer todos los Nodes del bucle al que pertenece el Node.
Ejemplos:
Consider the binary tree given below 1 / \ 2 3 / \ / \ 4 5 6 7 Now with the help of abtr pointers of node, we connect odd and even nodes as: Odd loop 1 -> 5 -> 3 -> 7 -> 1(again pointing to first node in the loop) Even loop 2 -> 4 -> 6 -> 2(again pointing to first node in the loop) Nodes in the respective loop can be arranged in any order. But from any node in the loop we should be able to traverse all the nodes in the loop.
Enfoque: Los siguientes pasos son:
- Agregue punteros de los Nodes que tienen números pares e impares a las arrays even_ptrs y odd_ptrs respectivamente. A través de cualquier recorrido del árbol, podríamos obtener los respectivos punteros de Node.
- Para la array even_ptrs y odd_ptrs , realice:
- Como la array contiene punteros de Node, considere un elemento en el i-ésimo índice, sea node , y el Node asignado->abtr = elemento en (i+1)-ésimo índice.
- Para el último elemento de la array, Node->abtr = elemento en el índice 0.
Implementación:
CPP
// C++ implementation to create odd and even loops // in a binary tree #include <bits/stdc++.h> using namespace std; // structure of a node struct Node { int data; Node *left, *right, *abtr; }; // Utility function to create a new node struct Node* newNode(int data) { struct Node* node = new Node; node->data = data; node->left = node->right = node->abtr = NULL; return node; } // preorder traversal to place the node pointer // in the respective even_ptrs or odd_ptrs list void preorderTraversal(Node *root, vector<Node*> *even_ptrs, vector<Node*> *odd_ptrs) { if (!root) return; // place node ptr in even_ptrs list if // node contains even number if (root->data % 2 == 0) (*even_ptrs).push_back(root); // else place node ptr in odd_ptrs list else (*odd_ptrs).push_back(root); preorderTraversal(root->left, even_ptrs, odd_ptrs); preorderTraversal(root->right, even_ptrs, odd_ptrs); } // function to create the even and odd loops void createLoops(Node *root) { vector<Node*> even_ptrs, odd_ptrs; preorderTraversal(root, &even_ptrs, &odd_ptrs); int i; // forming even loop for (i=1; i<even_ptrs.size(); i++) even_ptrs[i-1]->abtr = even_ptrs[i]; // for the last element even_ptrs[i-1]->abtr = even_ptrs[0]; // Similarly forming odd loop for (i=1; i<odd_ptrs.size(); i++) odd_ptrs[i-1]->abtr = odd_ptrs[i]; odd_ptrs[i-1]->abtr = odd_ptrs[0]; } // traversing the loop from any random // node in the loop void traverseLoop(Node *start) { Node *curr = start; do { cout << curr->data << " "; curr = curr->abtr; } while (curr != start); } // Driver program to test above int main() { // Binary tree formation struct Node* root = NULL; root = newNode(1); /* 1 */ root->left = newNode(2); /* / \ */ root->right = newNode(3); /* 2 3 */ root->left->left = newNode(4); /* / \ / \ */ root->left->right = newNode(5); /* 4 5 6 7 */ root->right->left = newNode(6); root->right->right = newNode(7); createLoops(root); // traversing odd loop from any // random odd node cout << "Odd nodes: "; traverseLoop(root->right); cout << endl << "Even nodes: "; // traversing even loop from any // random even node traverseLoop(root->left); return 0; }
Java
// Java implementation to create odd and even loops // in a binary tree import java.util.*; class GFG { // structure of a node static class Node { int data; Node left, right, abtr; }; // Utility function to create a new node static Node newNode(int data) { Node node = new Node(); node.data = data; node.left = node.right = node.abtr = null; return node; } static Vector<Node> even_ptrs = new Vector<>(); static Vector<Node> odd_ptrs = new Vector<>(); // preorder traversal to place the node pointer // in the respective even_ptrs or odd_ptrs list static void preorderTraversal(Node root) { if (root == null) return; // place node ptr in even_ptrs list if // node contains even number if (root.data % 2 == 0) (even_ptrs).add(root); // else place node ptr in odd_ptrs list else (odd_ptrs).add(root); preorderTraversal(root.left); preorderTraversal(root.right); } // function to create the even and odd loops static void createLoops(Node root) { preorderTraversal(root); int i; // forming even loop for (i = 1; i < even_ptrs.size(); i++) even_ptrs.get(i - 1).abtr = even_ptrs.get(i); // for the last element even_ptrs.get(i - 1).abtr = even_ptrs.get(0); // Similarly forming odd loop for (i = 1; i < odd_ptrs.size(); i++) odd_ptrs.get(i - 1).abtr = odd_ptrs.get(i); odd_ptrs.get(i - 1).abtr = odd_ptrs.get(0); } // traversing the loop from any random // node in the loop static void traverseLoop(Node start) { Node curr = start; do { System.out.print(curr.data + " "); curr = curr.abtr; } while (curr != start); } // Driver code public static void main(String[] args) { // Binary tree formation Node root = null; root = newNode(1); /* 1 */ root.left = newNode(2); /* / \ */ root.right = newNode(3); /* 2 3 */ root.left.left = newNode(4); /* / \ / \ */ root.left.right = newNode(5); /* 4 5 6 7 */ root.right.left = newNode(6); root.right.right = newNode(7); createLoops(root); // traversing odd loop from any // random odd node System.out.print("Odd nodes: "); traverseLoop(root.right); System.out.print( "\nEven nodes: "); // traversing even loop from any // random even node traverseLoop(root.left); } } // This code is contributed by aashish1995
Python3
# Python3 implementation to create odd and even loops # in a binary tree # structure of a node class Node: def __init__(self, x): self.data = x self.left = None self.right = None self.abtr = None even_ptrs = [] odd_ptrs = [] # preorder traversal to place the node pointer # in the respective even_ptrs or odd_ptrs list def preorderTraversal(root): global even_ptrs, odd_ptrs if (not root): return # place node ptr in even_ptrs list if # node contains even number if (root.data % 2 == 0): even_ptrs.append(root) # else place node ptr in odd_ptrs list else: odd_ptrs.append(root) preorderTraversal(root.left) preorderTraversal(root.right) # function to create the even and odd loops def createLoops(root): preorderTraversal(root) # forming even loop i = 1 while i < len(even_ptrs): even_ptrs[i - 1].abtr = even_ptrs[i] i += 1 # for the last element even_ptrs[i - 1].abtr = even_ptrs[0] # Similarly forming odd loop i = 1 while i < len(odd_ptrs): odd_ptrs[i - 1].abtr = odd_ptrs[i] i += 1 odd_ptrs[i - 1].abtr = odd_ptrs[0] #traversing the loop from any random #node in the loop def traverseLoop(start): curr = start while True and curr: print(curr.data, end = " ") curr = curr.abtr if curr == start: break print() # Driver program to test above if __name__ == '__main__': # Binary tree formation root = None root = Node(1) #/* 1 */ root.left = Node(2) # /* / \ */ root.right = Node(3) #/* 2 3 */ root.left.left = Node(4) #/* / \ / \ */ root.left.right = Node(5) #/* 4 5 6 7 */ root.right.left = Node(6) root.right.right = Node(7) createLoops(root) # traversing odd loop from any # random odd node print("Odd nodes:", end = " ") traverseLoop(root.right) print("Even nodes:", end = " ") # traversing even loop from any # random even node traverseLoop(root.left) # This code is contributed by mohit kumar 29
C#
// C# implementation to create odd and even loops // in a binary tree using System; using System.Collections.Generic; class GFG { // structure of a node public class Node { public int data; public Node left, right, abtr; }; // Utility function to create a new node static Node newNode(int data) { Node node = new Node(); node.data = data; node.left = node.right = node.abtr = null; return node; } static List<Node> even_ptrs = new List<Node>(); static List<Node> odd_ptrs = new List<Node>(); // preorder traversal to place the node pointer // in the respective even_ptrs or odd_ptrs list static void preorderTraversal(Node root) { if (root == null) return; // place node ptr in even_ptrs list if // node contains even number if (root.data % 2 == 0) (even_ptrs).Add(root); // else place node ptr in odd_ptrs list else (odd_ptrs).Add(root); preorderTraversal(root.left); preorderTraversal(root.right); } // function to create the even and odd loops static void createLoops(Node root) { preorderTraversal(root); int i; // forming even loop for (i = 1; i < even_ptrs.Count; i++) even_ptrs[i - 1].abtr = even_ptrs[i]; // for the last element even_ptrs[i - 1].abtr = even_ptrs[0]; // Similarly forming odd loop for (i = 1; i < odd_ptrs.Count; i++) odd_ptrs[i - 1].abtr = odd_ptrs[i]; odd_ptrs[i - 1].abtr = odd_ptrs[0]; } // traversing the loop from any random // node in the loop static void traverseLoop(Node start) { Node curr = start; do { Console.Write(curr.data + " "); curr = curr.abtr; } while (curr != start); } // Driver code public static void Main(String[] args) { // Binary tree formation Node root = null; root = newNode(1); /* 1 */ root.left = newNode(2); /* / \ */ root.right = newNode(3); /* 2 3 */ root.left.left = newNode(4); /* / \ / \ */ root.left.right = newNode(5); /* 4 5 6 7 */ root.right.left = newNode(6); root.right.right = newNode(7); createLoops(root); // traversing odd loop from any // random odd node Console.Write("Odd nodes: "); traverseLoop(root.right); Console.Write( "\nEven nodes: "); // traversing even loop from any // random even node traverseLoop(root.left); } } // This code is contributed by gauravrajput1
Javascript
<script> // Javascript implementation to // create odd and even loops // in a binary tree // structure of a node class Node { // Utility function to create a new node constructor(data) { this.data=data; this.left=this.right=this.abtr =null; } } let even_ptrs = []; let odd_ptrs = []; // preorder traversal to place the node pointer // in the respective even_ptrs or odd_ptrs list function preorderTraversal(root) { if (root == null) return; // place node ptr in even_ptrs list if // node contains even number if (root.data % 2 == 0) (even_ptrs).push(root); // else place node ptr in odd_ptrs list else (odd_ptrs).push(root); preorderTraversal(root.left); preorderTraversal(root.right); } // function to create the even and odd loops function createLoops(root) { preorderTraversal(root); let i; // forming even loop for (i = 1; i < even_ptrs.length; i++) even_ptrs[i-1].abtr = even_ptrs[i]; // for the last element even_ptrs[i-1].abtr = even_ptrs[0]; // Similarly forming odd loop for (i = 1; i < odd_ptrs.length; i++) odd_ptrs[i-1].abtr = odd_ptrs[i]; odd_ptrs[i-1].abtr = odd_ptrs[0]; } // traversing the loop from any random // node in the loop function traverseLoop(start) { let curr = start; do { document.write(curr.data + " "); curr = curr.abtr; } while (curr != start); } // Driver code /* 1 */ /* / \ */ /* 2 3 */ /* / \ / \ */ /* 4 5 6 7 */ // Binary tree formation let root = null; root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.left.right = new Node(5); root.right.left = new Node(6); root.right.right = new Node(7); createLoops(root); // traversing odd loop from any // random odd node document.write("Odd nodes: "); traverseLoop(root.right); document.write( "<br>Even nodes: "); // traversing even loop from any // random even node traverseLoop(root.left); // This code is contributed by patel2127 </script>
Odd nodes: 3 7 1 5 Even nodes: 2 4 6
Complejidad de tiempo: igual a la complejidad de tiempo de cualquier recorrido de árbol recursivo que sea O (n)
Este artículo es una contribución de Ayush Jauhari . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA