private static GeneralTreeNode <int> FindNodeUsingDFS(GeneralTreeNode <int> node, int inputNumber) { node.Visited = true; if (node == null) { return(null); } if (inputNumber == node.Value) { return(node); } else { foreach (var adjNode in node.ChildNodes) { // checking for visited is not required for a tree as its not cyclic but is required for a graph var resultNode = FindNodeUsingDFS(adjNode, inputNumber); if (resultNode != null) { return(resultNode); } continue; } } return(null); }
private static GeneralTreeNode <int> FindNodeUsingBFS(GeneralTreeNode <int> root, int inputNumber) { GeneralTreeNode <int> nodeFound; Queue <GeneralTreeNode <int> > queue = new Queue <GeneralTreeNode <int> >(); queue.Enqueue(root); while (queue.Count != 0) { var node = queue.Dequeue(); node.Visited = true; if (inputNumber == node.Value) { return(node); } foreach (var adjNode in node.ChildNodes) { // checking for visited is not required for a tree as its not cyclic but is required for a graph if (!adjNode.Visited) { queue.Enqueue(adjNode); } } } return(null); }
/* * Nodes in a Subtree * You are given a tree that contains N nodes, each containing an integer u which corresponds to a lowercase character c * in the string s using 1-based indexing. * You are required to answer Q queries of type [u, c], where u is an integer and c is a lowercase letter. * The query result is the number of nodes in the subtree of node u containing c. * Signature * int[] countOfNodes(Node root, ArrayList<Query> queries, String s) * Input * A pointer to the root node, an array list containing Q queries of type [u, c], and a string s * Constraints * N and Q are the integers between 1 and 1,000,000 * u is a unique integer between 1 and N * s is of the length of N, containing only lowercase letters * c is a lowercase letter contained in string s * Node 1 is the root of the tree * Output * An integer array containing the response to each query * Example * 1(a) * / \ * 2(b) 3(a) * s = "aba" * RootNode = 1 * query = [[1, 'a']] * Note: Node 1 corresponds to first letter 'a', Node 2 corresponds to second letter of the string 'b', Node 3 corresponds to third letter of the string 'a'. * output = [2] * Both Node 1 and Node 3 contain 'a', so the number of nodes within the subtree of Node 1 containing 'a' is 2. */ // Used as Linked list nodes public static int[] CountOfNodes(GeneralTreeNode <int> root, List <Query> queries, String s) { var result = new List <int>(); // find the sub node foreach (var query in queries) { var inputNumber = query.U; var inputChar = query.C; int counter = 0; // search the tree for this number, user a breadthfirst approach var nodeFound = FindNodeUsingDFS(root, inputNumber); GeneralTreeNode <int> .TraverseBFS(nodeFound, (node) => { var pos = node.Value; if (s[pos - 1] == inputChar) { counter++; } }); result.Add(counter); } return(result.ToArray()); }
public static void TraverseDFS(GeneralTreeNode <T> node, Action <GeneralTreeNode <T> > func) { if (node != null) { func(node); foreach (var childNode in node.ChildNodes) { TraverseDFS(childNode, func); } } }
public static void TraverseBFS(GeneralTreeNode <T> node, Action <GeneralTreeNode <T> > func) { if (node != null) { var queue = new Queue <GeneralTreeNode <T> >(); queue.Enqueue(node); while (queue.Count != 0) { var workNode = queue.Dequeue(); //visit the node func(workNode); // push all its children in its queue foreach (var childNode in workNode.ChildNodes) { queue.Enqueue(childNode); } } } }
public void TestcountOfNodes() { // Testcase 2 int n_2 = 7, q_2 = 3; String s_2 = "abaacab"; GeneralTreeNode <int> root_2 = new GeneralTreeNode <int>(1); root_2.ChildNodes.Add(new GeneralTreeNode <int>(2)); root_2.ChildNodes.Add(new GeneralTreeNode <int>(3)); root_2.ChildNodes.Add(new GeneralTreeNode <int>(7)); root_2.ChildNodes[0].ChildNodes.Add(new GeneralTreeNode <int>(4)); root_2.ChildNodes[0].ChildNodes.Add(new GeneralTreeNode <int>(5)); root_2.ChildNodes[1].ChildNodes.Add(new GeneralTreeNode <int>(6)); var queries_2 = new List <Query>(); queries_2.Add(new Query(1, 'a')); queries_2.Add(new Query(2, 'b')); queries_2.Add(new Query(3, 'a')); int[] output_2 = TreesAndGraphs.CountOfNodes(root_2, queries_2, s_2); int[] expected_2 = { 4, 1, 2 }; Assert.AreEqual(expected_2, output_2); //Testcase 1 int n_1 = 3, q_1 = 1; String s_1 = "aba"; GeneralTreeNode <int> root_1 = new GeneralTreeNode <int>(1); root_1.ChildNodes.Add(new GeneralTreeNode <int>(2)); root_1.ChildNodes.Add(new GeneralTreeNode <int>(3)); var queries_1 = new List <Query>(); queries_1.Add(new Query(1, 'a')); int[] output_1 = TreesAndGraphs.CountOfNodes(root_1, queries_1, s_1); int[] expected_1 = { 2 }; Assert.AreEqual(expected_1, output_1); }