Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
ensemblelearning
备份
提交
c4fb5c8f
备
备份
项目概览
ensemblelearning
/
备份
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
备
备份
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
c4fb5c8f
编写于
10月 14, 2020
作者:
ensemblelearning
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add new file
上级
28481b4d
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
152 addition
and
0 deletion
+152
-0
Java/二叉树的顺序存储
Java/二叉树的顺序存储
+152
-0
未找到文件。
Java/二叉树的顺序存储
0 → 100644
浏览文件 @
c4fb5c8f
import java.util.Scanner;
public class BinaryTree {
final private int DEFAULT_PEEK=6;//创建使用final防止继承
private int deep;//采用private作为访问控制修饰符
private int length;
private String[] array;
public static void main(String[] args) {
BinaryTree bin=new BinaryTree();//new一个新的对象,在系统中申请空间
bin.addNode();//调用addNode方法初始化节点
System.out.println("左节点是:");
System.out.println(bin.getLeftNode(2));//调用getLeftNode方法来返回左节点
System.out.println("右节点是:");
System.out.println(bin.getRightNode(2));//调用getRightNode方法来返回右节点
System.out.println("====================");//分割线
bin.addNodeLeft(2, "insert");//调用addLeftNode方法来添加左节点
bin.addNodeRight(2, "insert");//调用addRightNode方法来添加右节点
System.out.println("插入后左节点是:");
System.out.println(bin.getLeftNode(2));//调用getLeftNode方法来返回左节点
System.out.println("====================");
System.out.println("插入后右节点是:");
System.out.println(bin.getRightNode(2));//调用getRightNode方法来返回右节点
}
/**
* 无参构造器
*/
public BinaryTree() {
this.deep=DEFAULT_PEEK;//定义deep为设定好的"6"
this.length=(int)(Math.pow(2,deep)-1);//设定节点数
array=new String[length];//存储节点数在array中
}
/**
* 传入深度参数的构造器
*/
public BinaryTree(int deep) {
this.deep=deep;
this.length=(int)(Math.pow(2,deep)-1);
array=new String[length];
}
/**
* 传入参数深度和根结点的构造器
*/
public BinaryTree(int deep,String data) {
this.deep=deep;
this.length=(int)(Math.pow(2,deep)-1);
array=new String[length];
array[1]=data;//传入根节点
}
/**
* 添加初始化结点
*/
public void addNode() {
Scanner input=new Scanner(System.in);//新建一个键盘扫描器
System.out.println("请输入二叉树信息,输入值为0结束输入");
for (int i = 1; i < array.length; i++) {
if (array[i]==null||array[i].equals("0")==true) {//判定为空则输入节点
array[i]=input.nextLine();//nextline()读取到回车结束(但是会吸收回车的数据)
if (array[i].equals("0")==true) {//判定输入为0则结束
break;
}
}
}
}
/**
* 判断二叉树是否为空
*/
public boolean isEmpaty() {
if (array[1]!=null&&array[1].equals("0")!=true) {//判定剩余的节点不为空且为0则为空树·
return false;
}else {
return true;
}
}
/**
* 返回指定结点的值
*/
public String returnData(int index) {
if (index<1||index>array.length-1) {//判断是否存在该节点
return null;
}else {
return array[index];
}
}
/**
* 返回指定结点的父节点
*/
public String getParent(int index) {
if (index<1||index>array.length-1) {//判断是否存在该节点
return null;
}else {
return array[index/2];
}
}
/**
* 添加指定结点的左节点
*/
public void addNodeLeft(int index,String data) {
if (array[2*index]!=null&&array[2*index].equals("0")!=true) {//判断左节点不为空且不为0
System.out.println("当前结点左节点已有值,是否覆盖?Y/N");
Scanner input=new Scanner(System.in);//新建一个键盘扫描器
String in=input.nextLine();//nextline()读取到回车结束(但是会吸收回车的数据)
if (in.equals("Y")) {
array[index*2]=data;//将输入数据存入该节点中
}else if (in.equals("N")) {
return;
}
}
}
/**
* 添加指定结点的右节点
*/
public void addNodeRight(int index,String data) {
if (array[2*index+1]!=null&&array[2*index+1].equals("0")!=true) {//判断右节点不为空且不为0
System.out.println("当前结点右节点已有值,是否覆盖?Y/N");
Scanner input=new Scanner(System.in);//新建一个键盘扫描器
String in=input.nextLine();//nextline()读取到回车结束(但是会吸收回车的数据)
if (in.equals("Y")) {
array[index*2+1]=data;//将输入数据存入该节点中
}else if(in.equals("N")) {
return;
}
}
}
/**
* 返回指定结点的左节点
*/
public String getLeftNode(int index) {
if (array[2*index]!=null&&array[2*index].equals("0")!=true) {//判断左节点不为空且不为0
return array[index*2];
}
else {
return null;
}
}
/**
* 返回指定结点的右节点
*/
public String getRightNode(int index) {
if (array[2*index+1]!=null&&array[2*index+1].equals("0")!=true) {//判断右节点不为空且不为0
return array[index*2+1];
}
else {
return null;
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录