Special two digit numbers in a Binary Search Tree
Last Updated :
01 Dec, 2022
Given a Binary Search Trees, the task is to count the number of nodes which are having special two-digit numbers.
Prerequisite : Special Two Digit Number | Binary Search Tree
Examples :
Input : 15 7 987 21
Output : 0
Input : 19 99 57 1 22
Output : 2
Algorithm: Iterate through each node of tree recursively with a variable count, and check each node’s data for a special two-digit number. If it is then increment the variable count. In the end, return count.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node
{
struct Node *left;
int info;
struct Node *right;
};
void insert( struct Node **rt, int key)
{
if (*rt == NULL)
{
(*rt) = new Node();
(*rt) -> left = NULL;
(*rt) -> right = NULL;
(*rt) -> info = key;
}
else if (key < ((*rt) -> info))
insert(&((*rt) -> left), key);
else
insert(&(*rt) -> right, key);
}
int check( int num)
{
int sum = 0, i = num, sum_of_digits, prod_of_digits ;
if (num < 10 || num > 99)
return 0;
else
{
sum_of_digits = (i % 10) + (i / 10);
prod_of_digits = (i % 10) * (i / 10);
sum = sum_of_digits + prod_of_digits;
}
if (sum == num)
return 1;
else
return 0;
}
void countSpecialDigit( struct Node *rt, int *c)
{
int x;
if (rt == NULL)
return ;
else
{
x = check(rt -> info);
if (x == 1)
*c = *c + 1;
countSpecialDigit(rt -> left, c);
countSpecialDigit(rt -> right, c);
}
}
int main()
{
struct Node *root = NULL;
int count = 0;
insert(&root, 50);
insert(&root, 29);
insert(&root, 59);
insert(&root, 19);
insert(&root, 53);
insert(&root, 556);
insert(&root, 56);
insert(&root, 94);
insert(&root, 13);
countSpecialDigit(root, &count);
cout<< count;
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct Node
{
struct Node *left;
int info;
struct Node *right;
};
void insert( struct Node **rt, int key)
{
if (*rt == NULL)
{
(*rt) = ( struct Node *) malloc ( sizeof ( struct Node));
(*rt) -> left = NULL;
(*rt) -> right = NULL;
(*rt) -> info = key;
}
else if (key < ((*rt) -> info))
insert(&((*rt) -> left), key);
else
insert(&(*rt) -> right, key);
}
int check( int num)
{
int sum = 0, i = num, sum_of_digits, prod_of_digits ;
if (num < 10 || num > 99)
return 0;
else
{
sum_of_digits = (i % 10) + (i / 10);
prod_of_digits = (i % 10) * (i / 10);
sum = sum_of_digits + prod_of_digits;
}
if (sum == num)
return 1;
else
return 0;
}
void countSpecialDigit( struct Node *rt, int *c)
{
int x;
if (rt == NULL)
return ;
else
{
x = check(rt -> info);
if (x == 1)
*c = *c + 1;
countSpecialDigit(rt -> left, c);
countSpecialDigit(rt -> right, c);
}
}
int main()
{
struct Node *root = NULL;
int count = 0;
insert(&root, 50);
insert(&root, 29);
insert(&root, 59);
insert(&root, 19);
insert(&root, 53);
insert(&root, 556);
insert(&root, 56);
insert(&root, 94);
insert(&root, 13);
countSpecialDigit(root, &count);
printf ( "%d" , count);
return 0;
}
|
Java
class Node
{
int info;
Node left, right;
Node( int d)
{
info = d;
left = right = null ;
}
}
class BinaryTree{
static Node head;
static int count;
Node insert(Node node, int info)
{
if (node == null )
{
return ( new Node(info));
}
else
{
if (info <= node.info)
{
node.left = insert(node.left, info);
}
else
{
node.right = insert(node.right, info);
}
return node;
}
}
static int check( int num)
{
int sum = 0 , i = num,
sum_of_digits,
prod_of_digits;
if (num < 10 || num > 99 )
return 0 ;
else
{
sum_of_digits = (i % 10 ) + (i / 10 );
prod_of_digits = (i % 10 ) * (i / 10 );
sum = sum_of_digits + prod_of_digits;
}
if (sum == num)
return 1 ;
else
return 0 ;
}
static void countSpecialDigit(Node rt)
{
int x;
if (rt == null )
return ;
else
{
x = check(rt.info);
if (x == 1 )
count = count + 1 ;
countSpecialDigit(rt.left);
countSpecialDigit(rt.right);
}
}
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
Node root = null ;
root = tree.insert(root, 50 );
tree.insert(root, 29 );
tree.insert(root, 59 );
tree.insert(root, 19 );
tree.insert(root, 53 );
tree.insert(root, 556 );
tree.insert(root, 56 );
tree.insert(root, 94 );
tree.insert(root, 13 );
countSpecialDigit(root);
System.out.println(count);
}
}
|
Python3
class Node:
def __init__( self , x):
self .data = x
self .left = None
self .right = None
def insert(node, data):
global succ
root = node
if (node = = None ):
return Node(data)
if (data < node.data):
root.left = insert(node.left, data)
elif (data > node.data):
root.right = insert(node.right, data)
return root
def check(num):
sum = 0
i = num
if (num < 10 or num > 99 ):
return 0
else :
sum_of_digits = (i % 10 ) + (i / / 10 )
prod_of_digits = (i % 10 ) * (i / / 10 )
sum = sum_of_digits + prod_of_digits
if ( sum = = num):
return 1
else :
return 0
def countSpecialDigit(rt):
global c
if (rt = = None ):
return
else :
x = check(rt.data)
if (x = = 1 ):
c + = 1
countSpecialDigit(rt.left)
countSpecialDigit(rt.right)
if __name__ = = '__main__' :
root = None
c = 0
root = insert(root, 50 )
root = insert(root, 29 )
root = insert(root, 59 )
root = insert(root, 19 )
root = insert(root, 53 )
root = insert(root, 556 )
root = insert(root, 56 )
root = insert(root, 94 )
root = insert(root, 13 )
countSpecialDigit(root)
print (c)
|
C#
using System;
public class Node
{
public int info;
public Node left, right;
public Node( int d)
{
info = d;
left = right = null ;
}
}
public class BinaryTree
{
public static Node head;
public static int count;
public Node insert(Node node, int info)
{
if (node == null )
{
return ( new Node(info));
}
else
{
if (info <= node.info)
{
node.left = insert(node.left, info);
}
else
{
node.right = insert(node.right, info);
}
}
return node;
}
static int check( int num)
{
int sum = 0, i = num, sum_of_digits, prod_of_digits;
if (num < 10 || num > 99)
{
return 0;
}
else
{
sum_of_digits = (i % 10) + (i / 10);
prod_of_digits = (i % 10) * (i / 10);
sum = sum_of_digits + prod_of_digits;
}
if (sum == num)
{
return 1;
}
else
{
return 0;
}
}
static void countSpecialDigit(Node rt)
{
int x;
if (rt == null )
{
return ;
}
else
{
x = check(rt.info);
if (x == 1)
{
count = count + 1;
}
countSpecialDigit(rt.left);
countSpecialDigit(rt.right);
}
}
static public void Main ()
{
BinaryTree tree = new BinaryTree();
Node root = null ;
root = tree.insert(root, 50);
tree.insert(root, 29);
tree.insert(root, 59);
tree.insert(root, 19);
tree.insert(root, 53);
tree.insert(root, 556);
tree.insert(root, 56);
tree.insert(root, 94);
tree.insert(root, 13);
countSpecialDigit(root);
Console.WriteLine(count);
}
}
|
Javascript
<script>
class Node
{
constructor(d)
{
this .info = d;
this .left = this .right = null ;
}
}
let head;
let count=0;
function insert(node,info)
{
if (node == null )
{
return ( new Node(info));
}
else
{
if (info <= node.info)
{
node.left = insert(node.left, info);
}
else
{
node.right = insert(node.right, info);
}
return node;
}
}
function check(num)
{
let sum = 0, i = num,
sum_of_digits,
prod_of_digits;
if (num < 10 || num > 99)
return 0;
else
{
sum_of_digits = (i % 10) + Math.floor(i / 10);
prod_of_digits = (i % 10) * Math.floor(i / 10);
sum = sum_of_digits + prod_of_digits;
}
if (sum == num)
return 1;
else
return 0;
}
function countSpecialDigit(rt)
{
let x;
if (rt == null )
return ;
else
{
x = check(rt.info);
if (x == 1)
count = count + 1;
countSpecialDigit(rt.left);
countSpecialDigit(rt.right);
}
}
let root = null ;
root = insert(root, 50);
root=insert(root, 29);
root=insert(root, 59);
root=insert(root, 19);
root=insert(root, 53);
root=insert(root, 556);
root=insert(root, 56);
root=insert(root, 94);
root=insert(root, 13);
countSpecialDigit(root);
document.write(count);
</script>
|
Time Complexity: O(N), Where N is the number of nodes in Tree.
Auxiliary Space: O(h), Here h is the height of the tree and this extra space is used due to the recursion call stack.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...