顺序二叉树 5.3 KB
Newer Older
ensemblelearning's avatar
ensemblelearning 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
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;
        }
    }
}