提交 c4fb5c8f 编写于 作者: ensemblelearning's avatar ensemblelearning

Add new file

上级 28481b4d
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.
先完成此消息的编辑!
想要评论请 注册