
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Query for ancestor-descendant relationship in a tree in C++ Program
In this problem, we are given an N vertex tree and Q queries each consisting of two values i and j. Our task is to create a program to solve a Query for ancestor-descendant relationship in a tree.
To solve each query, we need to check whether the node i is the ancestor of node j in the tree.
Let’s take an example to understand the problem,
Input
Q = 2, query[][] = {{3, 5}, {1, 6}}
Output
No Yes
Explanation
i = 3, j = 5: The node 3 is not the ancestor of node 5. Return NO. i = 1, j = 6: The node 1 is the ancestor of node 6. Return YES.
Solution Approach
A solution to the problem is using tree traversing technique which is DFS (depth first Search). We will traverse from the ith node and do down to all its descendants and then check if the jth node is descendants of it. An efficient way to solve the problem is by finding out the time-in and time-out of each node. And check if they share ancestor-descendant relations.
Program to illustrate the working of our solution,
Example
#include <bits/stdc++.h> using namespace std; void depthFirstSearch(vector<int> g[], int u, int parent, int entTime[], int exitTime[], int& cnt){ entTime[u] = cnt++; for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if (v != parent) depthFirstSearch(g, v, u, entTime, exitTime, cnt); } exitTime[u] = cnt++; } void calcTimeInAndOut(int edges[][2], int V, int entTime[], int exitTime[]){ vector<int> g[V]; for (int i = 0; i < V - 1; i++) { int u = edges[i][0]; int v = edges[i][1]; g[u].push_back(v); g[v].push_back(u); } int cnt = 0; depthFirstSearch(g, 0, -1, entTime, exitTime, cnt); } int main(){ int edges[][2] = { { 0, 1 }, { 0, 2 }, { 1, 3 }, { 1, 4 }, { 4, 5 }, { 5, 6 }, { 5, 7 }}; int E = sizeof(edges) / sizeof(edges[0]); int V = E + 1; int Q = 2; int query[Q][2] = {{3, 5}, {1, 6}}; int entTime[V], exitTime[V]; calcTimeInAndOut(edges, V, entTime, exitTime); for(int i = 0; i < Q; i++){ cout<<"For query "<<(i+1)<<" : "; (entTime[query[i][0]] <= entTime[query[i][1]] && exitTime[query[i][0]] <= exitTime[query[i][1]])? cout<<"is Ancestor\n":cout<<"is not Ancestor\n"; } return 0; }
Output
For query 1 : is Ancestor For query 2 : is not Ancestor
- Related Articles
- Query for ancestor-descendant relationship in a tree in C++
- C++ Program to Find Lowest Common Ancestor in a Binary Search Tree
- Program to find Kth ancestor of a tree node in Python
- Lowest Common Ancestor of a Binary Tree in Python
- Lowest Common Ancestor of a Binary Search Tree in Python
- Program for Christmas Tree in C
- Program to find an ancestor which is common of two elements in a binary tree in Python
- Program to find out the lowest common ancestor of a binary tree using Python
- C++ Program to Search for an Element in a Binary Search Tree
- Program to find out the lowest common ancestor of a binary tree using parent pointers using Python
- Program to find out the lowest common ancestor of a binary tree of given nodes using Python
- Deletion in a Binary Tree in C++ Program
- Descendant Selectors in CSS
- C++ Program to Create the Prufer Code for a Tree
- Create a mirror tree from the given binary tree in C++ Program
