[Java]自定义实现BinaryTree(二叉树)和其接口有,并实现各种遍历方法及查询结点方法

BinaryTree接口

package MyTree;

public interface BinaryTree {
    //判断是否为空树
    public Boolean isEmpty();

    //树结点数量
    public int size();

    //获取树的高度
    public int getHeight();
    public int getHeight(Node root);


    //先序遍历
    public void preOrderTraverse();
    public void preOrderTraverse(Node root);

    //中序遍历
    public void inOrderTraverse();
    public void inOrderTraverse(Node root);

    //后序遍历
    public void postOrderTraverse();
    public void postOrderTraverse(Node root);

    //寻找某个值
    public Node findKey(int value);

    //层次遍历
    public void levelOrderByStack();

    //借助栈进行中序遍历
    public void inOrderByStack();



}

定义树节点

package MyTree;

public class Node {
    Object value;
    Node leftChild;
    Node rightChild;

    public Node(Object value){
        super();
        this.value=value;
    }

    public Node(Object value, Node leftChild, Node rightChild) {
        this.value = value;
        this.leftChild = leftChild;
        this.rightChild = rightChild;

    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                ", leftChild=" + leftChild +
                ", rightChild=" + rightChild +
                '}';
    }
}

BinaryTree的各种自定义遍历方法及查询方法

package MyTree;

import java.util.*;

public class LinkedBinaryTree implements BinaryTree {
    public Node root;
    private int size;

    public LinkedBinaryTree(Node root) {
        this.root = root;
    }

    @Override
    public Boolean isEmpty() {
        return root == null;
    }

    @Override
    public int size() {
        System.out.println("二叉树的节点个数:");
        return this.size(root);
    }
    public int size(Node root){
        if(root!=null){
            int nl=this.size(root.leftChild);


            int nr=this.size(root.rightChild);


            return nl+nr+1;

        }else{
            return 0;

        }

    }

    @Override
    public int getHeight() {
        System.out.println("二叉树的高度:");
        return this.getHeight(root);

    }
    public int getHeight(Node root){
        if(root!=null){
            //获取左子树的高度
          int nl=this.getHeight(root.leftChild);

            //获取右子树的高度
          int nr=this.getHeight(root.rightChild);

            //返回左子树,右子树的高度加1
          return nl>nr?nl+1:nr+1;

        }else{

            return 0;
        }



    }



    public void preOrderTraverse(){
        System.out.println("先序遍历");
        this.preOrderTraverse(this.root);
        System.out.println();

    }
    @Override
    public void preOrderTraverse(Node root) {
        /*
        * 1.输出根节点的值
        * 2.对左子树进行先序遍历
        * 3.对右子树进行先序遍历
        * */
        if(root!=null) {
            System.out.print(root.value+"  ");


            this.preOrderTraverse(root.leftChild);

          this.preOrderTraverse(root.rightChild);

        }
    }

    @Override
    public void inOrderTraverse() {
        System.out.println("中序遍历");
        this.inOrderTraverse(this.root);
        System.out.println();

    }


    public void inOrderTraverse(Node root) {
        if(root!=null){
            this.inOrderTraverse(root.leftChild);
            System.out.print(root.value+"   ");
            this.inOrderTraverse(root.rightChild);

        }

    }

    @Override
    public void postOrderTraverse() {
        System.out.println("后序遍历");
        this.postOrderTraverse(this.root);
        System.out.println();

    }

    @Override
    public void postOrderTraverse(Node root) {
        if(root!=null){
            this.postOrderTraverse(root.leftChild);
            this.postOrderTraverse(root.rightChild);
            System.out.print(root.value+"   ");
        }

    }

    public Node findKey(int value){

        return this.findKey(value,root);
    }

    @Override
    public void levelOrderByStack() {
        System.out.println("按照层次遍历二叉树");
        if(root == null){
            return;
        }
        Queue<Node> queue=new LinkedList<Node>();//队列
        queue.add(root);
        while(queue.size()!=0){
            int len=queue.size();
            for(int i=0;i<len;i++){
                Node temp=queue.poll();
                System.out.print(temp.value+"  ");
                if(temp.leftChild!=null){
                    queue.add(temp.leftChild);

                }
                if(temp.rightChild!=null){
                    queue.add(temp.rightChild);
                }
            }

        }

        System.out.println();
    }

    public Node findKey(Object value, Node root){
        //结点为空 可能是整个树的根节点 也可能是递归调用中叶子结点的左孩子和有孩子
        if(root ==null){
            return null;
        }else if(root!=null&&root.value==value){//找到了 结束条件1
            return root;
        }else {
            //递归调用
            Node node1=this.findKey(value,root.leftChild);
            Node node2=this.findKey(value,root.rightChild);
            if(node1!=null&& node1.value==value ){

                return node1;
            }else if(node2!=null&& node2.value==value) {

                return node2;
            }  else {
                return null;
            }
        }
    }

      //栈 后进的先出
    public void inOrderByStack(){
        System.out.println("中序非递归遍历");
        Deque<Node> stack=new LinkedList<Node>();//栈
        Node current=root;
        while(current!=null||!stack.isEmpty()){
            while(current!=null){
                stack.push(current);
                current=current.leftChild;
            }
            if(!stack.isEmpty()){
                current=stack.pop();
                System.out.print(current.value+"  ");
                current=current.rightChild;
            }
        }
        System.out.println();
    }


}

测试BinaryTree

package MyTree;

import java.util.function.BinaryOperator;


public class Test {
    public static void main(String[] args) {
        //创建一个二叉树
        Node node5=new Node(5,null,null);
        Node node4=new Node(4,null,node5);
        Node node7=new Node(7,null,null);
        Node node6=new Node(6,null,node7);
        Node node3=new Node(3,null,null);
        Node node2=new Node(2,node3,node6);
        Node node1=new Node(1,node4,node2);

        BinaryTree btree=new LinkedBinaryTree(node1);






        //判断二叉树是否为空
        System.out.println(btree.isEmpty());



        //先序遍历递归 1 4 5 2 3 6 7
        btree.preOrderTraverse();




        //中序遍历递归4 5 1 3 2 6 7
        btree.inOrderTraverse();



        //后序遍历递归5 4 3 7 9 2 1
        btree.postOrderTraverse();



        //中序遍历非递归(借助栈)4 5 1 3 2 6 7
        btree.inOrderByStack();



        //层序遍历递归(借助队列) 1 4 2 5 3 6 7
        btree.levelOrderByStack();


        //在二叉树中查找某个值
        System.out.println(btree.findKey(7));



        //二叉树的高度
        System.out.println(btree.getHeight());


        //二叉树的结点数
        System.out.println(btree.size());



    }

}

测试结果:

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值