SlideShare a Scribd company logo
Java:
Create a java application using the code example in the following URL regarding a binary search
tree structure:
CODE EXAMPLE:
class BST_class {
//node class that defines BST node
class Node {
int key;
Node left, right;
public Node(int data){
key = data;
left = right = null;
}
}
// BST root node
Node root;
// Constructor for BST =>initial empty tree
BST_class(){
root = null;
}
//delete a node from BST
void deleteKey(int key) {
root = delete_Recursive(root, key);
}
//recursive delete function
Node delete_Recursive(Node root, int key) {
//tree is empty
if (root == null) return root;
//traverse the tree
if (key < root.key) //traverse left subtree
root.left = delete_Recursive(root.left, key);
else if (key > root.key) //traverse right subtree
root.right = delete_Recursive(root.right, key);
else {
// node contains only one child
if (root.left == null)
return root.right;
else if (root.right == null)
return root.left;
// node has two children;
//get inorder successor (min value in the right subtree)
root.key = minValue(root.right);
// Delete the inorder successor
root.right = delete_Recursive(root.right, root.key);
}
return root;
}
int minValue(Node root) {
//initially minval = root
int minval = root.key;
//find minval
while (root.left != null) {
minval = root.left.key;
root = root.left;
}
return minval;
}
// insert a node in BST
void insert(int key) {
root = insert_Recursive(root, key);
}
//recursive insert function
Node insert_Recursive(Node root, int key) {
//tree is empty
if (root == null) {
root = new Node(key);
return root;
}
//traverse the tree
if (key < root.key) //insert in the left subtree
root.left = insert_Recursive(root.left, key);
else if (key > root.key) //insert in the right subtree
root.right = insert_Recursive(root.right, key);
// return pointer
return root;
}
// method for inorder traversal of BST
void inorder() {
inorder_Recursive(root);
}
// recursively traverse the BST
void inorder_Recursive(Node root) {
if (root != null) {
inorder_Recursive(root.left);
System.out.print(root.key + " ");
inorder_Recursive(root.right);
}
}
boolean search(int key) {
root = search_Recursive(root, key);
if (root!= null)
return true;
else
return false;
}
//recursive insert function
Node search_Recursive(Node root, int key) {
// Base Cases: root is null or key is present at root
if (root==null || root.key==key)
return root;
// val is greater than root's key
if (root.key > key)
return search_Recursive(root.left, key);
// val is less than root's key
return search_Recursive(root.right, key);
}
}
class Main{
public static void main(String[] args) {
//create a BST object
BST_class bst = new BST_class();
/* BST tree example
45
/ 
10 90
/  /
7 12 50 */
//insert data into BST
bst.insert(45);
bst.insert(10);
bst.insert(7);
bst.insert(12);
bst.insert(90);
bst.insert(50);
//print the BST
System.out.println("The BST Created with input data(Left-root-right):");
bst.inorder();
//delete leaf node
System.out.println("nThe BST after Delete 12(leaf node):");
bst.deleteKey(12);
bst.inorder();
//delete the node with one child
System.out.println("nThe BST after Delete 90 (node with 1 child):");
bst.deleteKey(90);
bst.inorder();
//delete node with two children
System.out.println("nThe BST after Delete 45 (Node with two children):");
bst.deleteKey(45);
bst.inorder();
//search a key in the BST
boolean ret_val = bst.search (50);
System.out.println("nKey 50 found in BST:" + ret_val );
ret_val = bst.search (12);
System.out.println("nKey 12 found in BST:" + ret_val );
}
}

More Related Content

PDF
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
PDF
BSTNode.Java Node class used for implementing the BST. .pdf
PDF
in this assignment you are asked to write a simple driver program an.pdf
DOCX
For this project, write a program that stores integers in a binary.docx
PDF
Given the following codepackage data1;import java.util.;p.pdf
DOCX
Required to augment the authors Binary Search Tree (BST) code to .docx
PDF
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
PDF
I have a .java program that I need to modify so that it1) reads i.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
BSTNode.Java Node class used for implementing the BST. .pdf
in this assignment you are asked to write a simple driver program an.pdf
For this project, write a program that stores integers in a binary.docx
Given the following codepackage data1;import java.util.;p.pdf
Required to augment the authors Binary Search Tree (BST) code to .docx
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
I have a .java program that I need to modify so that it1) reads i.pdf

Similar to JavaCreate a java application using the code example in the follo.pdf (20)

