# Count Total No. of Ancestors in a Binary Search Tree

In a binary search tree, an ancestor of a node is any node on the path from the root to that node. Specifically, a node A is an ancestor of a node B if A is on the path from the root to B and A is higher than B in the tree (i.e., A is closer to the root). In other words, any node that is a parent, grandparent, great-grandparent, etc., of a node B in the tree is considered an ancestor of B.

The concept of ancestors is important in binary search trees because it is often useful to traverse the tree from the root to a particular node while keeping track of the ancestors along the way. This can be useful, for example, in algorithms that involve searching for a node, inserting a new node, or deleting a node from the tree.

### Implementation

``````// Writing a C++ program to find out the above approach for the following
#include <bits/stdc++.h>
using namespace std;

// Writing a function to add an edge between the nodes u and v
int u, int v)
{
}

// Creating a function that will perform the DFS traversal for the following code and store the parent of each of the nodes.
void dfs(vector<int>& parent,
int u,
int par = -1)
{
// we have to store the parent node
parent[u] = par;

// now, we will have to commute to the child node
for (auto child : adj[u]) {

// We have to recursively admit a function for the DFS traversal of the child node
if (child != par)
}
}
// Creating a function that will count the total number of ancestors that has a smaller value than the current node
void countSmallerAncestors(
{
// We have to store the parent of each node present
vector<int> parent(int(1e5), 0);

// Now, we will perform the DFS traversal for each node.

// we have to traverse all the nodes present
for (int i = 1; i <= n; i++) {

int node = i;

// Now, we have to store the total number of ancestors in the smaller node
int cnt = 0;

// Loop until parent[node] != -1
while (parent[node] != -1) {

// In case the condition of the program is satisfiable, and we have to increment the comment by 1.
if (parent[node] < i)
cnt += 1;

node = parent[node];
}

// Now, we have to print the result for the node that is present
cout << cnt << " ";
}
}

// Writing the main code for the program above
int main()
{
int N = 6;

// Tree Formation

return 0;
}
``````

Output: Example 2)

``````// Writing a Java program to find out the above approach for the following
import java.io.*;
import java.util.*;

class TPT{
// Writing a function to add an edge between the nodes u and v
int u, int v)
{
}
// Creating a function that will perform the DFS traversal for the following code and store the parent of each of the nodes.
static void dfs(ArrayList<Integer> parent,
int u, int par)
{
// we have to store the parent node
parent.set(u,par);

// now, we will have to commute to the child node
{
// We have to recursively admit a function for the DFS traversal of the child node
if (child != par)
}
}
// Creating a function that will count the total number of ancestors that has a smaller value than the current node
static void countSmallerAncestors(
{
// We have to store the parent of each node present
ArrayList<Integer> parent = new ArrayList<Integer>();
for(int i = 0; i < (int)(1e5); i++)
{
}

// Now, we will perform the DFS traversal for each node.

for(int i = 1; i <= n; i++)
{
int node = i;

// Now, we have to store the total number of ancestors in the smaller node
int cnt = 0;

// Loop until parent[node] != -1
while (parent.get(node) != -1)
{
// In case the condition of the program is satisfiable, and we have to increment the comment by 1.
if (parent.get(node) < i)
cnt += 1;

node = parent.get(node);
}
// Now we have to print the result for the node that is present
System.out.print(cnt + " ");
}
}

// Writing the main code for the program above
public static void main (String[] args)
{
int N = 6;
for(int i = 0; i < (int)(1e5); i++)
{
}

// Tree Formation

}
}``````

Output: Example 3)

``````// Writing a Python program to find out the above approach for the following
// Writing a function to add an edge between the nodes u and v

// Creating a function that will perform the DFS traversal for the following code and store the parent of each of the nodes.
def dfs(u, par = -1):

// we have to store the parent node
parent[u] = par

// now, we will have to commute to the child node
// We have to recursively admit a function for the DFS traversal of the child node
if (child != par):
dfs(child, u)
// Creating a function that will count the total number of ancestors that has a smaller value than the current node
def countSmallerAncestors(n):
// We have to store the parent of each node present
// Now, we will perform the DFS traversal for each node.
dfs(1)
// Now, we have to store the total number of ancestors in the smaller node
for i in range(1, n + 1):
node = i
// In case the condition of the program is satisfiable, and we have to increment the comment by 1.
cnt = 0

# Loop until parent[node] != -1
while (parent[node] != -1):
// Now, we have to print the result for the node that is present
if (parent[node] < i):
cnt += 1

node = parent[node]

// Now we have to print the result for the node that is present
print(cnt, end = " ")

// Writing the main code for the program above
if __name__ == '__main__':

N = 6
adj = [[] for i in range(10**5)]
parent =  * (10**5)

# Tree Formation

countSmallerAncestors(N)``````

Output: Example 4)

``````// Writing a C# program to find out the above approach for the following
using System;
using System.Collections.Generic;
class TPT {
// Writing a function to add an edge between the nodes u and v
{
}
// Creating a function that will perform the DFS traversal for the following code and store the parent of each of the nodes.
static void dfs(List<int> parent,
int u,
int par = -1)
{
// we have to store the parent node
parent[u] = par;
// now, we will have to commute to the child node
// We have to recursively admit a function for the DFS traversal of the child node
if (child != par)
}
}
// Creating a function that will count the total number of ancestors that has a smaller value than the current node
static void countSmallerAncestors(
{
// We have to store the parent of each node present
List<int> parent = new List<int>();
for(int i = 0; i < (int)(1e5); i++)
{
}

// Now, we will perform the DFS traversal for each node.
// Now we have to store the total number of ancestors in the smaller node
for (int i = 1; i <= n; i++) {

int node = i;

// Store the number of ancestors
// smaller than node
int cnt = 0;

// Loop until parent[node] != -1
while (parent[node] != -1) {
// In case the condition of the program is satisfiable, and we have to increment the comment by 1.
if (parent[node] < i)
cnt += 1;

node = parent[node];
}
// Now we have to print the result for the node that is present
Console.Write(cnt + " ");
}
}

static void Main() {
int N = 6;
for(int i = 0; i < (int)(1e5); i++)
{
}

// Tree Formation

}
}``````

Output: Example 5)

``````<script>
// Writing a Javascript program to find out the above approach for the following
// Writing a function to add an edge between the nodes u and v
{
}
// Creating a function that will perform the DFS traversal for the following code and store the parent of each of the nodes.
function dfs(parent, adj, u, par = -1)
{
// we have to store the parent node
parent[u] = par;

// now, we will have to commute to the child node
// We have to recursively admit a function for the DFS traversal of the child node
if (child != par)
});
}
// Creating a function that will count the total number of ancestors that has a smaller value than the current node
{
// We have to store the parent of each node present
var parent = Array(100000).fill(0);

// Now, we will perform the DFS traversal for each node.

// Now we have to store the total number of ancestors in the smaller node
for (var i = 1; i <= n; i++) {

var node = i;

// In case the condition of the program is satisfiable, and we have to increment the comment by 1.
var cnt = 0;

// Loop until parent[node] != -1
while (parent[node] != -1) {

// Now we have to print the result for the node that is present
if (parent[node] < i)
cnt += 1;

node = parent[node];
}

// Print the required result
// for the current node
document.write( cnt + " ");
}
}

// Writing the main code for the program above
var N = 6;

// Tree Formation 