Second smallest element in BST

Here given code implementation process.

//C Program 
//Second smallest element in BST
#include <stdio.h>
#include <stdlib.h>

 //Binary Search Tree node
struct Node
{
	int data;
	struct Node *left, *right;
};
//Adding a new node in binary search tree
void add(struct Node **root, int data)
{
	//Create a dynamic node of binary search tree 
	struct Node *new_node = (struct Node *) malloc(sizeof(struct Node));
	if (new_node != NULL)
	{
		//Set data and pointer values
		new_node->data = data;
		new_node->left = NULL; //Initially node left-pointer is NULL
		new_node->right = NULL; //Initially node right-pointer is NULL
		if ( *root == NULL)
		{
			//When add a first node in BST
			*root = new_node;
		}
		else
		{
			struct Node *find = *root;
			//iterate binary tree and add new node to proper position
			while (find != NULL)
			{
				if (find->data > data)
				{
					if (find->left == NULL)
					{
						find->left = new_node;
						break;
					}
					else
					{ //visit left sub-tree
						find = find->left;
					}
				}
				else
				{
					if (find->right == NULL)
					{
						find->right = new_node;
						break;
					}
					else
					{
						//visit right sub-tree
						find = find->right;
					}
				}
			}
		}
	}
	else
	{
		printf("Memory Overflow\n");
		exit(0); //Terminate program execution
	}
}
//Find the 2nd Smallest node in binary tree
void find_second_small(struct Node *root, int *counter, struct Node **element)
{
	if (root != NULL)
	{
		find_second_small(root->left, counter, element);
		if ( *counter == 2)
		{
			return;
		}
		if ( *element == NULL || ( *element)->data < root->data)
		{
			//Modified counter variable by one
			( *counter) ++;*element = root;
		}
		find_second_small(root->right, counter, element);
	}
}
void second_small(struct Node *root)
{
	if (root == NULL)
	{
		//When BST are no have elements
		printf("Empty Binary Search Tree\n");
	}
	else
	{
		int counter = 0;
		struct Node *element = NULL;
		find_second_small(root, & counter, & element);
		if (counter < 2)
		{
			//If In case 2nd Smallest node are 
			//Not exist in binary search tree, then
			printf("Second smallest node are not exist  \n");
		}
		else
		{
			printf("Second smallest node : %d \n", element->data);
		}
	}
}
int main()
{
	struct Node *root = NULL;
	//Add nodes in binary search tree
	/*
	        7
	      /   \
	     3     10
	    / \   / \
	   3   5 8   20
	  /   /   \    \
	 3   4     9    20
	*/
	add( & root, 7);
	add( & root, 3);
	add( & root, 10);
	add( & root, 3);
	add( & root, 5);
	add( & root, 8);
	add( & root, 20);
	add( & root, 3);
	add( & root, 4);
	add( & root, 9);
	add( & root, 20);
	second_small(root);
	return 0;
}

Output

