SlideShare a Scribd company logo
1 of 10
Download to read offline
package DataStructures;
public class HelloWorld >
{
public HelloWorld( )
{
root = null;
}
public void insert( AnyType x )
{
root = insert( x, root );
}
public void remove( AnyType x )
{
root = remove( x, root );
}
public AnyType findMin( )
{
if( isEmpty( ) )
System.out.println("tree is empty");
return findMin( root ).element;
}
public AnyType findMax( )
{
if( isEmpty( ) )
System.out.println("tree is empty");
return findMax( root ).element;
}
public boolean contains( AnyType x )
{
return contains( x, root );
}
public void makeEmpty( )
{
root = null;
}
public boolean isEmpty( )
{
return root == null;
}
public void printTree( )
{
if( isEmpty( ) )
System.out.println( "Empty tree" );
else
printTree( root );
}
private BinaryNode insert( AnyType x, BinaryNode t )
{
if( t == null )
return new BinaryNode( x, null, null );
int compareResult = x.compareTo( t.element );
if( compareResult < 0 )
t.left = insert( x, t.left );
else if( compareResult > 0 )
t.right = insert( x, t.right );
else
; // Duplicate; do nothing
return t;
}
private BinaryNode remove( AnyType x, BinaryNode t )
{
if( t == null )
return t; // Item not found; do nothing
int compareResult = x.compareTo( t.element );
if( compareResult < 0 )
t.left = remove( x, t.left );
else if( compareResult > 0 )
t.right = remove( x, t.right );
else if( t.left != null && t.right != null ) // Two children
{
t.element = findMin( t.right ).element;
t.right = remove( t.element, t.right );
}
else
t = ( t.left != null ) ? t.left : t.right;
return t;
}
private BinaryNode findMin( BinaryNode t )
{
if( t == null )
return null;
else if( t.left == null )
return t;
return findMin( t.left );
}
private BinaryNode findMax( BinaryNode t )
{
if( t != null )
while( t.right != null )
t = t.right;
return t;
}
private boolean contains( AnyType x, BinaryNode t )
{
if( t == null )
return false;
int compareResult = x.compareTo( t.element );
if( compareResult < 0 )
return contains( x, t.left );
else if( compareResult > 0 )
return contains( x, t.right );
else
return true; // Match
}
private void printTree( BinaryNode t )
{
if( t != null )
{
printTree( t.left );
System.out.println( t.element );
printTree( t.right );
}
}
private int height( BinaryNode t )
{
if( t == null )
return -1;
else
return 1 + Math.max( height( t.left ), height( t.right ) );
}
// Basic node stored in unbalanced binary search trees
private static class BinaryNode
{
// Constructors
BinaryNode( AnyType theElement )
{
this( theElement, null, null );
}
BinaryNode( AnyType theElement, BinaryNode lt, BinaryNode rt )
{
element = theElement;
left = lt;
right = rt;
}
AnyType element; // The data in the node
BinaryNode left; // Left child
BinaryNode right; // Right child
}
/** The tree root. */
private BinaryNode root;
public static void main( String [ ] args )
{
HelloWorld t = new HelloWorld( );
final int NUMS = 4000;
final int GAP = 37;
System.out.println( "Checking... (no more output means success)" );
for( int i = GAP; i != 0; i = ( i + GAP ) % NUMS )
t.insert( i );
for( int i = 1; i < NUMS; i+= 2 )
t.remove( i );
if( NUMS < 40 )
t.printTree( );
if( t.findMin( ) != 2 || t.findMax( ) != NUMS - 2 )
System.out.println( "FindMin or FindMax error!" );
for( int i = 2; i < NUMS; i+=2 )
if( !t.contains( i ) )
System.out.println( "Find error1!" );
for( int i = 1; i < NUMS; i+=2 )
{
if( t.contains( i ) )
System.out.println( "Find error2!" );
}
}
}
Solution
package DataStructures;
public class HelloWorld >
{
public HelloWorld( )
{
root = null;
}
public void insert( AnyType x )
{
root = insert( x, root );
}
public void remove( AnyType x )
{
root = remove( x, root );
}
public AnyType findMin( )
{
if( isEmpty( ) )
System.out.println("tree is empty");
return findMin( root ).element;
}
public AnyType findMax( )
{
if( isEmpty( ) )
System.out.println("tree is empty");
return findMax( root ).element;
}
public boolean contains( AnyType x )
{
return contains( x, root );
}
public void makeEmpty( )
{
root = null;
}
public boolean isEmpty( )
{
return root == null;
}
public void printTree( )
{
if( isEmpty( ) )
System.out.println( "Empty tree" );
else
printTree( root );
}
private BinaryNode insert( AnyType x, BinaryNode t )
{
if( t == null )
return new BinaryNode( x, null, null );
int compareResult = x.compareTo( t.element );
if( compareResult < 0 )
t.left = insert( x, t.left );
else if( compareResult > 0 )
t.right = insert( x, t.right );
else
; // Duplicate; do nothing
return t;
}
private BinaryNode remove( AnyType x, BinaryNode t )
{
if( t == null )
return t; // Item not found; do nothing
int compareResult = x.compareTo( t.element );
if( compareResult < 0 )
t.left = remove( x, t.left );
else if( compareResult > 0 )
t.right = remove( x, t.right );
else if( t.left != null && t.right != null ) // Two children
{
t.element = findMin( t.right ).element;
t.right = remove( t.element, t.right );
}
else
t = ( t.left != null ) ? t.left : t.right;
return t;
}
private BinaryNode findMin( BinaryNode t )
{
if( t == null )
return null;
else if( t.left == null )
return t;
return findMin( t.left );
}
private BinaryNode findMax( BinaryNode t )
{
if( t != null )
while( t.right != null )
t = t.right;
return t;
}
private boolean contains( AnyType x, BinaryNode t )
{
if( t == null )
return false;
int compareResult = x.compareTo( t.element );
if( compareResult < 0 )
return contains( x, t.left );
else if( compareResult > 0 )
return contains( x, t.right );
else
return true; // Match
}
private void printTree( BinaryNode t )
{
if( t != null )
{
printTree( t.left );
System.out.println( t.element );
printTree( t.right );
}
}
private int height( BinaryNode t )
{
if( t == null )
return -1;
else
return 1 + Math.max( height( t.left ), height( t.right ) );
}
// Basic node stored in unbalanced binary search trees
private static class BinaryNode
{
// Constructors
BinaryNode( AnyType theElement )
{
this( theElement, null, null );
}
BinaryNode( AnyType theElement, BinaryNode lt, BinaryNode rt )
{
element = theElement;
left = lt;
right = rt;
}
AnyType element; // The data in the node
BinaryNode left; // Left child
BinaryNode right; // Right child
}
/** The tree root. */
private BinaryNode root;
public static void main( String [ ] args )
{
HelloWorld t = new HelloWorld( );
final int NUMS = 4000;
final int GAP = 37;
System.out.println( "Checking... (no more output means success)" );
for( int i = GAP; i != 0; i = ( i + GAP ) % NUMS )
t.insert( i );
for( int i = 1; i < NUMS; i+= 2 )
t.remove( i );
if( NUMS < 40 )
t.printTree( );
if( t.findMin( ) != 2 || t.findMax( ) != NUMS - 2 )
System.out.println( "FindMin or FindMax error!" );
for( int i = 2; i < NUMS; i+=2 )
if( !t.contains( i ) )
System.out.println( "Find error1!" );
for( int i = 1; i < NUMS; i+=2 )
{
if( t.contains( i ) )
System.out.println( "Find error2!" );
}
}
}

