Find minimum node in binary search tree
Here given code implementation process.
//C Program
//Find minimum node in binary search tree
#include <stdio.h>
#include <stdlib.h>
//structure of Binary Search Tree node
struct Node
{
int data;
struct Node *left, *right;
};
//Adding a new node in binary search tree
void add_node(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 adds a first node in binary tree
*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 and print minimum node of binary search tree
void min_node(struct Node *root)
{
if (root == NULL)
{
printf("\n Empty Tree");
}
else
{
printf(" Minimum node is : ");
struct Node *temp = root;
//Find leftmost node from to root
while (temp->left != NULL)
{
//Visit left node
temp = temp->left;
}
//Display node value
printf("%d\n", temp->data);
}
}
int main()
{
struct Node *root = NULL;
//Add nodes in binary search tree
/*
5
/ \
3 9
/ \ / \
-7 4 8 11
\ / \
2 7 12
*/
add_node( & root, 5);
add_node( & root, 3);
add_node( & root, 9);
add_node( & root, -7);
add_node( & root, 4);
add_node( & root, 8);
add_node( & root, 11);
add_node( & root, 12);
add_node( & root, 7);
add_node( & root, 2);
min_node(root);
return 0;
}
Output
Minimum node is : -7
//Java program
//Find minimum node in binary search tree
//Binary Tree Node
class Node
{
public int data;
public Node left;
public Node right;
public Node(int data)
{
this.data = data;
this.left = null;
this.right = null;
}
}
class BinarySearchTree
{
public Node root;
//Class constructors
public BinarySearchTree()
{
root = null;
}
//insert element
public void add_node(int data)
{
//Create a new binary tree node
Node new_node = new Node(data);
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 >= 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
{
System.out.println("Memory Overflow");
}
}
//Find and print minimum node of binary search tree
public void min_node()
{
if (root == null)
{
System.out.print("\n Empty Tree");
}
else
{
System.out.print(" Minimum node is : ");
Node temp = root;
//Find leftmost node from to root
while (temp.left != null)
{
//Visit left node
temp = temp.left;
}
System.out.print(" " + temp.data + "\n");
}
}
public static void main(String[] args)
{
BinarySearchTree obj = new BinarySearchTree();
//Create binary search tree
/*
5
/ \
3 9
/ \ / \
-7 4 8 11
\ / \
2 7 12
*/
obj.add_node(5);
obj.add_node(3);
obj.add_node(9);
obj.add_node(-7);
obj.add_node(4);
obj.add_node(8);
obj.add_node(11);
obj.add_node(12);
obj.add_node(7);
obj.add_node(2);
obj.min_node();
}
}
Output
Minimum node is : -7
//Include header file
#include <iostream>
using namespace std;
//C++ program
//Find minimum node in binary search tree
//Binary Tree Node
class Node
{
public: int data;
Node * left;
Node * right;
Node(int data)
{
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
class BinarySearchTree
{
public: Node * root;
//Class constructors
BinarySearchTree()
{
this->root = NULL;
}
//insert element
void add_node(int data)
{
//Create a new binary tree node
Node * new_node = new Node(data);
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 >= 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
{
cout << "Memory Overflow";
}
}
//Find and print minimum node of binary search tree
void min_node()
{
if (this->root == NULL)
{
cout << "\n Empty Tree";
}
else
{
cout << " Minimum node is : ";
Node * temp = this->root;
//Find leftmost node from to root
while (temp->left != NULL)
{
//Visit left node
temp = temp->left;
}
cout << " " << temp->data << "\n";
}
}
};
int main()
{
BinarySearchTree obj = BinarySearchTree();
//Create binary search tree
/*
5
/ \
3 9
/ \ / \
-7 4 8 11
\ / \
2 7 12
*/
obj.add_node(5);
obj.add_node(3);
obj.add_node(9);
obj.add_node(-7);
obj.add_node(4);
obj.add_node(8);
obj.add_node(11);
obj.add_node(12);
obj.add_node(7);
obj.add_node(2);
obj.min_node();
return 0;
}
Output
Minimum node is : -7
//Include namespace system
using System;
//C# program
//Find minimum node in binary search tree
//Binary Tree Node
public class Node
{
public int data;
public Node left;
public Node right;
public Node(int data)
{
this.data = data;
this.left = null;
this.right = null;
}
}
class BinarySearchTree
{
public Node root;
//Class constructors
public BinarySearchTree()
{
root = null;
}
//insert element
public void add_node(int data)
{
//Create a new binary tree node
Node new_node = new Node(data);
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 >= 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
{
Console.WriteLine("Memory Overflow");
}
}
//Find and print minimum node of binary search tree
public void min_node()
{
if (root == null)
{
Console.Write("\n Empty Tree");
}
else
{
Console.Write(" Minimum node is : ");
Node temp = root;
//Find leftmost node from to root
while (temp.left != null)
{
//Visit left node
temp = temp.left;
}
Console.Write(" " + temp.data + "\n");
}
}
public static void Main(String[] args)
{
BinarySearchTree obj = new BinarySearchTree();
//Create binary search tree
/*
5
/ \
3 9
/ \ / \
-7 4 8 11
\ / \
2 7 12
*/
obj.add_node(5);
obj.add_node(3);
obj.add_node(9);
obj.add_node(-7);
obj.add_node(4);
obj.add_node(8);
obj.add_node(11);
obj.add_node(12);
obj.add_node(7);
obj.add_node(2);
obj.min_node();
}
}
Output
Minimum node is : -7
<?php
//Php program
//Find minimum node in binary search tree
//Binary Tree Node
class Node
{
public $data;
public $left;
public $right;
function __construct($data)
{
$this->data = $data;
$this->left = null;
$this->right = null;
}
}
class BinarySearchTree
{
public $root;
//Class constructors
function __construct()
{
$this->root = null;
}
//insert element
public function add_node($data)
{
//Create a new binary tree node
$new_node = new Node($data);
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 >= $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
{
echo "Memory Overflow";
}
}
//Find and print minimum node of binary search tree
public function min_node()
{
if ($this->root == null)
{
echo "\n Empty Tree";
}
else
{
echo " Minimum node is : ";
$temp = $this->root;
//Find leftmost node from to root
while ($temp->left != null)
{
//Visit left node
$temp = $temp->left;
}
echo " ". $temp->data ."\n";
}
}
}
function main()
{
$obj = new BinarySearchTree();
//Create binary search tree
/*
5
/ \
3 9
/ \ / \
-7 4 8 11
\ / \
2 7 12
*/
$obj->add_node(5);
$obj->add_node(3);
$obj->add_node(9);
$obj->add_node(-7);
$obj->add_node(4);
$obj->add_node(8);
$obj->add_node(11);
$obj->add_node(12);
$obj->add_node(7);
$obj->add_node(2);
$obj->min_node();
}
main();
Output
Minimum node is : -7
//Node Js program
//Find minimum node in binary search tree
//Binary Tree Node
class Node
{
constructor(data)
{
this.data = data;
this.left = null;
this.right = null;
}
}
class BinarySearchTree
{
//Class constructors
constructor()
{
this.root = null;
}
//insert element
add_node(data)
{
//Create a new binary tree node
var new_node = new Node(data);
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 >= 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
{
process.stdout.write("Memory Overflow");
}
}
//Find and print minimum node of binary search tree
min_node()
{
if (this.root == null)
{
process.stdout.write("\n Empty Tree");
}
else
{
process.stdout.write(" Minimum node is : ");
var temp = this.root;
//Find leftmost node from to root
while (temp.left != null)
{
//Visit left node
temp = temp.left;
}
process.stdout.write(" " + temp.data + "\n");
}
}
}
function main()
{
var obj = new BinarySearchTree();
//Create binary search tree
/*
5
/ \
3 9
/ \ / \
-7 4 8 11
\ / \
2 7 12
*/
obj.add_node(5);
obj.add_node(3);
obj.add_node(9);
obj.add_node(-7);
obj.add_node(4);
obj.add_node(8);
obj.add_node(11);
obj.add_node(12);
obj.add_node(7);
obj.add_node(2);
obj.min_node();
}
main();
Output
Minimum node is : -7
# Python 3 program
# Find minimum node in binary search tree
# Binary Tree Node
class Node :
def __init__(self, data) :
self.data = data
self.left = None
self.right = None
class BinarySearchTree :
# Class constructors
def __init__(self) :
self.root = None
# insert element
def add_node(self, data) :
# Create a new binary tree node
new_node = Node(data)
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 >= data) :
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("Memory Overflow", end = "")
# Find and print minimum node of binary search tree
def min_node(self) :
if (self.root == None) :
print("\n Empty Tree", end = "")
else :
print(" Minimum node is : ", end = "")
temp = self.root
# Find leftmost node from to root
while (temp.left != None) :
# Visit left node
temp = temp.left
print(" ", temp.data ,"\n", end = "")
def main() :
obj = BinarySearchTree()
# Create binary search tree
#
# 5
# / \
# 3 9
# / \ / \
# -7 4 8 11
# \ / \
# 2 7 12
#
obj.add_node(5)
obj.add_node(3)
obj.add_node(9)
obj.add_node(-7)
obj.add_node(4)
obj.add_node(8)
obj.add_node(11)
obj.add_node(12)
obj.add_node(7)
obj.add_node(2)
obj.min_node()
if __name__ == "__main__": main()
Output
Minimum node is : -7
# Ruby program
# Find minimum node in binary search tree
# Binary 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)
self.data = data
self.left = nil
self.right = nil
end
end
class BinarySearchTree
# Define the accessor and reader of class BinarySearchTree
attr_reader :root
attr_accessor :root
# Class constructors
def initialize()
@root = nil
end
# insert element
def add_node(data)
# Create a new binary tree node
new_node = Node.new(data)
if (new_node != nil)
if (self.root == nil)
# When adds a first node in binary tree
self.root = new_node
else
find = self.root
# add new node to proper position
while (find != nil)
if (find.data >= data)
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("Memory Overflow")
end
end
# Find and print minimum node of binary search tree
def min_node()
if (@root == nil)
print("\n Empty Tree")
else
print(" Minimum node is : ")
temp = @root
# Find leftmost node from to root
while (temp.left != nil)
# Visit left node
temp = temp.left
end
print(" ", temp.data ,"\n")
end
end
end
def main()
obj = BinarySearchTree.new()
# Create binary search tree
#
# 5
# / \
# 3 9
# / \ / \
# -7 4 8 11
# \ / \
# 2 7 12
#
obj.add_node(5)
obj.add_node(3)
obj.add_node(9)
obj.add_node(-7)
obj.add_node(4)
obj.add_node(8)
obj.add_node(11)
obj.add_node(12)
obj.add_node(7)
obj.add_node(2)
obj.min_node()
end
main()
Output
Minimum node is : -7
//Scala program
//Find minimum node in binary search tree
//Binary 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)
{
//Class constructors
def this()
{
this(null);
}
//insert element
def add_node(data: Int): Unit = {
//Create a new binary tree node
var new_node: Node = new Node(data);
if (new_node != null)
{
if (this.root == null)
{
//When adds a first node in binary tree
this.root = new_node;
}
else
{
var find: Node = this.root;
//add new node to proper position
while (find != null)
{
if (find.data >= data)
{
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("Memory Overflow");
}
}
//Find and print minimum node of binary search tree
def min_node(): Unit = {
if (root == null)
{
print("\n Empty Tree");
}
else
{
print(" Minimum node is : ");
var temp: Node = root;
//Find leftmost node from to root
while (temp.left != null)
{
//Visit left node
temp = temp.left;
}
print(" " + temp.data + "\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: BinarySearchTree = new BinarySearchTree();
//Create binary search tree
/*
5
/ \
3 9
/ \ / \
-7 4 8 11
\ / \
2 7 12
*/
obj.add_node(5);
obj.add_node(3);
obj.add_node(9);
obj.add_node(-7);
obj.add_node(4);
obj.add_node(8);
obj.add_node(11);
obj.add_node(12);
obj.add_node(7);
obj.add_node(2);
obj.min_node();
}
}
Output
Minimum node is : -7
//Swift program
//Find minimum node in binary search tree
//Binary Tree Node
class Node
{
var data: Int;
var left: Node? ;
var right: Node? ;
init(_ data: Int)
{
self.data = data;
self.left = nil;
self.right = nil;
}
}
class BinarySearchTree
{
var root: Node? ;
//Class constructors
init()
{
self.root = nil;
}
//insert element
func add_node(_ data: Int)
{
//Create a new binary tree node
let new_node: Node? = Node(data);
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 >= data)
{
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("Memory Overflow", terminator: "");
}
}
//Find and print minimum node of binary search tree
func min_node()
{
if (self.root == nil)
{
print("\n Empty Tree", terminator: "");
}
else
{
print(" Minimum node is : ", terminator: "");
var temp: Node? = self.root;
//Find leftmost node from to root
while (temp!.left != nil)
{
//Visit left node
temp = temp!.left;
}
print(" ", temp!.data ,"\n", terminator: "");
}
}
}
func main()
{
let obj: BinarySearchTree = BinarySearchTree();
//Create binary search tree
/*
5
/ \
3 9
/ \ / \
-7 4 8 11
\ / \
2 7 12
*/
obj.add_node(5);
obj.add_node(3);
obj.add_node(9);
obj.add_node(-7);
obj.add_node(4);
obj.add_node(8);
obj.add_node(11);
obj.add_node(12);
obj.add_node(7);
obj.add_node(2);
obj.min_node();
}
main();
Output
Minimum node is : -7
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