Second smallest node : 4
//Java program
//Second smallest element in BST
//Binary Search Tree Node
class Node
{
	// Data value 
	public int data;
	// Indicates left and right subtree
	public Node left;
	public Node right;
	public Node(int data)
	{
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinarySearchTree
{
	public Node root;
	public int counter;
	public Node result;
	public BinarySearchTree()
	{
		this.root = null;
		this.result = null;
		this.counter = 0;
	}
	//insert a node in BST
	public void add(int value)
	{
		//Create a dynamic node of binary search tree 
		Node new_node = new Node(value);
		if (new_node != null)
		{
			if (root == null)
			{
				//When adds a first node in binary tree
				root = new_node;
			}
			else
			{
				Node find = root;
				//add new node to proper position
				while (find != null)
				{
					if (find.data >= value)
					{
						if (find.left == null)
						{
							find.left = new_node;
							break;
						}
						else
						{
							//visit left sub-tree
							find = find.left;
						}
					}
					else
					{
						if (find.right == null)
						{
							find.right = new_node;
							break;
						}
						else
						{
							//visit right sub-tree
							find = find.right;
						}
					}
				}
			}
		}
		else
		{
			System.out.print("\nMemory Overflow\n");
		}
	}
	//Find the 2nd smallest node in binary tree
	public void find_second_small(Node head)
	{
		if (head != null)
		{
			find_second_small(head.left);
			if (this.counter == 2)
			{
				return;
			}
			if (result == null || result.data < head.data)
			{
				//Modified counter variable by one
				this.counter++;
				result = head;
			}
			find_second_small(head.right);
		}
	}
	public void second_small()
	{
		if (root == null)
		{
			//When BST are no have elements
			System.out.print("Empty Binary Search Tree\n");
		}
		else
		{
			this.counter = 0;
			this.result = null;
			find_second_small(root);
			if (this.counter < 2)
			{
				//If In case 2nd Smallest node are 
				//Not exist in binary search tree
				System.out.print("Second smallest node are not exist  \n");
			}
			else
			{
				System.out.print("Second smallest node : " + result.data + "\n");
			}
		}
	}
	public static void main(String[] args)
	{
		BinarySearchTree obj = new BinarySearchTree();
		//binary search tree
		/*
          7
        /   \
       3     10
      / \   / \
     3   5 8   20
    /   /   \    \
   3   4     9    20
  */
		obj.add(7);
		obj.add(3);
		obj.add(10);
		obj.add(3);
		obj.add(5);
		obj.add(8);
		obj.add(20);
		obj.add(3);
		obj.add(4);
		obj.add(9);
		obj.add(20);
		obj.second_small();
	}
}

Output

Second smallest node : 4
//Include header file
#include <iostream>
using namespace std;

//C++ program
//Second smallest element in BST
//Binary Search Tree Node
class Node
{
	public:
	// Data value 
	int data;
	// Indicates left and right subtree
	Node * left;
	Node * right;
	Node(int data)
	{
		this->data = data;
		this->left = NULL;
		this->right = NULL;
	}
};
class BinarySearchTree
{
	public: Node * root;
	int counter;
	Node * result;
	BinarySearchTree()
	{
		this->root = NULL;
		this->result = NULL;
		this->counter = 0;
	}
	//insert a node in BST
	void add(int value)
	{
		//Create a dynamic node of binary search tree 
		Node * new_node = new Node(value);
		if (new_node != NULL)
		{
			if (this->root == NULL)
			{
				//When adds a first node in binary tree
				this->root = new_node;
			}
			else
			{
				Node * find = this->root;
				//add new node to proper position
				while (find != NULL)
				{
					if (find->data >= value)
					{
						if (find->left == NULL)
						{
							find->left = new_node;
							break;
						}
						else
						{
							//visit left sub-tree
							find = find->left;
						}
					}
					else
					{
						if (find->right == NULL)
						{
							find->right = new_node;
							break;
						}
						else
						{
							//visit right sub-tree
							find = find->right;
						}
					}
				}
			}
		}
		else
		{
			cout << "\nMemory Overflow\n";
		}
	}
	//Find the 2nd smallest node in binary tree
	void find_second_small(Node * head)
	{
		if (head != NULL)
		{
			this->find_second_small(head->left);
			if (this->counter == 2)
			{
				return;
			}
			if (this->result == NULL || this->result->data < head->data)
			{
				//Modified counter variable by one
				this->counter++;
				this->result = head;
			}
			this->find_second_small(head->right);
		}
	}
	void second_small()
	{
		if (this->root == NULL)
		{
			//When BST are no have elements
			cout << "Empty Binary Search Tree\n";
		}
		else
		{
			this->counter = 0;
			this->result = NULL;
			this->find_second_small(this->root);
			if (this->counter < 2)
			{
				//If In case 2nd Smallest node are 
				//Not exist in binary search tree
				cout << "Second smallest node are not exist  \n";
			}
			else
			{
				cout << "Second smallest node : " << this->result->data << "\n";
			}
		}
	}
};
int main()
{
	BinarySearchTree obj = BinarySearchTree();
	//binary search tree
	/*
	          7
	        /   \
	       3     10
	      / \   / \
	     3   5 8   20
	    /   /   \    \
	   3   4     9    20
	  */
	obj.add(7);
	obj.add(3);
	obj.add(10);
	obj.add(3);
	obj.add(5);
	obj.add(8);
	obj.add(20);
	obj.add(3);
	obj.add(4);
	obj.add(9);
	obj.add(20);
	obj.second_small();
	return 0;
}

Output

Second smallest node : 4
//Include namespace system
using System;
//C# program
//Second smallest element in BST
//Binary Search Tree Node
class Node
{
	// Data value 
	public int data;
	// Indicates left and right subtree
	public Node left;
	public Node right;
	public Node(int data)
	{
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinarySearchTree
{
	public Node root;
	public int counter;
	public Node result;
	public BinarySearchTree()
	{
		this.root = null;
		this.result = null;
		this.counter = 0;
	}
	//insert a node in BST
	public void add(int value)
	{
		//Create a dynamic node of binary search tree 
		Node new_node = new Node(value);
		if (new_node != null)
		{
			if (root == null)
			{
				//When adds a first node in binary tree
				root = new_node;
			}
			else
			{
				Node find = root;
				//add new node to proper position
				while (find != null)
				{
					if (find.data >= value)
					{
						if (find.left == null)
						{
							find.left = new_node;
							break;
						}
						else
						{
							//visit left sub-tree
							find = find.left;
						}
					}
					else
					{
						if (find.right == null)
						{
							find.right = new_node;
							break;
						}
						else
						{
							//visit right sub-tree
							find = find.right;
						}
					}
				}
			}
		}
		else
		{
			Console.Write("\nMemory Overflow\n");
		}
	}
	//Find the 2nd smallest node in binary tree
	public void find_second_small(Node head)
	{
		if (head != null)
		{
			find_second_small(head.left);
			if (this.counter == 2)
			{
				return;
			}
			if (result == null || result.data < head.data)
			{
				//Modified counter variable by one
				this.counter++;
				result = head;
			}
			find_second_small(head.right);
		}
	}
	public void second_small()
	{
		if (root == null)
		{
			//When BST are no have elements
			Console.Write("Empty Binary Search Tree\n");
		}
		else
		{
			this.counter = 0;
			this.result = null;
			find_second_small(root);
			if (this.counter < 2)
			{
				//If In case 2nd Smallest node are 
				//Not exist in binary search tree
				Console.Write("Second smallest node are not exist  \n");
			}
			else
			{
				Console.Write("Second smallest node : " + result.data + "\n");
			}
		}
	}
	public static void Main(String[] args)
	{
		BinarySearchTree obj = new BinarySearchTree();
		//binary search tree
		/*
		          7
		        /   \
		       3     10
		      / \   / \
		     3   5 8   20
		    /   /   \    \
		   3   4     9    20
		  */
		obj.add(7);
		obj.add(3);
		obj.add(10);
		obj.add(3);
		obj.add(5);
		obj.add(8);
		obj.add(20);
		obj.add(3);
		obj.add(4);
		obj.add(9);
		obj.add(20);
		obj.second_small();
	}
}

Output

Second smallest node : 4
<?php
//Php program
//Second smallest element in BST
//Binary Search Tree Node
class Node
{
	// Data value 
	public $data;
	// Indicates left and right subtree
	public $left;
	public $right;

	function __construct($data)
	{
		$this->data = $data;
		$this->left = null;
		$this->right = null;
	}
}
class BinarySearchTree
{
	public $root;
	public $counter;
	public $result;

	function __construct()
	{
		$this->root = null;
		$this->result = null;
		$this->counter = 0;
	}
	//insert a node in BST
	public	function add($value)
	{
		//Create a dynamic node of binary search tree 
		$new_node = new Node($value);
		if ($new_node != null)
		{
			if ($this->root == null)
			{
				//When adds a first node in binary tree
				$this->root = $new_node;
			}
			else
			{
				$find = $this->root;
				//add new node to proper position
				while ($find != null)
				{
					if ($find->data >= $value)
					{
						if ($find->left == null)
						{
							$find->left = $new_node;
							break;
						}
						else
						{
							//visit left sub-tree
							$find = $find->left;
						}
					}
					else
					{
						if ($find->right == null)
						{
							$find->right = $new_node;
							break;
						}
						else
						{
							//visit right sub-tree
							$find = $find->right;
						}
					}
				}
			}
		}
		else
		{
			echo "\nMemory Overflow\n";
		}
	}
	//Find the 2nd smallest node in binary tree
	public	function find_second_small($head)
	{
		if ($head != null)
		{
			$this->find_second_small($head->left);
			if ($this->counter == 2)
			{
				return;
			}
			if ($this->result == null || $this->result->data < $head->data)
			{
				//Modified counter variable by one
				$this->counter++;
				$this->result = $head;
			}
			$this->find_second_small($head->right);
		}
	}
	public	function second_small()
	{
		if ($this->root == null)
		{
			echo "Empty Binary Search Tree\n";
		}
		else
		{
			//reset the instance variable value
			$this->counter = 0;
			$this->result = null;
			$this->find_second_small($this->root);
			if ($this->counter < 2)
			{
				echo "Second smallest node are not exist  \n";
			}
			else
			{
				echo "Second smallest node : ". $this->result->data ."\n";
			}
		}
	}
}

function main()
{
	$obj = new BinarySearchTree();
	//binary search tree
	/*
	          7
	        /   \
	       3     10
	      / \   / \
	     3   5 8   20
	    /   /   \    \
	   3   4     9    20
	  */
	$obj->add(7);
	$obj->add(3);
	$obj->add(10);
	$obj->add(3);
	$obj->add(5);
	$obj->add(8);
	$obj->add(20);
	$obj->add(3);
	$obj->add(4);
	$obj->add(9);
	$obj->add(20);
	$obj->second_small();
}
main();

Output

Second smallest node : 4
//Node Js program
//Second smallest element in BST
//Binary Search Tree Node
class Node
{
	constructor(data)
	{
      	// Data value 
		this.data = data;
      	// Indicates left and right subtree
		this.left = null;
		this.right = null;
	}
}
class BinarySearchTree
{
	constructor()
	{
		this.root = null;
		this.result = null;
		this.counter = 0;
	}
	//insert a node in BST
	add(value)
	{
		//Create a dynamic node of binary search tree 
		var new_node = new Node(value);
		if (new_node != null)
		{
			if (this.root == null)
			{
				//When adds a first node in binary tree
				this.root = new_node;
			}
			else
			{
				var find = this.root;
				//add new node to proper position
				while (find != null)
				{
					if (find.data >= value)
					{
						if (find.left == null)
						{
							find.left = new_node;
							break;
						}
						else
						{
							//visit left sub-tree
							find = find.left;
						}
					}
					else
					{
						if (find.right == null)
						{
							find.right = new_node;
							break;
						}
						else
						{
							//visit right sub-tree
							find = find.right;
						}
					}
				}
			}
		}
		else
		{
			process.stdout.write("\nMemory Overflow\n");
		}
	}
	//Find the 2nd smallest node in binary tree
	find_second_small(head)
	{
		if (head != null)
		{
			this.find_second_small(head.left);
			if (this.counter == 2)
			{
				return;
			}
			if (this.result == null || this.result.data < head.data)
			{
				//Modified counter variable by one
				this.counter++;
				this.result = head;
			}
			this.find_second_small(head.right);
		}
	}
	second_small()
	{
		if (this.root == null)
		{
			process.stdout.write("Empty Binary Search Tree\n");
		}
		else
		{
			//reset the instance variable value
			this.counter = 0;
			this.result = null;
			this.find_second_small(this.root);
			if (this.counter < 2)
			{
				process.stdout.write("Second smallest node are not exist  \n");
			}
			else
			{
				process.stdout.write("Second smallest node : " + this.result.data + "\n");
			}
		}
	}
}

function main()
{
	var obj = new BinarySearchTree();
	//binary search tree
	/*
	          7
	        /   \
	       3     10
	      / \   / \
	     3   5 8   20
	    /   /   \    \
	   3   4     9    20
	  */
	obj.add(7);
	obj.add(3);
	obj.add(10);
	obj.add(3);
	obj.add(5);
	obj.add(8);
	obj.add(20);
	obj.add(3);
	obj.add(4);
	obj.add(9);
	obj.add(20);
	obj.second_small();
}
main();

Output

Second smallest node : 4
# Python 3 program
# Second smallest element in BST

# Binary Search Tree Node
class Node :

	def __init__(self, data) :
    	#  Data value 
		self.data = data
		#  Indicates left and right subtree
		self.left = None
		self.right = None
	

class BinarySearchTree :
	
	def __init__(self) :
		self.root = None
		self.result = None
		self.counter = 0
	
	# insert a node in BST
	def add(self, value) :
		# Create a dynamic node of binary search tree 
		new_node = Node(value)
		if (new_node != None) :
			if (self.root == None) :
				# When adds a first node in binary tree
				self.root = new_node
			else :
				find = self.root
				# add new node to proper position
				while (find != None) :
					if (find.data >= value) :
						if (find.left == None) :
							find.left = new_node
							break
						else :
							# visit left sub-tree
							find = find.left
						
					else :
						if (find.right == None) :
							find.right = new_node
							break
						else :
							# visit right sub-tree
							find = find.right
						
					
				
			
		else :
			print("\nMemory Overflow\n", end = "")
		
	
	# Find the 2nd smallest node in binary tree
	def find_second_small(self, head) :
		if (head != None) :
			self.find_second_small(head.left)
			if (self.counter == 2) :
				return
			
			if (self.result == None or self.result.data < head.data) :
				# Modified counter variable by one
				self.counter += 1
				self.result = head
			
			self.find_second_small(head.right)
		
	
	def second_small(self) :
		if (self.root == None) :
			print("Empty Binary Search Tree\n", end = "")
		else :
			# reset the instance variable value
			self.counter = 0
			self.result = None
			self.find_second_small(self.root)
			if (self.counter < 2) :
				print("Second smallest node are not exist  \n", end = "")
			else :
				print("Second smallest node : ", self.result.data ,"\n", end = "")
			
		
	

def main() :
	obj = BinarySearchTree()
	# binary search tree
	# 
	#           7
	#         /   \
	#        3     10
	#       / \   / \
	#      3   5 8   20
	#     /   /   \    \
	#    3   4     9    20
	#   
	
	obj.add(7)
	obj.add(3)
	obj.add(10)
	obj.add(3)
	obj.add(5)
	obj.add(8)
	obj.add(20)
	obj.add(3)
	obj.add(4)
	obj.add(9)
	obj.add(20)
	obj.second_small()

if __name__ == "__main__": main()

Output

Second smallest node :  4
# Ruby program
# Second smallest element in BST
# Binary Search Tree Node
class Node 

	# Define the accessor and reader of class Node  
	attr_reader :data, :left, :right
	attr_accessor :data, :left, :right
	
	def initialize(data)
		#  Data value 
		self.data = data
		#  Indicates left and right subtree
		self.left = nil
		self.right = nil
	end
end
class BinarySearchTree 

	# Define the accessor and reader of class BinarySearchTree  
	attr_reader :root, :counter, :result
	attr_accessor :root, :counter, :result


	
	def initialize()
	
		self.root = nil
		self.result = nil
		self.counter = 0
	end
	# insert a node in BST
	def add(value)
	
		# Create a dynamic node of binary search tree 
		new_node = Node.new(value)
		if (new_node != nil)
		
			if (@root == nil)
			
				# When adds a first node in binary tree
				@root = new_node
			else
			
				find = @root
				# add new node to proper position
				while (find != nil)
				
					if (find.data >= value)
					
						if (find.left == nil)
						
							find.left = new_node
							break
						else
						
							# visit left sub-tree
							find = find.left
						end
					else
					
						if (find.right == nil)
						
							find.right = new_node
							break
						else
						
							# visit right sub-tree
							find = find.right
						end
					end
				end
			end
		else
		
			print("\nMemory Overflow\n")
		end
	end
	# Find the 2nd smallest node in binary tree
	def find_second_small(head)
	
		if (head != nil)
		
			self.find_second_small(head.left)
			if (self.counter == 2)
			
				return
			end
			if (@result == nil || @result.data < head.data)
			
				# Modified counter variable by one
				self.counter += 1
				@result = head
			end
			self.find_second_small(head.right)
		end
	end
	def second_small()
	
		if (@root == nil)
		
			# When BST are no have elements
			print("Empty Binary Search Tree\n")
		else
		
			# reset the instance variable value
			self.counter = 0
			self.result = nil
			self.find_second_small(@root)
			if (self.counter < 2)
			
				# If In case 2nd Smallest node are 
				# Not exist in binary search tree
				print("Second smallest node are not exist  \n")
			else
			
				print("Second smallest node : ", @result.data ,"\n")
			end
		end
	end
end
def main()

	obj = BinarySearchTree.new()
	# binary search tree
	# 
	#           7
	#         /   \
	#        3     10
	#       / \   / \
	#      3   5 8   20
	#     /   /   \    \
	#    3   4     9    20
	#   
	
	obj.add(7)
	obj.add(3)
	obj.add(10)
	obj.add(3)
	obj.add(5)
	obj.add(8)
	obj.add(20)
	obj.add(3)
	obj.add(4)
	obj.add(9)
	obj.add(20)
	obj.second_small()
end
main()

Output

Second smallest node : 4
//Scala program
//Second smallest element in BST

//Binary Search Tree Node
class Node(var data: Int,
	var left: Node,
		var right: Node)
{
	def this(data: Int)
	{
		this(data, null, null);
	}
}
class BinarySearchTree(var root: Node,
	var counter: Int,
		var result: Node)
{
	def this()
	{
		this(null, 0, null);
	}
	//insert a node in BST
	def add(value: Int): Unit = {
		//Create a dynamic node of binary search tree 
		var new_node: Node = new Node(value);
		if (new_node != null)
		{
			if (root == null)
			{
				//When adds a first node in binary tree
				root = new_node;
			}
			else
			{
				var find: Node = root;
				//add new node to proper position
				while (find != null)
				{
					if (find.data >= value)
					{
						if (find.left == null)
						{
							find.left = new_node;
							return;
						}
						else
						{
							//visit left sub-tree
							find = find.left;
						}
					}
					else
					{
						if (find.right == null)
						{
							find.right = new_node;
							return;
						}
						else
						{
							//visit right sub-tree
							find = find.right;
						}
					}
				}
			}
		}
		else
		{
			print("\nMemory Overflow\n");
		}
	}
	//Find the 2nd smallest node in binary tree
	def find_second_small(head: Node): Unit = {
		if (head != null)
		{
			find_second_small(head.left);
			if (this.counter == 2)
			{
				return;
			}
			if (result == null || result.data < head.data)
			{
				//Modified counter variable by one
				this.counter += 1;
				result = head;
			}
			find_second_small(head.right);
		}
	}
	def second_small(): Unit = {
		if (root == null)
		{
			//When BST are no have elements
			print("Empty Binary Search Tree\n");
		}
		else
		{
			//reset the instance variable value
			this.counter = 0;
			this.result = null;
			find_second_small(root);
			if (this.counter < 2)
			{
				//If In case 2nd Smallest node are 
				//Not exist in binary search tree
				print("Second smallest node are not exist  \n");
			}
			else
			{
				print("Second smallest node : " + result.data + "\n");
			}
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: BinarySearchTree = new BinarySearchTree();
		//binary search tree
		/*
		          7
		        /   \
		       3     10
		      / \   / \
		     3   5 8   20
		    /   /   \    \
		   3   4     9    20
		  */
		obj.add(7);
		obj.add(3);
		obj.add(10);
		obj.add(3);
		obj.add(5);
		obj.add(8);
		obj.add(20);
		obj.add(3);
		obj.add(4);
		obj.add(9);
		obj.add(20);
		obj.second_small();
	}
}

Output

Second smallest node : 4
//Swift program
//Second smallest element in BST
//Binary Search Tree Node
class Node
{
	// Data value 
	var data: Int;
	// Indicates left and right subtree
	var left: Node? ;
	var right: Node? ;
	init(_ data: Int)
	{
		self.data = data;
		self.left = nil;
		self.right = nil;
	}
}
class BinarySearchTree
{
	var root: Node? ;
	var counter: Int;
	var result: Node? ;
	init()
	{
		self.root = nil;
		self.result = nil;
		self.counter = 0;
	}
	//insert a node in BST
	func add(_ value: Int)
	{
		//Create a dynamic node of binary search tree 
		let new_node: Node? = Node(value);
		if (new_node != nil)
		{
			if (self.root == nil)
			{
				//When adds a first node in binary tree
				self.root = new_node;
			}
			else
			{
				var find: Node? = self.root;
				//add new node to proper position
				while (find != nil)
				{
					if (find!.data >= value)
					{
						if (find!.left == nil)
						{
							find!.left = new_node;
							break;
						}
						else
						{
							//visit left sub-tree
							find = find!.left;
						}
					}
					else
					{
						if (find!.right == nil)
						{
							find!.right = new_node;
							break;
						}
						else
						{
							//visit right sub-tree
							find = find!.right;
						}
					}
				}
			}
		}
		else
		{
			print("\nMemory Overflow\n", terminator: "");
		}
	}
	//Find the 2nd smallest node in binary tree
	func find_second_small(_ head: Node? )
	{
		if (head != nil)
		{
			self.find_second_small(head!.left);
			if (self.counter == 2)
			{
				return;
			}
			if (self.result == nil || self.result!.data < head!.data)
			{
				//Modified counter variable by one
				self.counter += 1;
				self.result = head;
			}
			self.find_second_small(head!.right);
		}
	}
	func second_small()
	{
		if (self.root == nil)
		{
			print("Empty Binary Search Tree\n", terminator: "");
		}
		else
		{
			//reset the instance variable value
			self.counter = 0;
			self.result = nil;
			self.find_second_small(self.root);
			if (self.counter < 2)
			{
				print("Second smallest node are not exist  \n", terminator: "");
			}
			else
			{
				print("Second smallest node : ", self.result!.data ,"\n", terminator: "");
			}
		}
	}
}
func main()
{
	let obj: BinarySearchTree = BinarySearchTree();
	//binary search tree
	/*
	          7
	        /   \
	       3     10
	      / \   / \
	     3   5 8   20
	    /   /   \    \
	   3   4     9    20
	  */
	obj.add(7);
	obj.add(3);
	obj.add(10);
	obj.add(3);
	obj.add(5);
	obj.add(8);
	obj.add(20);
	obj.add(3);
	obj.add(4);
	obj.add(9);
	obj.add(20);
	obj.second_small();
}
main();

Output

Second smallest node :  4

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved