《Java语言程序设计基础篇》第10版 梁勇 著第二十五章练习题答案.docx

上传人:b****1 文档编号:1853330 上传时间:2023-05-01 格式:DOCX 页数:202 大小:62.27KB
下载 相关 举报
《Java语言程序设计基础篇》第10版 梁勇 著第二十五章练习题答案.docx_第1页
第1页 / 共202页
《Java语言程序设计基础篇》第10版 梁勇 著第二十五章练习题答案.docx_第2页
第2页 / 共202页
《Java语言程序设计基础篇》第10版 梁勇 著第二十五章练习题答案.docx_第3页
第3页 / 共202页
《Java语言程序设计基础篇》第10版 梁勇 著第二十五章练习题答案.docx_第4页
第4页 / 共202页
《Java语言程序设计基础篇》第10版 梁勇 著第二十五章练习题答案.docx_第5页
第5页 / 共202页
《Java语言程序设计基础篇》第10版 梁勇 著第二十五章练习题答案.docx_第6页
第6页 / 共202页
《Java语言程序设计基础篇》第10版 梁勇 著第二十五章练习题答案.docx_第7页
第7页 / 共202页
《Java语言程序设计基础篇》第10版 梁勇 著第二十五章练习题答案.docx_第8页
第8页 / 共202页
《Java语言程序设计基础篇》第10版 梁勇 著第二十五章练习题答案.docx_第9页
第9页 / 共202页
《Java语言程序设计基础篇》第10版 梁勇 著第二十五章练习题答案.docx_第10页
第10页 / 共202页
《Java语言程序设计基础篇》第10版 梁勇 著第二十五章练习题答案.docx_第11页
第11页 / 共202页
《Java语言程序设计基础篇》第10版 梁勇 著第二十五章练习题答案.docx_第12页
第12页 / 共202页
《Java语言程序设计基础篇》第10版 梁勇 著第二十五章练习题答案.docx_第13页
第13页 / 共202页
《Java语言程序设计基础篇》第10版 梁勇 著第二十五章练习题答案.docx_第14页
第14页 / 共202页
《Java语言程序设计基础篇》第10版 梁勇 著第二十五章练习题答案.docx_第15页
第15页 / 共202页
《Java语言程序设计基础篇》第10版 梁勇 著第二十五章练习题答案.docx_第16页
第16页 / 共202页
《Java语言程序设计基础篇》第10版 梁勇 著第二十五章练习题答案.docx_第17页
第17页 / 共202页
《Java语言程序设计基础篇》第10版 梁勇 著第二十五章练习题答案.docx_第18页
第18页 / 共202页
《Java语言程序设计基础篇》第10版 梁勇 著第二十五章练习题答案.docx_第19页
第19页 / 共202页
《Java语言程序设计基础篇》第10版 梁勇 著第二十五章练习题答案.docx_第20页
第20页 / 共202页
亲,该文档总共202页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

《Java语言程序设计基础篇》第10版 梁勇 著第二十五章练习题答案.docx

《《Java语言程序设计基础篇》第10版 梁勇 著第二十五章练习题答案.docx》由会员分享,可在线阅读,更多相关《《Java语言程序设计基础篇》第10版 梁勇 著第二十五章练习题答案.docx(202页珍藏版)》请在冰点文库上搜索。

《Java语言程序设计基础篇》第10版 梁勇 著第二十五章练习题答案.docx

《Java语言程序设计基础篇》第10版梁勇著第二十五章练习题答案

《Java语言程序设计(基础篇)》(第10版梁勇著)

第二十五章练习题答案

25.1

publicclassExercise25_01{

publicstaticvoidmain(String[]args){

newExercise25_01();

}

publicExercise25_01(){

BinaryTreetree=newBinaryTree();

System.out.print("Theheightoftreeis"+tree.height());

tree.insert("Red");

System.out.print("\nTheheightoftreeis"+tree.height());

tree.insert("Green");

System.out.print("\nTheheightoftreeis"+tree.height());

BinaryTreetree1=newBinaryTree(newString[]

{"Tom","George","Jean","Jane","Kevin","Peter","Susan",

"Jen","Kim","Michael","Michelle"});

System.out.print("\nThebreadth-firsttraversalis");

tree1.breadthFirstTraversal();

System.out.print("\nTheheightoftree1is"+tree1.height());

BinaryTreetree2=

newBinaryTree(newInteger[]{50,45,35,48,59,51,58});

System.out.print("\nThebreadth-firsttraversalfortree2is");

tree2.breadthFirstTraversal();

System.out.print("\nTheheightoftree2is"+tree2.height());

}

publicclassBinaryTree>extendsAbstractTree{

protectedTreeNoderoot;

protectedintsize=0;

/**Createadefaultbinarytree*/

publicBinaryTree(){

}

/**Createabinarytreefromanarrayofobjects*/

publicBinaryTree(E[]objects){

for(inti=0;i

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(current

returntrue;

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

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 表格模板 > 合同协议

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2