Simple Recursive solution to check whether BST contains dead end
Given a Binary Search Tree that contains positive integer values greater than 0. The task is to check whether the BST contains a dead end or not. Here Dead End means, we are not able to insert any integer element after that node.
Examples:
Input : 8
/ \
5 9
/ \
2 7
/
1
Output : Yes
Explanation : Node "1" is the dead End because
after that we cant insert any element.
Input : 8
/ \
7 10
/ / \
2 9 13
Output :Yes
Explanation : We can't insert any element at
node 9.
We have discussed a solution in below post.
Check whether BST contains Dead End or not
The idea in this post is based on method 3 of Check if a binary tree is BST or not.
First of all, it is given that it is a BST and nodes are greater than zero, root node can be in the range [1, ?] and if root val is say, val, then left sub-tree can have the value in the range [1, val-1] and right sub-tree the value in range [val+1, ?].
we need to traverse recursively and when the min and max value of range coincided it means that we cannot add any node further in the tree.
Hence we encounter a dead end.
Following is the simple recursive solution to the problem.
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node *left, *right;
};
Node* newNode( int data)
{
Node* temp = new Node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
struct Node* insert( struct Node* node, int key)
{
if (node == NULL)
return newNode(key);
if (key < node->data)
node->left = insert(node->left, key);
else if (key > node->data)
node->right = insert(node->right, key);
return node;
}
bool deadEnd(Node* root, int min=1, int max=INT_MAX)
{
if (!root)
return false ;
if (min == max)
return true ;
return deadEnd(root->left, min, root->data - 1) ||
deadEnd(root->right, root->data + 1, max);
}
int main()
{
Node* root = NULL;
root = insert(root, 8);
root = insert(root, 5);
root = insert(root, 2);
root = insert(root, 3);
root = insert(root, 7);
root = insert(root, 11);
root = insert(root, 4);
if (deadEnd(root) == true )
cout << "Yes " << endl;
else
cout << "No " << endl;
return 0;
}
|
Java
class BinarySearchTree {
class Node {
int data;
Node left, right;
public Node( int item) {
data = item;
left = right = null ;
}
}
Node root;
BinarySearchTree() {
root = null ;
}
void insert( int data) {
root = insertRec(root, data);
}
Node insertRec(Node root, int data) {
if (root == null ) {
root = new Node(data);
return root;
}
if (data < root.data)
root.left = insertRec(root.left, data);
else if (data > root.data)
root.right = insertRec(root.right, data);
return root;
}
boolean deadEnd(Node root, int min, int max)
{
if (root== null )
return false ;
if (min == max)
return true ;
return deadEnd(root.left, min, root.data - 1 )||
deadEnd(root.right, root.data + 1 , max);
}
public static void main(String[] args) {
BinarySearchTree tree = new BinarySearchTree();
tree.insert( 8 );
tree.insert( 5 );
tree.insert( 2 );
tree.insert( 3 );
tree.insert( 7 );
tree.insert( 11 );
tree.insert( 4 );
if (tree.deadEnd(tree.root , 1 ,
Integer.MAX_VALUE) == true )
System.out.println( "Yes " );
else
System.out.println( "No " );
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def insert(node, key):
if node = = None :
return Node(key)
if key < node.data:
node.left = insert(node.left, key)
elif key > node.data:
node.right = insert(node.right, key)
return node
def deadEnd(root, Min , Max ):
if root = = None :
return False
if Min = = Max :
return True
return (deadEnd(root.left, Min , root.data - 1 ) or
deadEnd(root.right, root.data + 1 , Max ))
if __name__ = = '__main__' :
root = None
root = insert(root, 8 )
root = insert(root, 5 )
root = insert(root, 2 )
root = insert(root, 3 )
root = insert(root, 7 )
root = insert(root, 11 )
root = insert(root, 4 )
if deadEnd(root, 1 , 9999999999 ) = = True :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class BinarySearchTree
{
public class Node
{
private readonly BinarySearchTree outerInstance;
public int data;
public Node left, right;
public Node(BinarySearchTree outerInstance, int item)
{
this .outerInstance = outerInstance;
data = item;
left = right = null ;
}
}
public Node root;
public BinarySearchTree()
{
root = null ;
}
public virtual void insert( int data)
{
root = insertRec(root, data);
}
public virtual Node insertRec(Node root, int data)
{
if (root == null )
{
root = new Node( this , data);
return root;
}
if (data < root.data)
{
root.left = insertRec(root.left, data);
}
else if (data > root.data)
{
root.right = insertRec(root.right, data);
}
return root;
}
public virtual bool deadEnd(Node root, int min, int max)
{
if (root == null )
{
return false ;
}
if (min == max)
{
return true ;
}
return deadEnd(root.left, min, root.data - 1) || deadEnd(root.right, root.data + 1, max);
}
public static void Main( string [] args)
{
BinarySearchTree tree = new BinarySearchTree();
tree.insert(8);
tree.insert(5);
tree.insert(2);
tree.insert(3);
tree.insert(7);
tree.insert(11);
tree.insert(4);
if (tree.deadEnd(tree.root,1, int .MaxValue) == true )
{
Console.WriteLine( "Yes " );
}
else
{
Console.WriteLine( "No " );
}
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .left = null ;
this .right = null ;
}
}
var root = null ;
function insert(data) {
root = insertRec(root, data);
}
function insertRec(root , data) {
if (root == null ) {
root = new Node(data);
return root;
}
if (data < root.data)
root.left = insertRec(root.left, data);
else if (data > root.data)
root.right = insertRec(root.right, data);
return root;
}
function deadEnd(root , min , max)
{
if (root== null )
return false ;
if (min == max)
return true ;
return deadEnd(root.left, min, root.data - 1)||
deadEnd(root.right, root.data + 1, max);
}
insert(8);
insert(5);
insert(2);
insert(3);
insert(7);
insert(11);
insert(4);
if (deadEnd(root ,1 ,
Number.MAX_VALUE) == true )
document.write( "Yes " );
else
document.write( "No " );
</script>
|
Time Complexity: O(N), As we have to visit every node of the tree in the worst case.
Auxiliary Space: O(h), Here h is the height of the tree and the extra space is used in recursion call stack.
Last Updated :
01 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...