第一范文网 - 专业文章范例文档资料分享平台

学位论文-—双向循环链表的创建及相关操作的实现课程设计说明书

来源:用户分享 时间:2025/5/24 0:15:38 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

山东建筑大学计算机学院课程设计说明书

exchangeTree(rootNode); } }

private BiTNode exchangeTree(BiTNode t) { if (t != null) {

BiTNode p = t.right; t.right = t.left; t.left = p;

exchangeTree(t.right); exchangeTree(t.left); }

return t;

}

// 计算树的深度

public int depth() {

return depth(rootNode); }

private int depth(BiTNode t) { // 返回二叉树的深度 int depthleft, depthright; if (t == null) return 0;

depthleft = depth(t.left); depthright = depth(t.right);

return Math.max(depthleft, depthright) + 1; }

//横向输出树状图

public void showTree(BiTNode t,int n){ if (t==null) return; showTree(t.right,++n);

for (int i = 0; i < n; i++) System.out.print(\); System.out.print(t.data+\); showTree(t.left,n++); } }

24

山东建筑大学计算机学院课程设计说明书

3.测试函数 package kcsj;

public class Test {

public static void pln(Object o) {

System.out.println(o); }

public static void main(String[] args) {

BinaryTree bt = new BinaryTree(); Character[] charsPre = { 'a', 'b', 'd', null, null, null, 'c', 'e', null, null, 'f' };

Character[] charsPath = { 'a', 'b', 'c', 'd', null, 'e', 'f' };

pln(\先序建树:{'a','b','d',null,null,null,'c','e',null,null,'f'}\); bt.creatTree(charsPre); pln(\层序遍历结果:\); bt.pathOrder();

pln(\);

pln(\树图为(横向):\);

bt.showTree(bt.rootNode, 1); pln(\);

pln(\层序建树:{'a','b','c','d',null,'e','f'}\); bt.creatPathTree(charsPath); pln(\先序遍历结果:\); bt.preOrder(); pln(\);

pln(\树图为(横向):\);

bt.showTree(bt.rootNode, 1); pln(\);

pln(\叶子节点数:\ + bt.countLeafNode()); pln(\交换后层次遍历结果:\); bt.exchangeTree(); bt.pathOrder(); pln(\);

25

山东建筑大学计算机学院课程设计说明书

}

}

pln(\树图为(横向):\); bt.showTree(bt.rootNode, 1); pln(\);

pln(\深度为:\ + bt.depth());

五、测试数据

1、对每个函数的测试数据

利用线序遍历和层次遍历分别建树a b c d e f 2、对程序整体的测试数据 a b c d e f

六、测试情况

先序建树:{'a','b','d',null,null,null,'c','e',null,null,'f'} 层序遍历结果: a b c d e f 树图为(横向): f c e a b d

层序建树:{'a','b','c','d',null,'e','f'} 先序遍历结果: a b d c e f 树图为(横向): f c e a b d

26

山东建筑大学计算机学院课程设计说明书

叶子节点数:3

交换后层次遍历结果: a c b f e d 树图为(横向): d b a

e c f

深度为:3

27

学位论文-—双向循环链表的创建及相关操作的实现课程设计说明书.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c0wm953gngx1is530855j3blzb1bw3200hnk_7.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top