PDF
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
PDF
The following is to be written in JavaThe following is the BSTree.pdf
PDF
A perfect left-sided binary tree is a binary tree where every intern.pdf
DOCX
Once you have all the structures working as intended- it is time to co.docx
PDF
To create a node for a binary search tree (BTNode, ) and to create a .pdf
PDF
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
PDF
08 binarysearchtrees 1
PPTX
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
PDF
Add to BST.java a method height() that computes the height of the tr.pdf
PDF
Generics and data structures in Java for an introductory programming course
PDF
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
PPT
s11_bin_search_trees.ppt
DOCX
1 COMP 182 Fall 2016 Project 6 Binary Search Trees .docx
PDF
For the code below complete the preOrder() method so that it perform.pdf
PPTX
Trees data structure
PDF
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
DOCX
Please finish everything marked TODO BinaryNode-java public class Bi.docx
PDF
import javautilQueue import javautilLinkedList import .pdf
PPTX
Lecture_10 - Revised.pptx
PDF
There is BinarySearchTree class. When removing a node from a BST, we.pdf
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
The following is to be written in JavaThe following is the BSTree.pdf
A perfect left-sided binary tree is a binary tree where every intern.pdf
Once you have all the structures working as intended- it is time to co.docx
To create a node for a binary search tree (BTNode, ) and to create a .pdf
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
08 binarysearchtrees 1
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
Add to BST.java a method height() that computes the height of the tr.pdf
Generics and data structures in Java for an introductory programming course
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
s11_bin_search_trees.ppt
1 COMP 182 Fall 2016 Project 6 Binary Search Trees .docx
For the code below complete the preOrder() method so that it perform.pdf
Trees data structure
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
Please finish everything marked TODO BinaryNode-java public class Bi.docx
import javautilQueue import javautilLinkedList import .pdf
Lecture_10 - Revised.pptx
There is BinarySearchTree class. When removing a node from a BST, we.pdf

More from ambersushil (20)

PDF
Juan Williams is a writer with a best selling novel. Juan wishes to .pdf
PDF
Juliets Delivery Services complet� las transacciones proporcionadas.pdf
PDF
Judge Mark Griffiths finds that Moodle is a relentless and predatory.pdf
PDF
Jones, Inc. uses the equity method of accounting for its investment .pdf
PDF
John y Sharon celebran un contrato de trabajo, que especifica que Jo.pdf
PDF
John, Lesa y Trevor forman una sociedad de responsabilidad limitada..pdf
PDF
John has recently learned about advance directives in a healthcare l.pdf
PDF
Joe�s Ristorant� is a small restaurant near a college campus. It ser.pdf
PDF
Johanna Murray, una activista clim�tica en The National Footprint Fo.pdf
PDF
Joannie est� en la categor�a impositiva del 15. Trabaja para una em.pdf
PDF
Joan accompanies her husband Dan to the clinic and reports to the nu.pdf
PDF
Jim blindfolds Dwight and gives him a 4lb weight to hold. He then ta.pdf
PDF
Jill and Jim are married and they have 2 children (Josh and Kenny). .pdf
PDF
Jet blue airways, con sede en Nueva York, hab�a comenzado 2007 en ra.pdf
PDF
Jerrold tiene una filosof�a con respecto a la retenci�n de empleados.pdf
PDF
Jes�s Mart�nez es un hombre hispano de 46 a�os de su unidad. Ingres�.pdf
PDF
Jeremiah Restoration Company completed the following selected transa.pdf
PDF
Jeremy est� considerando usar mucho el color blanco en el logotipo p.pdf
PDF
JenkinsWe have two dozen APIs to deploy, and the dev team was able.pdf
PDF
Jefferson Tutoring had the following payroll information on February.pdf
Juan Williams is a writer with a best selling novel. Juan wishes to .pdf
Juliets Delivery Services complet� las transacciones proporcionadas.pdf
Judge Mark Griffiths finds that Moodle is a relentless and predatory.pdf
Jones, Inc. uses the equity method of accounting for its investment .pdf
John y Sharon celebran un contrato de trabajo, que especifica que Jo.pdf
John, Lesa y Trevor forman una sociedad de responsabilidad limitada..pdf
John has recently learned about advance directives in a healthcare l.pdf
Joe�s Ristorant� is a small restaurant near a college campus. It ser.pdf
Johanna Murray, una activista clim�tica en The National Footprint Fo.pdf
Joannie est� en la categor�a impositiva del 15. Trabaja para una em.pdf
Joan accompanies her husband Dan to the clinic and reports to the nu.pdf
Jim blindfolds Dwight and gives him a 4lb weight to hold. He then ta.pdf
Jill and Jim are married and they have 2 children (Josh and Kenny). .pdf
Jet blue airways, con sede en Nueva York, hab�a comenzado 2007 en ra.pdf
Jerrold tiene una filosof�a con respecto a la retenci�n de empleados.pdf
Jes�s Mart�nez es un hombre hispano de 46 a�os de su unidad. Ingres�.pdf
Jeremiah Restoration Company completed the following selected transa.pdf
Jeremy est� considerando usar mucho el color blanco en el logotipo p.pdf
JenkinsWe have two dozen APIs to deploy, and the dev team was able.pdf
Jefferson Tutoring had the following payroll information on February.pdf

