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

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

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

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

return data; }

public double getWight() { return weight; }

@Override

public int compareTo(BiTNode o) { if (o.getWight() > this.getWight()) return 1;

if (o.getWight() < this.getWight()) return -1; return 0; } }

2.BinaryTree()构造 package kcsj;

import java.util.LinkedList; import java.util.Queue;

public class BinaryTree> { AnyType[] pre, in;

BiTNode rootNode = new BiTNode(); int count = 0;

public BinaryTree() { rootNode = null; }

public BinaryTree(AnyType rootNodeItem) { rootNode.data = rootNodeItem;

rootNode.left = rootNode.right = null; }

public BinaryTree(BiTNode t) { rootNode = t; }

//1. 先序遍历建树

20

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

public BiTNode creatTree(AnyType[] a) { return rootNode = creatBinaryTree(a); }

private BiTNode creatBinaryTree(AnyType[] a) { BiTNode p = null; if (count < a.length) {

AnyType data = a[count]; count++;

if (data != null) {

p = new BiTNode((AnyType) data); p.left = creatTree(a); p.right = creatTree(a); } }

return p;

}

//1.层次遍历排序建树

public void creatPathTree(AnyType[] a) { if (a != null) {

creatPathBinaryTree(a);

} }

private void creatPathBinaryTree(AnyType[] a) {

Queue> q = new LinkedList>(); BiTNode node = new BiTNode(a[0]); rootNode = node; q.offer(node); int i = 1;

while (i < a.length) {

if (a[i] != null) {

node = new BiTNode(a[i]); q.element().left = node; q.offer(q.element().left); }

if (i < a.length - 1) {

if (a[++i] != null) {

21

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

}

}

}

node = new BiTNode(a[i]); q.element().right = node; q.offer(q.element().right); }

q.poll(); i++;

//2.实现二叉树的层次遍历 public void pathOrder() { if (rootNode != null) pathOrder(rootNode); }

public void pathOrder(BiTNode t) {

Queue> q = new LinkedList>(); q.offer(t);

while (!q.isEmpty()) {

if (q.element().left != null) q.offer(q.element().left); if (q.element().right != null) q.offer(q.element().right);

System.out.print(q.poll().data + \);

}

}

// 先序遍历

public void preOrder() { if (rootNode != null) preOrder(rootNode); }

private void preOrder(BiTNode t) { if (t != null) {

System.out.print(t.data+\);

22

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

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

}

// 统计节点的个数

public int countNode() {

return countNode(rootNode); }

private int countNode(BiTNode t) { int m, n;

if (t == null) return 0;

m = countNode(t.left); n = countNode(t.right); return m + n + 1; }

// 3.统计叶子节点个数(递归) public int countLeafNode() {

return countLeafNode(rootNode); }

private int countLeafNode(BiTNode t) { int m = 0, n = 0; if (t == null) return 0;

if (t.left == null && t.right == null) { return 1; }

m = countLeafNode(t.left); n = countLeafNode(t.right); return m + n; }

// 4.将二叉树左+右子树相互交换(递归) public void exchangeTree() { if (rootNode != null) {

23

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