insert(objects[i]);
}
/**
*Returnthedepthofthisbinarytree.Depthisthenumberofthenodesin
*thelongestpathofthetree
*/
publicintheight(){
returnheight(root);
}
privateintheight(TreeNoderoot){
if(root==null){
return0;
}else{
return1+Math.max(height(root.left),height(root.right));
}
}
/**Displaysthenodesinbreadth-firsttraversal*/
publicvoidbreadthFirstTraversal(){
java.util.LinkedList>queue=
newjava.util.LinkedList>();
if(root==null)
return;
queue.add(root);
while(!
queue.isEmpty()){
TreeNodenode=queue.removeFirst();
System.out.print(node.element+"");
if(node.left!
=null)
queue.add(node.left);
if(node.right!
=null)
queue.add(node.right);
}
}
/**Returnstrueiftheelementisinthetree*/
publicbooleansearch(Ee){
TreeNodecurrent=root;//Startfromtheroot
while(current!
=null){
if(pareTo(current.element)<0){
current=current.left;
}elseif(pareTo(current.element)>0){
current=current.right;
}else
//elementmatchescurrent.element
returntrue;//Elementisfound
}
returnfalse;
}
/**
*InsertelementointothebinarytreeReturntrueiftheelementis
*insertedsuccessfully
*/
publicbooleaninsert(Ee){
if(root==null)
root=createNewNode(e);//Createanewroot
else{
//Locatetheparentnode
TreeNodeparent=null;
TreeNodecurrent=root;
while(current!
=null)
if(pareTo(current.element)<0){
parent=current;
current=current.left;
}elseif(pareTo(current.element)>0){
parent=current;
current=current.right;
}else
returnfalse;//Duplicatenodenotinserted
//Createthenewnodeandattachittotheparentnode
if(pareTo(parent.element)<0)
parent.left=createNewNode(e);
else
parent.right=createNewNode(e);
}
size++;
returntrue;//Elementinserted
}
protectedTreeNodecreateNewNode(Ee){
returnnewTreeNode(e);
}
/**Inordertraversalfromtheroot*/
publicvoidinorder(){
inorder(root);
}
/**Inordertraversalfromasubtree*/
protectedvoidinorder(TreeNoderoot){
if(root==null)
return;
inorder(root.left);
System.out.print(root.element+"");
inorder(root.right);
}
/**Postordertraversalfromtheroot*/
publicvoidpostorder(){
postorder(root);
}
/**Postordertraversalfromasubtree*/
protectedvoidpostorder(TreeNoderoot){
if(root==null)
return;
postorder(root.left);
postorder(root.right);
System.out.print(root.element+"");
}
/**Preordertraversalfromtheroot*/
publicvoidpreorder(){
preorder(root);
}
/**Preordertraversalfromasubtree*/
protectedvoidpreorder(TreeNoderoot){
if(root==null)
return;
System.out.print(root.element+"");
preorder(root.left);
preorder(root.right);
}
/**Innerclasstreenode*/
publicclassTreeNode>{
Eelement;
TreeNodeleft;
TreeNoderight;
publicTreeNode(Ee){
element=e;
}
}
/**Getthenumberofnodesinthetree*/
publicintgetSize(){
returnsize;
}
/**Returnstherootofthetree*/
publicTreeNodegetRoot(){
returnroot;
}
/**Returnsapathfromtherootleadingtothespecifiedelement*/
publicjava.util.ArrayList>path(Ee){
java.util.ArrayList>list=newjava.util.ArrayList>();
TreeNodecurrent=root;//Startfromtheroot
while(current!
=null){
list.add(current);//Addthenodetothelist
if(pareTo(current.element)<0){
current=current.left;
}elseif(pareTo(current.element)>0){
current=current.right;
}else
break;
}
returnlist;//Returnanarrayofnodes
}
/**
*Deleteanelementfromthebinarytree.Returntrueiftheelementis
*deletedsuccessfullyReturnfalseiftheelementisnotinthetree
*/
publicbooleandelete(Ee){
//Locatethenodetobedeletedandalsolocateitsparentnode
TreeNodeparent=null;
TreeNodecurrent=root;
while(current!
=null){
if(pareTo(current.element)<0){
parent=current;
current=current.left;
}elseif(pareTo(current.element)>0){
parent=current;
current=current.right;
}else
break;//Elementisinthetreepointedbycurrent
}
if(current==null)
returnfalse;//Elementisnotinthetree
//Case1:
currenthasnoleftchildren
if(current.left==null){
//Connecttheparentwiththerightchildofthecurrentnode
if(parent==null){
root=current.right;
}else{
if(pareTo(parent.element)<0)
parent.left=current.right;
else
parent.right=current.right;
}
}else{
//Case2:
Thecurrentnodehasaleftchild
//Locatetherightmostnodeintheleftsubtreeof
//thecurrentnodeandalsoitsparent
TreeNodeparentOfRightMost=current;
TreeNoderightMost=current.left;
while(rightMost.right!
=null){
parentOfRightMost=rightMost;
rightMost=rightMost.right;//Keepgoingtotheright
}
//ReplacetheelementincurrentbytheelementinrightMost
current.element=rightMost.element;
//Eliminaterightmostnode
if(parentOfRightMost.right==rightMost)
parentOfRightMost.right=rightMost.left;
else
//Specialcase:
parentOfRightMost==current
parentOfRightMost.left=rightMost.left;
}
size--;
returntrue;//Elementinserted
}
/**Obtainaniterator.Useinorder.*/
publicjava.util.Iteratoriterator(){
returninorderIterator();
}
/**Obtainaninorderiterator*/
publicjava.util.IteratorinorderIterator(){
returnnewInorderIterator();
}
//InnerclassInorderIterator
classInorderIteratorimplementsjava.util.Iterator{
//Storetheelementsinalist
privatejava.util.ArrayListlist=newjava.util.ArrayList();
privateintcurrent=0;//Pointtothecurrentelementinlist
publicInorderIterator(){
inorder();//Traversebinarytreeandstoreelementsinlist
}
/**Inordertraversalfromtheroot*/
privatevoidinorder(){
inorder(root);
}
/**Inordertraversalfromasubtree*/
privatevoidinorder(TreeNoderoot){
if(root==null)
return;
inorder(root.left);
list.add(root.element);
inorder(root.right);
}
/**Nextelementfortraversing?
*/
publicbooleanhasNext(){
if(currentreturntrue;
returnfalse;
}
/**Getthecurrentelementandmovecursortothenext*/
publicObjectnext(){
returnlist.get(current++);
}
/**Removethecurrentelementandrefreshthelist*/
publicvoidremove(){
delete(list.get(current));//Deletethecurrentelement
list.clear();//Clearthelist
inorder();//Rebuildthelist
}
}
/**Removeallelementsfromthetree*/
publicvoidclear(){
root=null;
size=0;
}
}
publicinterfaceTree>{
/**Returntrueiftheelementisinthetree*/
publicbooleansearch(Ee);
/**
*InsertelementointothebinarytreeReturntrueiftheelementis
*insertedsuccessfully
*/
publicbooleaninsert(Ee);
/**
*DeletethespecifiedelementfromthetreeReturntrueiftheelementis
*deletedsuccessfully
*/
publicbooleandelete(Ee);
/**Inordertraversalfromtheroot*/
publicvoidinorder();
/**Postordertraversalfromtheroot*/
publicvoidpostorder();
/**Preordertraversalfromtheroot*/
publicvoidpreorder();
/**Getthenumberofnodesinthetree*/
publicintgetSize();
/**Returntrueifthetreeisempty*/
publicbooleanisEmpty();
/**Returnaniteratortotraverseelementsinthetree*/
publicjava.util.Iteratoriterator();
}
publicabstractclassAbstractTree>implements
Tree{
/**Inordertraversalfromtheroot*/
publicvoidinorder(){
}
/**Postordertraversalfromtheroot*/
publicvoidpostorder(){
}
/**Preordertraversalfromtheroot*/
publicvoidpreorder(){
}
/**Returntrueifthetreeisempty*/
publicbooleanisEmpty(){
returngetSize()==0;
}
/**Returnaniteratortotraverseelementsinthetree*/
publicjava.util.Iteratoriterator(){
returnnull;
}
}
}
25.1附加
importjavafx.application.Application;
importjavafx.geometry.Pos;
importjavafx.s