More Related Content

Similar to package DataStructures; public class HelloWorld AnyType extends.pdf

Given the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfGiven the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfillyasraja7
 
I have a .java program that I need to modify so that it1) reads i.pdf
I have a .java program that I need to modify so that it1) reads i.pdfI have a .java program that I need to modify so that it1) reads i.pdf
I have a .java program that I need to modify so that it1) reads i.pdfallystraders
 
mainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdfmainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdffathimafancyjeweller
 
Once you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docxOnce you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docxfarrahkur54
 
( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdf
( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdf( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdf
( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdfaristogifts99
 
1. Add a breadth-first (level-order) traversal function to the binar.pdf
1. Add a breadth-first (level-order) traversal function to the binar.pdf1. Add a breadth-first (level-order) traversal function to the binar.pdf
1. Add a breadth-first (level-order) traversal function to the binar.pdfarjuncp10
 
I need help finishing this code in JavaYou will need to create t.pdf
I need help finishing this code in JavaYou will need to create t.pdfI need help finishing this code in JavaYou will need to create t.pdf
I need help finishing this code in JavaYou will need to create t.pdfallurafashions98
 
Table.java Huffman code frequency tableimport java.io.;im.docx
 Table.java Huffman code frequency tableimport java.io.;im.docx Table.java Huffman code frequency tableimport java.io.;im.docx
Table.java Huffman code frequency tableimport java.io.;im.docxMARRY7
 
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdfANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdfanukoolelectronics
 
This is to test a balanced tree. I need help testing an unbalanced t.pdf
This is to test a balanced tree. I need help testing an unbalanced t.pdfThis is to test a balanced tree. I need help testing an unbalanced t.pdf
This is to test a balanced tree. I need help testing an unbalanced t.pdfakaluza07
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldBTI360
 
Here is the code given in the instructionsclass AVL {.pdf
Here is the code given in the instructionsclass AVL {.pdfHere is the code given in the instructionsclass AVL {.pdf
Here is the code given in the instructionsclass AVL {.pdfmanjan6
 
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdfModify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdfarjuncorner565
 
Please help write BinaryTree-java Thank you! Create a class BinaryTr.pdf
Please help write BinaryTree-java Thank you!   Create a class BinaryTr.pdfPlease help write BinaryTree-java Thank you!   Create a class BinaryTr.pdf
Please help write BinaryTree-java Thank you! Create a class BinaryTr.pdfinfo750646
 
You can list anything, it doesnt matter. I just want to see code f.pdf
You can list anything, it doesnt matter. I just want to see code f.pdfYou can list anything, it doesnt matter. I just want to see code f.pdf
You can list anything, it doesnt matter. I just want to see code f.pdffashionbigchennai
 
Write a program that displays an AVL tree along with its balance fac.docx
 Write a program that displays an AVL tree  along with its balance fac.docx Write a program that displays an AVL tree  along with its balance fac.docx
Write a program that displays an AVL tree along with its balance fac.docxajoy21
 
Here is the following code mentioned in the instructions.pdf
Here is the following code mentioned in the instructions.pdfHere is the following code mentioned in the instructions.pdf
Here is the following code mentioned in the instructions.pdfarbaazrabs
 
Implement a function TNode copy_tree(TNode t) that creates a copy .pdf
Implement a function TNode copy_tree(TNode t) that creates a copy .pdfImplement a function TNode copy_tree(TNode t) that creates a copy .pdf
Implement a function TNode copy_tree(TNode t) that creates a copy .pdffeetshoemart
 

Similar to package DataStructures; public class HelloWorld AnyType extends.pdf (20)

Binary Tree
Binary  TreeBinary  Tree
Binary Tree
 
Given the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfGiven the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdf
 
I have a .java program that I need to modify so that it1) reads i.pdf
I have a .java program that I need to modify so that it1) reads i.pdfI have a .java program that I need to modify so that it1) reads i.pdf
I have a .java program that I need to modify so that it1) reads i.pdf
 
mainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdfmainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdf
 
Once you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docxOnce you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docx
 
( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdf
( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdf( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdf
( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdf
 
1. Add a breadth-first (level-order) traversal function to the binar.pdf
1. Add a breadth-first (level-order) traversal function to the binar.pdf1. Add a breadth-first (level-order) traversal function to the binar.pdf
1. Add a breadth-first (level-order) traversal function to the binar.pdf
 
I need help finishing this code in JavaYou will need to create t.pdf
I need help finishing this code in JavaYou will need to create t.pdfI need help finishing this code in JavaYou will need to create t.pdf
I need help finishing this code in JavaYou will need to create t.pdf
 
Table.java Huffman code frequency tableimport java.io.;im.docx
 Table.java Huffman code frequency tableimport java.io.;im.docx Table.java Huffman code frequency tableimport java.io.;im.docx
Table.java Huffman code frequency tableimport java.io.;im.docx
 
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdfANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
 
week-14x
week-14xweek-14x
week-14x
 
This is to test a balanced tree. I need help testing an unbalanced t.pdf
This is to test a balanced tree. I need help testing an unbalanced t.pdfThis is to test a balanced tree. I need help testing an unbalanced t.pdf
This is to test a balanced tree. I need help testing an unbalanced t.pdf
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 World
 
Here is the code given in the instructionsclass AVL {.pdf
Here is the code given in the instructionsclass AVL {.pdfHere is the code given in the instructionsclass AVL {.pdf
Here is the code given in the instructionsclass AVL {.pdf
 
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdfModify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
 
Please help write BinaryTree-java Thank you! Create a class BinaryTr.pdf
Please help write BinaryTree-java Thank you!   Create a class BinaryTr.pdfPlease help write BinaryTree-java Thank you!   Create a class BinaryTr.pdf
Please help write BinaryTree-java Thank you! Create a class BinaryTr.pdf
 
You can list anything, it doesnt matter. I just want to see code f.pdf
You can list anything, it doesnt matter. I just want to see code f.pdfYou can list anything, it doesnt matter. I just want to see code f.pdf
You can list anything, it doesnt matter. I just want to see code f.pdf
 
Write a program that displays an AVL tree along with its balance fac.docx
 Write a program that displays an AVL tree  along with its balance fac.docx Write a program that displays an AVL tree  along with its balance fac.docx
Write a program that displays an AVL tree along with its balance fac.docx
 
Here is the following code mentioned in the instructions.pdf
Here is the following code mentioned in the instructions.pdfHere is the following code mentioned in the instructions.pdf
Here is the following code mentioned in the instructions.pdf
 
Implement a function TNode copy_tree(TNode t) that creates a copy .pdf
Implement a function TNode copy_tree(TNode t) that creates a copy .pdfImplement a function TNode copy_tree(TNode t) that creates a copy .pdf
Implement a function TNode copy_tree(TNode t) that creates a copy .pdf
 

More from apleathers

While a majority of the worlds current electricity supply is gener.pdf
While a majority of the worlds current electricity supply is gener.pdfWhile a majority of the worlds current electricity supply is gener.pdf
While a majority of the worlds current electricity supply is gener.pdfapleathers
 
What is broadbandWhat is the potential for broadband wireless syste.pdf
What is broadbandWhat is the potential for broadband wireless syste.pdfWhat is broadbandWhat is the potential for broadband wireless syste.pdf
What is broadbandWhat is the potential for broadband wireless syste.pdfapleathers
 
WaterSolutionWater.pdf
WaterSolutionWater.pdfWaterSolutionWater.pdf
WaterSolutionWater.pdfapleathers
 
Thousands of repeating units called nephrons will form a kidney and .pdf
Thousands of repeating units called nephrons will form a kidney and .pdfThousands of repeating units called nephrons will form a kidney and .pdf
Thousands of repeating units called nephrons will form a kidney and .pdfapleathers
 
To understand the complexicities of the issue at hand we must first .pdf
To understand the complexicities of the issue at hand we must first .pdfTo understand the complexicities of the issue at hand we must first .pdf
To understand the complexicities of the issue at hand we must first .pdfapleathers
 
The P is the present value of the cash flow streamP = 100 (1+10.pdf
The P is the present value of the cash flow streamP = 100  (1+10.pdfThe P is the present value of the cash flow streamP = 100  (1+10.pdf
The P is the present value of the cash flow streamP = 100 (1+10.pdfapleathers
 
Ribose sugar puckers adopt for both Deoxy nucleotides and ribonucleo.pdf
Ribose sugar puckers adopt for both Deoxy nucleotides and ribonucleo.pdfRibose sugar puckers adopt for both Deoxy nucleotides and ribonucleo.pdf
Ribose sugar puckers adopt for both Deoxy nucleotides and ribonucleo.pdfapleathers
 
Ques-1 Prenatal diagnosis has both positive and potentially negativ.pdf
Ques-1 Prenatal diagnosis has both positive and potentially negativ.pdfQues-1 Prenatal diagnosis has both positive and potentially negativ.pdf
Ques-1 Prenatal diagnosis has both positive and potentially negativ.pdfapleathers
 
QuestionWhat are the elements of safe drinking water act What ar.pdf
QuestionWhat are the elements of safe drinking water act What ar.pdfQuestionWhat are the elements of safe drinking water act What ar.pdf
QuestionWhat are the elements of safe drinking water act What ar.pdfapleathers
 
Programimport java.util.; import java.io.; class FoodTruck.pdf
Programimport java.util.; import java.io.; class FoodTruck.pdfProgramimport java.util.; import java.io.; class FoodTruck.pdf
Programimport java.util.; import java.io.; class FoodTruck.pdfapleathers
 
Please follow the data and description Active Directory In gen.pdf
Please follow the data and description Active Directory In gen.pdfPlease follow the data and description Active Directory In gen.pdf
Please follow the data and description Active Directory In gen.pdfapleathers
 
it always attacks oxygen.the image doesnot workSolutionit alwa.pdf
it always attacks oxygen.the image doesnot workSolutionit alwa.pdfit always attacks oxygen.the image doesnot workSolutionit alwa.pdf
it always attacks oxygen.the image doesnot workSolutionit alwa.pdfapleathers
 
import java.util.Scanner;public class Fraction {   instan.pdf
import java.util.Scanner;public class Fraction {    instan.pdfimport java.util.Scanner;public class Fraction {    instan.pdf
import java.util.Scanner;public class Fraction {   instan.pdfapleathers
 
Hi,Please find the Ansswer below.PLAYLIST.h#include iostrea.pdf
Hi,Please find the Ansswer below.PLAYLIST.h#include iostrea.pdfHi,Please find the Ansswer below.PLAYLIST.h#include iostrea.pdf
Hi,Please find the Ansswer below.PLAYLIST.h#include iostrea.pdfapleathers
 
Family as a Social Institution A family can be defined as group of .pdf
Family as a Social Institution A family can be defined as group of .pdfFamily as a Social Institution A family can be defined as group of .pdf
Family as a Social Institution A family can be defined as group of .pdfapleathers
 
D. The actual development of new standards and protocols for the Int.pdf
D. The actual development of new standards and protocols for the Int.pdfD. The actual development of new standards and protocols for the Int.pdf
D. The actual development of new standards and protocols for the Int.pdfapleathers
 
Dependent Variable. A variable that depends on one or more other var.pdf
Dependent Variable. A variable that depends on one or more other var.pdfDependent Variable. A variable that depends on one or more other var.pdf
Dependent Variable. A variable that depends on one or more other var.pdfapleathers
 
C is correct. While all Layer 2 devices split collision domains, swi.pdf
C is correct. While all Layer 2 devices split collision domains, swi.pdfC is correct. While all Layer 2 devices split collision domains, swi.pdf
C is correct. While all Layer 2 devices split collision domains, swi.pdfapleathers
 
Answer.The following organisms are present in pod.1. Prokaryotes.pdf
Answer.The following organisms are present in pod.1. Prokaryotes.pdfAnswer.The following organisms are present in pod.1. Prokaryotes.pdf
Answer.The following organisms are present in pod.1. Prokaryotes.pdfapleathers
 
AnswerThe mobile elements are the elerments move form one place t.pdf
AnswerThe mobile elements are the elerments move form one place t.pdfAnswerThe mobile elements are the elerments move form one place t.pdf
AnswerThe mobile elements are the elerments move form one place t.pdfapleathers
 

More from apleathers (20)

While a majority of the worlds current electricity supply is gener.pdf
While a majority of the worlds current electricity supply is gener.pdfWhile a majority of the worlds current electricity supply is gener.pdf
While a majority of the worlds current electricity supply is gener.pdf
 
What is broadbandWhat is the potential for broadband wireless syste.pdf
What is broadbandWhat is the potential for broadband wireless syste.pdfWhat is broadbandWhat is the potential for broadband wireless syste.pdf
What is broadbandWhat is the potential for broadband wireless syste.pdf
 
WaterSolutionWater.pdf
WaterSolutionWater.pdfWaterSolutionWater.pdf
WaterSolutionWater.pdf
 
Thousands of repeating units called nephrons will form a kidney and .pdf
Thousands of repeating units called nephrons will form a kidney and .pdfThousands of repeating units called nephrons will form a kidney and .pdf
Thousands of repeating units called nephrons will form a kidney and .pdf
 
To understand the complexicities of the issue at hand we must first .pdf
To understand the complexicities of the issue at hand we must first .pdfTo understand the complexicities of the issue at hand we must first .pdf
To understand the complexicities of the issue at hand we must first .pdf
 
The P is the present value of the cash flow streamP = 100 (1+10.pdf
The P is the present value of the cash flow streamP = 100  (1+10.pdfThe P is the present value of the cash flow streamP = 100  (1+10.pdf
The P is the present value of the cash flow streamP = 100 (1+10.pdf
 
Ribose sugar puckers adopt for both Deoxy nucleotides and ribonucleo.pdf
Ribose sugar puckers adopt for both Deoxy nucleotides and ribonucleo.pdfRibose sugar puckers adopt for both Deoxy nucleotides and ribonucleo.pdf
Ribose sugar puckers adopt for both Deoxy nucleotides and ribonucleo.pdf
 
Ques-1 Prenatal diagnosis has both positive and potentially negativ.pdf
Ques-1 Prenatal diagnosis has both positive and potentially negativ.pdfQues-1 Prenatal diagnosis has both positive and potentially negativ.pdf
Ques-1 Prenatal diagnosis has both positive and potentially negativ.pdf
 
QuestionWhat are the elements of safe drinking water act What ar.pdf
QuestionWhat are the elements of safe drinking water act What ar.pdfQuestionWhat are the elements of safe drinking water act What ar.pdf
QuestionWhat are the elements of safe drinking water act What ar.pdf
 
Programimport java.util.; import java.io.; class FoodTruck.pdf
Programimport java.util.; import java.io.; class FoodTruck.pdfProgramimport java.util.; import java.io.; class FoodTruck.pdf
Programimport java.util.; import java.io.; class FoodTruck.pdf
 
Please follow the data and description Active Directory In gen.pdf
Please follow the data and description Active Directory In gen.pdfPlease follow the data and description Active Directory In gen.pdf
Please follow the data and description Active Directory In gen.pdf
 
it always attacks oxygen.the image doesnot workSolutionit alwa.pdf
it always attacks oxygen.the image doesnot workSolutionit alwa.pdfit always attacks oxygen.the image doesnot workSolutionit alwa.pdf
it always attacks oxygen.the image doesnot workSolutionit alwa.pdf
 
import java.util.Scanner;public class Fraction {   instan.pdf
import java.util.Scanner;public class Fraction {    instan.pdfimport java.util.Scanner;public class Fraction {    instan.pdf
import java.util.Scanner;public class Fraction {   instan.pdf
 
Hi,Please find the Ansswer below.PLAYLIST.h#include iostrea.pdf
Hi,Please find the Ansswer below.PLAYLIST.h#include iostrea.pdfHi,Please find the Ansswer below.PLAYLIST.h#include iostrea.pdf
Hi,Please find the Ansswer below.PLAYLIST.h#include iostrea.pdf
 
Family as a Social Institution A family can be defined as group of .pdf
Family as a Social Institution A family can be defined as group of .pdfFamily as a Social Institution A family can be defined as group of .pdf
Family as a Social Institution A family can be defined as group of .pdf
 
D. The actual development of new standards and protocols for the Int.pdf
D. The actual development of new standards and protocols for the Int.pdfD. The actual development of new standards and protocols for the Int.pdf
D. The actual development of new standards and protocols for the Int.pdf
 
Dependent Variable. A variable that depends on one or more other var.pdf
Dependent Variable. A variable that depends on one or more other var.pdfDependent Variable. A variable that depends on one or more other var.pdf
Dependent Variable. A variable that depends on one or more other var.pdf
 
C is correct. While all Layer 2 devices split collision domains, swi.pdf
C is correct. While all Layer 2 devices split collision domains, swi.pdfC is correct. While all Layer 2 devices split collision domains, swi.pdf
C is correct. While all Layer 2 devices split collision domains, swi.pdf
 
Answer.The following organisms are present in pod.1. Prokaryotes.pdf
Answer.The following organisms are present in pod.1. Prokaryotes.pdfAnswer.The following organisms are present in pod.1. Prokaryotes.pdf
Answer.The following organisms are present in pod.1. Prokaryotes.pdf
 
AnswerThe mobile elements are the elerments move form one place t.pdf
AnswerThe mobile elements are the elerments move form one place t.pdfAnswerThe mobile elements are the elerments move form one place t.pdf
AnswerThe mobile elements are the elerments move form one place t.pdf
 

Recently uploaded

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 

Recently uploaded (20)

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 

package DataStructures; public class HelloWorld AnyType extends.pdf

  • 1. package DataStructures; public class HelloWorld > { public HelloWorld( ) { root = null; } public void insert( AnyType x ) { root = insert( x, root ); } public void remove( AnyType x ) { root = remove( x, root ); } public AnyType findMin( ) { if( isEmpty( ) ) System.out.println("tree is empty"); return findMin( root ).element; } public AnyType findMax( ) { if( isEmpty( ) ) System.out.println("tree is empty"); return findMax( root ).element; } public boolean contains( AnyType x ) { return contains( x, root ); } public void makeEmpty( ) { root = null;
  • 2. } public boolean isEmpty( ) { return root == null; } public void printTree( ) { if( isEmpty( ) ) System.out.println( "Empty tree" ); else printTree( root ); } private BinaryNode insert( AnyType x, BinaryNode t ) { if( t == null ) return new BinaryNode( x, null, null ); int compareResult = x.compareTo( t.element ); if( compareResult < 0 ) t.left = insert( x, t.left ); else if( compareResult > 0 ) t.right = insert( x, t.right ); else ; // Duplicate; do nothing return t; } private BinaryNode remove( AnyType x, BinaryNode t ) { if( t == null ) return t; // Item not found; do nothing int compareResult = x.compareTo( t.element ); if( compareResult < 0 ) t.left = remove( x, t.left );
  • 3. else if( compareResult > 0 ) t.right = remove( x, t.right ); else if( t.left != null && t.right != null ) // Two children { t.element = findMin( t.right ).element; t.right = remove( t.element, t.right ); } else t = ( t.left != null ) ? t.left : t.right; return t; } private BinaryNode findMin( BinaryNode t ) { if( t == null ) return null; else if( t.left == null ) return t; return findMin( t.left ); } private BinaryNode findMax( BinaryNode t ) { if( t != null ) while( t.right != null ) t = t.right; return t; } private boolean contains( AnyType x, BinaryNode t ) { if( t == null ) return false; int compareResult = x.compareTo( t.element ); if( compareResult < 0 ) return contains( x, t.left ); else if( compareResult > 0 )
  • 4. return contains( x, t.right ); else return true; // Match } private void printTree( BinaryNode t ) { if( t != null ) { printTree( t.left ); System.out.println( t.element ); printTree( t.right ); } } private int height( BinaryNode t ) { if( t == null ) return -1; else return 1 + Math.max( height( t.left ), height( t.right ) ); } // Basic node stored in unbalanced binary search trees private static class BinaryNode { // Constructors BinaryNode( AnyType theElement ) { this( theElement, null, null ); } BinaryNode( AnyType theElement, BinaryNode lt, BinaryNode rt ) { element = theElement; left = lt; right = rt; } AnyType element; // The data in the node
  • 5. BinaryNode left; // Left child BinaryNode right; // Right child } /** The tree root. */ private BinaryNode root; public static void main( String [ ] args ) { HelloWorld t = new HelloWorld( ); final int NUMS = 4000; final int GAP = 37; System.out.println( "Checking... (no more output means success)" ); for( int i = GAP; i != 0; i = ( i + GAP ) % NUMS ) t.insert( i ); for( int i = 1; i < NUMS; i+= 2 ) t.remove( i ); if( NUMS < 40 ) t.printTree( ); if( t.findMin( ) != 2 || t.findMax( ) != NUMS - 2 ) System.out.println( "FindMin or FindMax error!" ); for( int i = 2; i < NUMS; i+=2 ) if( !t.contains( i ) ) System.out.println( "Find error1!" ); for( int i = 1; i < NUMS; i+=2 ) { if( t.contains( i ) ) System.out.println( "Find error2!" ); } } } Solution package DataStructures; public class HelloWorld >
  • 6. { public HelloWorld( ) { root = null; } public void insert( AnyType x ) { root = insert( x, root ); } public void remove( AnyType x ) { root = remove( x, root ); } public AnyType findMin( ) { if( isEmpty( ) ) System.out.println("tree is empty"); return findMin( root ).element; } public AnyType findMax( ) { if( isEmpty( ) ) System.out.println("tree is empty"); return findMax( root ).element; } public boolean contains( AnyType x ) { return contains( x, root ); } public void makeEmpty( ) { root = null; } public boolean isEmpty( ) { return root == null;
  • 7. } public void printTree( ) { if( isEmpty( ) ) System.out.println( "Empty tree" ); else printTree( root ); } private BinaryNode insert( AnyType x, BinaryNode t ) { if( t == null ) return new BinaryNode( x, null, null ); int compareResult = x.compareTo( t.element ); if( compareResult < 0 ) t.left = insert( x, t.left ); else if( compareResult > 0 ) t.right = insert( x, t.right ); else ; // Duplicate; do nothing return t; } private BinaryNode remove( AnyType x, BinaryNode t ) { if( t == null ) return t; // Item not found; do nothing int compareResult = x.compareTo( t.element ); if( compareResult < 0 ) t.left = remove( x, t.left ); else if( compareResult > 0 ) t.right = remove( x, t.right ); else if( t.left != null && t.right != null ) // Two children {
  • 8. t.element = findMin( t.right ).element; t.right = remove( t.element, t.right ); } else t = ( t.left != null ) ? t.left : t.right; return t; } private BinaryNode findMin( BinaryNode t ) { if( t == null ) return null; else if( t.left == null ) return t; return findMin( t.left ); } private BinaryNode findMax( BinaryNode t ) { if( t != null ) while( t.right != null ) t = t.right; return t; } private boolean contains( AnyType x, BinaryNode t ) { if( t == null ) return false; int compareResult = x.compareTo( t.element ); if( compareResult < 0 ) return contains( x, t.left ); else if( compareResult > 0 ) return contains( x, t.right ); else return true; // Match }
  • 9. private void printTree( BinaryNode t ) { if( t != null ) { printTree( t.left ); System.out.println( t.element ); printTree( t.right ); } } private int height( BinaryNode t ) { if( t == null ) return -1; else return 1 + Math.max( height( t.left ), height( t.right ) ); } // Basic node stored in unbalanced binary search trees private static class BinaryNode { // Constructors BinaryNode( AnyType theElement ) { this( theElement, null, null ); } BinaryNode( AnyType theElement, BinaryNode lt, BinaryNode rt ) { element = theElement; left = lt; right = rt; } AnyType element; // The data in the node BinaryNode left; // Left child BinaryNode right; // Right child }
  • 10. /** The tree root. */ private BinaryNode root; public static void main( String [ ] args ) { HelloWorld t = new HelloWorld( ); final int NUMS = 4000; final int GAP = 37; System.out.println( "Checking... (no more output means success)" ); for( int i = GAP; i != 0; i = ( i + GAP ) % NUMS ) t.insert( i ); for( int i = 1; i < NUMS; i+= 2 ) t.remove( i ); if( NUMS < 40 ) t.printTree( ); if( t.findMin( ) != 2 || t.findMax( ) != NUMS - 2 ) System.out.println( "FindMin or FindMax error!" ); for( int i = 2; i < NUMS; i+=2 ) if( !t.contains( i ) ) System.out.println( "Find error1!" ); for( int i = 1; i < NUMS; i+=2 ) { if( t.contains( i ) ) System.out.println( "Find error2!" ); } } }