System.out.println("输入第"+i+"个数据");
try{
data_array[i]=Integer.parseInt(bin.readLine());
}catch(NumberFormatExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
Shuangshuang=newShuang(data_array);
//shuang.nizhi();
shuang.add(9,0);
//shuang.delete();
shuang.print();
}
}
五、测试数据
1、添加函数的测试
2、删除函数的测试
3、逆置函数的测试
六、测试情况
添加函数的测试
需要输入数据的个数:
5
输入第0个数据
1
输入第1个数据
2
输入第2个数据
3
输入第3个数据
4
输入第4个数据
5
现在将9添加到0号位置
现在进行遍历:
9
1
2
3
4
删除函数的测试
需要输入数据的个数:
5
输入第0个数据
1
输入第1个数据
2
输入第2个数据
3
输入第3个数据
4
输入第4个数据
5
现在删除最后一个数
现在进行遍历:
1
2
3
4
逆置函数的测试
需要输入数据的个数:
5
输入第0个数据
1
输入第1个数据
2
输入第2个数据
3
输入第3个数据
4
输入第4个数据
5
现在进行遍历:
1
2
3
4
5
现在进行逆置:
现在进行遍历:
5
4
3
2
1
树的创建及相关操作的实现
一、问题描述
二、数据结构
针对所处理的树:
1、存储结构
2、使用所选用语言的功能,实现上述的该存储结构
packagetree;
publicclassTreeNode{//树的节点的定义
TreeNodeleftch,rigthch;
intelement;
//构造方法
publicTreeNode(){
//TODOAuto-generatedconstructorstub
}
publicTreeNode(intelement,TreeNodeleft,TreeNoderight){
//TODOAuto-generatedconstructorstub
leftch=left;
rigthch=right;
this.element=element;
}
publicTreeNode(intelement){
this.element=element;
leftch=rigthch=null;
}
}
三、逻辑设计
1、总体思路
先找出有用的数据存储结构-->编写相关方法-->完成对数据的操作
2、模块划分(以图示的方法给出各个函数的调用关系)
四、编码
packagetree;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.util.ArrayList;
importjava.util.LinkedList;
publicclassTwoTree{
TreeNoderootNode;
publicTwoTree(){
//TODOAuto-generatedconstructorstub
this.rootNode=newTreeNode();
}
publicTwoTree(TreeNoderoot){
//TODOAuto-generatedconstructorstub
rootNode=root;
}
//1、利用先序遍历和中序遍历的结果建立二叉树
publicTreeNodecreatTree(ArrayListpreOrder,
ArrayListmidOrder){
TreeNoderoot=null;
if(preOrder.size()!
=0){
Integerx=preOrder.get(0);
root=newTreeNode(x);
inti=midOrder.indexOf(x);
ArrayListlist1=newArrayList();
ArrayListlist2=newArrayList();
ArrayListlist3=newArrayList();
ArrayListlist4=newArrayList();
for(intj=1;j
list1.add(preOrder.get(j));
}
for(intj=0;j
list3.add(midOrder.get(j));
}
for(intj=i+1;jlist2.add(preOrder.get(j));
}
for(intj=i+1;jlist4.add(midOrder.get(j));
}
root.leftch=creatTree(list1,list3);
root.rigthch=creatTree(list2,list4);
}
returnroot;
}
//2、实现二叉树的层次遍历
publicvoidcengOrder(){
if(rootNode!
=null){
ArrayListqueue=newArrayList();//队列
queue.add(rootNode);
while(queue.size()!
=0){
TreeNodehead=queue.remove(0);
System.out.println(""+head.element);
if(head.leftch!
=null){
queue.add(head.leftch);
}
if(head.rigthch!
=null){
queue.add(head.rigthch);
}
}
}
}
publicvoidpreorder(TreeNoderoot){
if(root!
=null){
System.out.println(""+root.element);
preorder(root.leftch);
preorder(root.rigthch);
}
}
//3、统计二叉树叶子结点的个数(递归)。
publicintcountLeafNode(TreeNoderoot){
intm,n;
if(root!
=null){
if(root.leftch==null&&root.rigthch==null){
return1;
}else{
m=countLeafNode(root.leftch);
n=countLeafNode(root.rigthch);
returnm+n;
}
}
return0;
}
//4、将二叉树左右子树相互交换(递归)
publicvoidchangChild(TreeNoderoot){
TreeNodeflag;
if(root!
=null){
flag=root.leftch;
root.leftch=root.rigthch;
root.rigthch=flag;
changChild(root.leftch);
changChild(root.rigthch);
}
}
//判断是不是完全二叉树
publicbooleanisCompelete(TreeNoderoot){
LinkedListqueue=newLinkedList();
TreeNodep;
if(root!
=null){
queue.push(root);
p=queue.pop();
while(p.leftch!
=null&&p.rigthch!
=null){
queue.push(p.leftch);
queue.push(p.rigthch);
p=queue.pop();
}
if(!
(p.leftch==null&&p.rigthch!
=null)){
if(p.leftch!
=null){
queue.push(p.leftch);
p=queue.pop();
}
while(queue!
=null){
if(!
(p.leftch==null&&p.rigthch==null)){
returnfalse;
}
p=queue.pop();
}
returntrue;
}
returnfalse;
}
returntrue;
}
//从键盘获取数组创建树
publicArrayListget_array(){
BufferedReaderbin=newBufferedReader(
newInputStreamReader(System.in));
intlength=0;
System.out.println("需要输入数据的个数:
");
try{
length=Integer.parseInt(bin.readLine());
}catch(NumberFormatExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();