SlideShare a Scribd company logo
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

Binary Tree
Binary  TreeBinary  Tree
Binary Tree
Vishal Gaur
 
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
illyasraja7
 
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
allystraders
 
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
fathimafancyjeweller
 
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
farrahkur54
 
( 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
aristogifts99
 
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
arjuncp10
 
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
allurafashions98
 
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
MARRY7
 
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
anukoolelectronics
 
week-14x
week-14xweek-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
akaluza07
 
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
BTI360
 
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
manjan6
 
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
arjuncorner565
 
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
info750646
 
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
fashionbigchennai
 
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
ajoy21
 
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
arbaazrabs
 
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
feetshoemart
 

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.pdf
apleathers
 
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
apleathers
 
WaterSolutionWater.pdf
WaterSolutionWater.pdfWaterSolutionWater.pdf
WaterSolutionWater.pdf
apleathers
 
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
apleathers
 
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
apleathers
 
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
apleathers
 
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
apleathers
 
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
apleathers
 
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
apleathers
 
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
apleathers
 
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
apleathers
 
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
apleathers
 
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
apleathers
 
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
apleathers
 
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
apleathers
 
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
apleathers
 
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
apleathers
 
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
apleathers
 
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
apleathers
 
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
apleathers
 

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

How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
Kavitha Krishnan
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 

Recently uploaded (20)

How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 

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!" ); } } }