二叉树基本操作与二叉排序树.docx
- 文档编号:24175941
- 上传时间:2023-05-25
- 格式:DOCX
- 页数:20
- 大小:154.22KB
二叉树基本操作与二叉排序树.docx
《二叉树基本操作与二叉排序树.docx》由会员分享,可在线阅读,更多相关《二叉树基本操作与二叉排序树.docx(20页珍藏版)》请在冰豆网上搜索。
二叉树基本操作与二叉排序树
《数据结构》课程设计
实验报告
题目二叉树与二叉排序树的建立及其应用
学院商学院
专业信息系统与信息管理
班级信息101
学号201052275115
学生姓名徐鸽
同组成员王超男何艳李梦雪关冬张卫芳韦露莎
指导教师刘小晶
编写日期2012年6月27日
一、问题描述:
1.二叉树的操作及应用
在此次课程设计中实现二叉树的建立,建立二叉树以后对该二叉树进行先根遍历、中根遍历、后根遍历操作。
然后再应用这些遍历操作来实现二叉树的查找操作,并且根据两种遍历操作来判断两棵树是否相等。
2.二叉排序树的有关操作
根据二叉排序树的结构特征建立二叉排序树。
并且在已有二叉排序树的基础上,根据二叉排序树的特点,对其进行查找操作、插入操作、删除操作。
二、问题分析:
问题为:
编程实现根据二叉树的先序遍历序列和中序遍历序列来建立两颗二叉树,并判断这两颗二叉树是否相等。
在整个课程设计中,我主要是负责二叉树相关应用内容里的建立两颗二叉树的工作。
(1)首先取先跟遍历序列中的第一个字符作为根结点的数据域值建立根结点;
(2)在中根遍历序列中查找确定这个根结点在中根遍历序列中的位置,由此得到在根结点左边的序列即为此根结点左子树的中根遍历序列,而右边的序列即为此根结点右子树的中根遍历序列。
(3)根据左、右子树的中根遍历序列中的字符个数再在先序遍历序列中确定左、右子树的先序遍历序列。
(4)根据
(2)(3)确定的左、右子树的先根和中根遍历序列采用递归调用的方法建立根结点的左、右子树。
要实现上述建立的二叉树的步骤,需要引入5个参数:
preOrder,inOrder,preIndex,inIndex和count。
其中:
参数preOrder是整棵树的先跟遍历序列;inPrder是整棵树的中根遍历序列;preIndex是先跟遍历序列在preOrder中的开始位置:
inIdex是中根遍历序列在inOrder中的开始位置;count表示树种结点的个数。
三、数据结构描述:
二叉链式存储结构
PublicclassBiTree{
PrivateBiTreeNoderoot;//树的根结点
PublicBiTree(){//构造一棵空树
This.root=null;
}
PublicBiTree(BiTreeNoderoot){//构造一棵树
This.root=root;
}
四、算法设计:
1.算法流程图:
2.具体算法:
PublicBiTree(Stringpreorder,StringinOrder,intpreIndex,intinIndex,intcount){
If(count>0){//先根和中根非空
Charr=preorder.charAt(preIndex);
//取先根字符串中的第一个元素作为根节点
intI=0;
for(;i //寻找根结点在中根遍历字符串中的索引 if(r==inOrder.charAt(i+inIndex)) break; root=newBiTreeNode(r);//建立树的根结点 root.setLchild(newBiTree(preorder,inOrder,preIndex+1.index,i).root); //建立树的左子树 root.setRchild(newBiTree(preorder,inOrder,preIndex+i+1,inIndex+i+1,count-i-1).root); //建立树的右子树 } } 五、详细程序清单: (1)二叉树 importjava.awt.*; importjavax.swing.*; classInterfaceextendsJFrame{ TextAreata=newTextArea(); publicInterface(){ setLayout(newBorderLayout()); add(ta); } } 二叉树节点 classBiTreeNode1{ privateObjectdata; privateBiTreeNode1lchild,rchild,root1; publicBiTreeNode1(){ this(null); } publicBiTreeNode1(Objectdata){ this(data,null,null); } publicBiTreeNode1(Objectdata,BiTreeNode1lchild,BiTreeNode1rchild){ this.data=data; this.lchild=lchild; this.rchild=rchild; } publicObjectgetData(){ returndata; } publicvoidsetData(Objectdata){ this.data=data; } publicBiTreeNode1root1(){ returnroot1; } publicBiTreeNode1getLchild(){ returnlchild; } publicvoidsetLchild(BiTreeNode1lchild){ this.lchild=lchild; } publicBiTreeNode1getRchild(){ returnrchild; } publicvoidsetRchild(BiTreeNode1rchild){ this.rchild=rchild; } } 二叉树测试类 importjavax.swing.*; publicclassBSTree1{ privateBiTreeNode1root; publicBSTree1(StringpreOrder,StringinOrder,intpreIndex,intinIndex, intcount){ if(count>0){ charr=preOrder.charAt(preIndex); inti=0; for(;i if(r==inOrder.charAt(i+inIndex)) break; root=newBiTreeNode1(r); root.setLchild(newBSTree1(preOrder,inOrder,preIndex+1,inIndex, i).root); root.setRchild(newBSTree1(preOrder,inOrder,preIndex+i+1, inIndex+i+1,count-i-1).root); } } booleanisEqual(BiTreeNode1T1,BiTreeNode1T2){ if(T1==null&&T2==null) returntrue; if(T1! =null&&T2! =null) if(T1.getData().equals(T2.getData())) if(isEqual(T1.getLchild(),T2.getLchild())) if(isEqual(T1.getRchild(),T2.getRchild())) returntrue; returnfalse; } publicstaticvoidmain(String[]args){ StringBuffersb=newStringBuffer(); Stringresult=""; Interfaceinterf=newInterface(); interf.setTitle("二叉树"); interf.setSize(500,300); interf.setVisible(true); StringpreOrder=JOptionPane.showInputDialog(null,"请输入建立二叉树的节点: ","输入",JOptionPane.QUESTION_MESSAGE); StringinOrder=JOptionPane.showInputDialog(null,"请输入二叉树节点的顺序(后根遍历): ","输入",JOptionPane.QUESTION_MESSAGE); sb.append("输入建立的二叉树节点是: ").append(preOrder).append("\n").append("输入二叉树节点的顺序(后根遍历)是: ").append(inOrder).append("\n"); BSTree1T1=newBSTree1(preOrder,inOrder,0,0,preOrder.length()); sb.append("树T1创建成功! ").append("\n"); StringpreOrder1=JOptionPane.showInputDialog(null,"请输入建立二叉树的节点: ","输入",JOptionPane.QUESTION_MESSAGE); StringinOrder1=JOptionPane.showInputDialog(null,"请输入二叉树节点的顺序(后根遍历): ","输入",JOptionPane.QUESTION_MESSAGE); sb.append("输入建立的二叉树节点是: ").append(preOrder1).append("\n").append("输入二叉树节点的顺序(后根遍历)是: ").append(inOrder1).append("\n"); BSTree1T2=newBSTree1(preOrder1,inOrder1,0,0,preOrder1.length()); sb.append("树T2创建成功! ").append("\n"); sb.append("T1左节点的右节点是: ").append(T1.root.getLchild().getRchild().getData()).append("\n"); sb.append("T1右节点是: ").append(T1.root.getRchild().getData()).append("\n"); sb.append("T2左节是: ").append(T2.root.getLchild().getData()).append("\n"); sb.append("T2右节点是: ").append(T2.root.getRchild().getData()).append("\n"); if(T1.isEqual(T1.root,T2.root)==true) sb.append("T1和T2的关系是: ").append("T1==T2"); else sb.append("T1和T2的关系是: ").append("T1! =T2"); result=sb.toString(); interf.ta.setText(result); interf.ta.setVisible(true); } } (2)二叉排序树节点 classBiTreeNode2{ privateintkey; privateBiTreeNode2lchild; BiTreeNode2rchild; publicBiTreeNode2(){ this(0,null,null); } publicBiTreeNode2(intkey){ this(key,null,null); } publicBiTreeNode2(intkey,BiTreeNode2lchild,BiTreeNode2rchild){ this.key=key; this.lchild=lchild; this.rchild=rchild; } publicintgetkey(){ returnkey; } publicBiTreeNode2getLchild(){ returnlchild; } publicBiTreeNode2getRchild(){ returnrchild; } publicvoidsetKey(intkey){ this.key=key; } publicvoidsetLchild(BiTreeNode2lchild){ this.lchild=lchild; } publicvoidsetRchild(BiTreeNode2rchild){ this.rchild=rchild; } } 二叉排序树测试类 importjava.util.*; importjavax.swing.*; publicclassBSTree2{ protectedBiTreeNode2root; privateStringBufferinsert=newStringBuffer(); publicBSTree2(){ root=null; } publicBSTree2(BiTreeNode2root){ this.root=root; } publicBiTreeNode2getRoot(){ returnroot; } publicvoidsetRoot(BiTreeNode2root){ this.root=root; } publicStringBufferinOrderTraverse(BiTreeNode2p){ if(p! =null){ inOrderTraverse(p.getLchild()); insert.append(p.getkey()).append(""); inOrderTraverse(p.getRchild()); } returninsert; } publicBiTreeNode2searchBST(intkey){ returnsearchBST(root,key); } //二叉树上的递归查找算法 privateBiTreeNode2searchBST(BiTreeNode2p,intkey){ if(p! =null){ if(key==p.getkey()){//查找成功 returnp; }//System.out.print(((RecordNode)p.getData()).getKey()+"? "); elseif(key returnsearchBST(p.getLchild(),key);//在左子树中查找 }else{ returnsearchBST(p.getRchild(),key);//在右子树中查找 } } returnnull; } publicbooleaninsertBST(intkey){ if(root==null){ root=newBiTreeNode2(key); returntrue; } returninsertBST(root,key); } privatebooleaninsertBST(BiTreeNode2p,intkey){ if(key==p.getkey()){ returnfalse; } if(key if(p.getLchild()==null){ p.setLchild(newBiTreeNode2(key)); returntrue; }else{ returninsertBST(p.getLchild(),key); } }elseif(p.getRchild()==null){ p.setRchild(newBiTreeNode2(key)); returntrue; }else{ returninsertBST(p.getRchild(),key); } } publicBiTreeNode2removeBST(intkey){ returnremoveBST(root,key,null); } privateBiTreeNode2removeBST(BiTreeNode2p,intkey,BiTreeNode2parent){ if(p! =null){ if(key returnremoveBST(p.getLchild(),key,p); }elseif(key>p.getkey()){ returnremoveBST(p.getRchild(),key,p); }elseif(p.getLchild()! =null&&p.getRchild()! =null){ BiTreeNode2innext=p.getRchild(); while(innext.getLchild()! =null){ innext=innext.getLchild(); } p.setKey(innext.getkey()); returnremoveBST(p.getRchild(),p.getkey(),p); }else{ if(parent==null){ if(p.getLchild()! =null){ root=p.getLchild(); }else{ root=p.getRchild(); } returnp; } if(p==parent.getLchild()){ if(p.getLchild()! =null){ parent.setLchild(p.getLchild()); }else{ parent.setLchild(p.getRchild()); } }elseif(p.getLchild()! =null){ parent.setRchild(p.getLchild()); }else{ parent.setRchild(p.getRchild()); } returnp; } } returnnull; } publicstaticvoidmain(Stringargs[]){ Interfaceinterf=newInterface(); interf.setTitle("二叉树"); interf.setSize(500,300); interf.setVisible(true); intinput; StringBuffersb=newStringBuffer(); Stringresult=""; BSTree2bstree=newBSTree2(); input=Integer.parseInt(JOptionPane.showInputDialog(null,"请输入二叉排序树的结点个数","输入",JOptionPane.QUESTION_MESSAGE)); sb.append("输入的二叉排序树结点个数为: ").append(input).append("\n"); StringBufferstr=newStringBuffer(); intkey; for(inti=1;i<=input;i++){//输入关键字序列 key=Integer.parseInt(JOptionPane.showInputDialog(null,"请输入结点的关键字序列: ","输入",JOptionPane.QUESTION_MESSAGE)); bstree.insertBST(key); str.append(key).append(""); } sb.append("输入的结点关键字序列为: ").append(str).append("\n"); StringBufferstr1=bstree.inOrderTraverse(bstree.getRoot()); sb.append("创建的二叉排序树的序遍历序列为: ").append(str1).append("\n"); bstree.insert.delete(0,bstree.insert.length()); input=Integer.parseInt(JOptionPane.showInputDialog(null,"请输入待查找的关键字: ","输入",JOptionPane.QUESTION_MESSAGE)); BiTreeNode2found=bstree.searchBST(input); sb.append("输入的待查找关键字是: ").append(input).append("\n"); if(found! =null){ sb.append("查找成功! ").append("\n"); }else{ sb.append("查找失败! ").append("\n"); } input=Integer.parseInt(JOptionPane.showInputDialog(null,"请输入待删除的关键字: ","输入",JOptionPane.QUESTION_MESSAGE)); sb.append("输入的待删除关键字是: ").append(input).append("\n"); BiTreeNode2found1=bstree.removeBST(input); if(found1! =null){ sb.append("删除成功! ").append("\n");
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 二叉 基本 操作 二叉排序树