数据结构中二叉树的实现

数据结构中二叉树的实现
需要实现的方法

package cn.datastructure.com;

public interface BinaryTree {
    //是否是空树
    boolean isEmpty();

    //树的节点数量
    int size();

    //获取二叉树的高度
    int getHeight();

    //查找key
    Node findKey(int key);

    //前序遍历递归
    void preOrderTraverse();

    //中序遍历递归
    void inOrderTraverse();

    //后序遍历递归
    void postOredrTraverse();

    //前序遍历非递归
    void preOrderByStack();

    //中序遍历非递归
    void inOrderByStack();

    //后序遍历非递归
    void postOrderByStack();

    //按照层次遍历二叉树
    void levelOrderByQueue();

}

定义一个节点类

package cn.datastructure.com;

public class Node {
    private int data;
    private Node leftChild;
    private Node rightChild;

    public Node(int data) {
        this.data = data;
    }

    public Node(int data, Node leftChild, Node rightChild) {
        this.data = data;
        this.leftChild = leftChild;
        this.rightChild = rightChild;
    }

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }

    public Node getLeftChild() {
        return leftChild;
    }

    public void setLeftChild(Node leftChild) {
        this.leftChild = leftChild;
    }

    public Node getRightChild() {
        return rightChild;
    }

    public void setRightChild(Node rightChild) {
        this.rightChild = rightChild;
    }

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

实现方法的具体过程

package cn.datastructure.com;

import java.util.*;

public class MyBinaryTree implements BinaryTree {
    private Node root;

    public MyBinaryTree() {

    }

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

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

    @Override
    public int size() {
        System.out.println("二叉树的节点个数:");
        return this.size(root);
    }
    private int size(Node root) {
        if (root == null) {
            return 0;
        }else {
            //获取左子树的size
            int nLeft = size(root.getLeftChild());
            //获取右子树的size
            int nRight = size(root.getRightChild());
            //返回左子树的size和右子树的size之和并加1
            return nLeft+nRight+1;
        }
     }
    @Override
    public int getHeight() {
        System.out.println("二叉树的高度:");
        return getHirht(root);
    }
    private int getHirht(Node root) {
        if (root == null) {
            return 0;
        }else {
            //获取左子树的高度
            int HL = getHirht(root.getLeftChild());
            //获取右子树的高度
            int HR = getHirht(root.getRightChild());
            //返回左子树,右子树中较大的高度并加1
            return HL > HR ? HL+1:HR+1;
        }
    }
    @Override
    public Node findKey(int key) {
        return this.findKey(key,root) ;
    }
    private Node findKey(int key,Node root) {
        if (root == null) {
            return null;
        }else if(root.getData()== key) {
            return root;
        }else {
            Node node1 = this.findKey(key,root.getLeftChild());
            Node node2 = this.findKey(key,root.getRightChild());
            if (node1 != null && node1.getData() == key) {
                return node1;
            }else if(node2 != null && node2.getData() == key) {
                return node2;
            }else {
                return null;
            }
        }
    }
    @Override
    public void preOrderTraverse() {
//        if (root != null) {
//            //前序遍历  先输出根节点的值
//            System.out.print(root.getData()+" ");
//            //对左子树进行先序遍历
//            BinaryTree leftTree = new MyBinaryTree(root.getLeftChild());
//            leftTree.preOrderTraverse();
//            //对右子树进行先序遍历
//            BinaryTree rightTree = new MyBinaryTree(root.getRightChild());
//            rightTree.preOrderTraverse();
//        }
        System.out.println("前序遍历;");
        this.preOrderTraverse(root);
        System.out.println();
    }
    private void preOrderTraverse(Node root) {
        if (root != null) {
            //前序遍历
            System.out.print(root.getData()+" ");
            this.preOrderTraverse(root.getLeftChild());
            this.preOrderTraverse(root.getRightChild());
        }
    }


    @Override
    public void inOrderTraverse() {
        System.out.println("中序遍历:");
        this.inOrderTraverse(root);
        System.out.println();
    }
    private void inOrderTraverse(Node root) {
        if (root != null) {
            //中序遍历
            this.inOrderTraverse(root.getLeftChild());
            System.out.print(root.getData()+" ");
            this.inOrderTraverse(root.getRightChild());
        }
    }
    @Override
    public void postOredrTraverse() {
        System.out.println("后序遍历:");
        this.postOrderTraverse(root);
        System.out.println();
    }
    private void postOrderTraverse(Node root) {
        if (root != null) {
            //后序遍历
            this.postOrderTraverse(root.getLeftChild());
            this.postOrderTraverse(root.getRightChild());
            System.out.print(root.getData()+" ");
        }
    }
    @Override
    public void preOrderByStack() {
        //前序遍历的非递归
//        System.out.println("前序遍历非递归:");
//        Deque<Node> satck = new LinkedList<>();
//        Node current = root;
//        //先将根节点压入栈中
//        satck.push(current);
//        while(current != null && !satck.isEmpty()) {
//            current = satck.poll();
//            System.out.print(current.getData()+" ");
//            if (current.getRightChild() != null) {
//                satck.push(current.getRightChild());
//            }
//            if (current.getLeftChild() != null) {
//                satck.push(current.getLeftChild());
//            }
//        }
//        System.out.println();
        System.out.println("前序遍历非递归:");
        Deque<Node> stack = new LinkedList<>();
        Node current = root;
        stack.push(current);
        while(current != null && !stack.isEmpty()) {
            current = stack.poll();
            System.out.print(current.getData()+" ");
            if (current.getRightChild() != null) {
                stack.push(current.getRightChild());
            }
            if (current.getLeftChild() != null) {
                stack.push(current.getLeftChild());
            }
        }
        System.out.println();
    }