Recently uploaded (20)

PPTX
Pharma ospi slides which help in ospi learning
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Cell Types and Its function , kingdom of life
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Pre independence Education in Inndia.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
Classroom Observation Tools for Teachers
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Lesson notes of climatology university.
PDF
Sports Quiz easy sports quiz sports quiz
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Pharma ospi slides which help in ospi learning
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
TR - Agricultural Crops Production NC III.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Cell Types and Its function , kingdom of life
VCE English Exam - Section C Student Revision Booklet
Pre independence Education in Inndia.pdf
01-Introduction-to-Information-Management.pdf
Classroom Observation Tools for Teachers
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Final Presentation General Medicine 03-08-2024.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Supply Chain Operations Speaking Notes -ICLT Program
2.FourierTransform-ShortQuestionswithAnswers.pdf
Lesson notes of climatology university.
Sports Quiz easy sports quiz sports quiz
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
O7-L3 Supply Chain Operations - ICLT Program
school management -TNTEU- B.Ed., Semester II Unit 1.pptx

JavaCreate a java application using the code example in the follo.pdf

  • 1. Java: Create a java application using the code example in the following URL regarding a binary search tree structure: CODE EXAMPLE: class BST_class { //node class that defines BST node class Node { int key; Node left, right; public Node(int data){ key = data; left = right = null; } } // BST root node Node root; // Constructor for BST =>initial empty tree BST_class(){ root = null; } //delete a node from BST void deleteKey(int key) { root = delete_Recursive(root, key); } //recursive delete function Node delete_Recursive(Node root, int key) { //tree is empty if (root == null) return root; //traverse the tree if (key < root.key) //traverse left subtree root.left = delete_Recursive(root.left, key);
  • 2. else if (key > root.key) //traverse right subtree root.right = delete_Recursive(root.right, key); else { // node contains only one child if (root.left == null) return root.right; else if (root.right == null) return root.left; // node has two children; //get inorder successor (min value in the right subtree) root.key = minValue(root.right); // Delete the inorder successor root.right = delete_Recursive(root.right, root.key); } return root; } int minValue(Node root) { //initially minval = root int minval = root.key; //find minval while (root.left != null) { minval = root.left.key; root = root.left; } return minval; } // insert a node in BST void insert(int key) { root = insert_Recursive(root, key); } //recursive insert function
  • 3. Node insert_Recursive(Node root, int key) { //tree is empty if (root == null) { root = new Node(key); return root; } //traverse the tree if (key < root.key) //insert in the left subtree root.left = insert_Recursive(root.left, key); else if (key > root.key) //insert in the right subtree root.right = insert_Recursive(root.right, key); // return pointer return root; } // method for inorder traversal of BST void inorder() { inorder_Recursive(root); } // recursively traverse the BST void inorder_Recursive(Node root) { if (root != null) { inorder_Recursive(root.left); System.out.print(root.key + " "); inorder_Recursive(root.right); } } boolean search(int key) { root = search_Recursive(root, key); if (root!= null) return true; else return false; }
  • 4. //recursive insert function Node search_Recursive(Node root, int key) { // Base Cases: root is null or key is present at root if (root==null || root.key==key) return root; // val is greater than root's key if (root.key > key) return search_Recursive(root.left, key); // val is less than root's key return search_Recursive(root.right, key); } } class Main{ public static void main(String[] args) { //create a BST object BST_class bst = new BST_class(); /* BST tree example 45 / 10 90 / / 7 12 50 */ //insert data into BST bst.insert(45); bst.insert(10); bst.insert(7); bst.insert(12); bst.insert(90); bst.insert(50); //print the BST System.out.println("The BST Created with input data(Left-root-right):"); bst.inorder(); //delete leaf node System.out.println("nThe BST after Delete 12(leaf node):");
  • 5. bst.deleteKey(12); bst.inorder(); //delete the node with one child System.out.println("nThe BST after Delete 90 (node with 1 child):"); bst.deleteKey(90); bst.inorder(); //delete node with two children System.out.println("nThe BST after Delete 45 (Node with two children):"); bst.deleteKey(45); bst.inorder(); //search a key in the BST boolean ret_val = bst.search (50); System.out.println("nKey 50 found in BST:" + ret_val ); ret_val = bst.search (12); System.out.println("nKey 12 found in BST:" + ret_val ); } }