    @Override
    public void inOrderByStack() {
        //中序遍历非递归
//        System.out.println("中序遍历非递归:");
//        Deque<Node> stack = new LinkedList<>();
//        Node current = root;
//        while(current != null || !stack.isEmpty()) {
//            while(current != null) {
//                stack.push(current);
//                current = current.getLeftChild();
//            }
//            if (!stack.isEmpty()) {
//                current = stack.poll();
//                System.out.print(current.getData()+" ");
//                current = current.getRightChild();
//            }
//        }
//        System.out.println();
        System.out.println("中序遍历非递归:");
        Deque<Node> stack = new LinkedList<>();
        Node current = root;
        while(current != null || !stack.isEmpty()) {
            while(current != null) {
                stack.push(current);
                current = current.getLeftChild();
            }
            if (!stack.isEmpty()) {
                current = stack.poll();
                System.out.print(current.getData()+" ");
                current = current.getRightChild();
            }
        }
        System.out.println();
    }

    @Override
    public void postOrderByStack() {
//        System.out.println("后序遍历的非递归:");
//        Deque<Node> stack = new LinkedList<>();
//        Deque<Node> output = new LinkedList<>();
//        Node node = root;
//        while(node != null || !stack.isEmpty()) {
//            if (node != null) {
//                stack.push(node);
//                output.push(node);
//                node = node.getRightChild();
//            }else {
//                node = stack.pop();
//                node = node.getLeftChild();
//            }
//        }
//        while(output.size() > 0) {
//             Node n = output.pop();
//            System.out.print(n.getData()+" ");
//        }
        System.out.println("后序遍历非递归:");
        Deque<Node> stack = new LinkedList<>();
        Deque<Node> output = new LinkedList<>();
        Node current = root;
        while(current != null || !stack.isEmpty()) {
            if(current != null) {
                stack.push(current);
                output.push(current);
                current = current.getRightChild();
            }else {
                current = stack.poll();
                current = current.getLeftChild();
            }
        }
        while(output.size() != 0) {
            Node tmp = output.poll();
            System.out.print(tmp.getData()+" ");
        }
    }

    @Override
    public void levelOrderByQueue() {
        //层次遍历 需要使用队列
        System.out.println("层次遍历:");
        if (root == null) {
            return;
        }
        Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        while(queue.size() != 0) {
            int len = queue.size();
            for (int i = 0; i < len; i++) {
                Node tmp = queue.poll();
                System.out.print(tmp.getData()+" ");
                if (tmp.getLeftChild() != null) {
                    queue.add(tmp.getLeftChild());
                }
                if (tmp.getRightChild() != null) {
                    queue.add(tmp.getRightChild());
                }
            }
        }
        System.out.println();
    }
}

测试结果

package cn.datastructure.com;

public class TestMyBinaryTree {
    public static void main(String[] args) {
//        Node node10 = new Node(10,null,null);
//        Node node9 = new Node(9,null,null);
//        Node node8 = new Node(8,null,node10);
//        Node node7 = new Node(7,null,null);
//        Node node6 = new Node(6,node9,null);
//        Node node5 = new Node(5,node8,null);
//        Node node1 = new Node(1,node7,node6);
//        Node node2 = new Node(2,node1,node5);
        Node node5 = new Node(5, null, null);
        Node node4 = new Node(4, null, node5);

        Node node3 = new Node(3, null, null);
        Node node7 = new Node(7, null, null);
        Node node6 = new Node(6, null, node7);

        Node node2 = new Node(2, node3, node6);

        Node node1 = new Node(1,node4,node2);
        BinaryTree btree = new MyBinaryTree(node1);
        //BinaryTree btree = new MyBinaryTree();
        //System.out.println(btree.isEmpty());

        System.out.println(btree.size());
        System.out.println(btree.getHeight());
        System.out.println(btree.findKey(1));
        btree.preOrderTraverse();
        btree.inOrderTraverse();
        btree.postOredrTraverse();
        btree.levelOrderByQueue();
        btree.inOrderByStack();
        btree.preOrderByStack();
        btree.postOrderByStack();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值