数据结构-二叉树 JAVA语言实现

数据结构-二叉树 JAVA语言实现

树是一种类似于链表的数据结构,但树是一种典型的非线性结构,一个结点可以指向多个节点。
二叉树是指树的每一个结点有0,1,2个孩子节点。
严格二叉树:每个结点要么有两个孩子结点,要么没有孩子结点。
满二叉树:每个结点正好有两个孩子结点且所有叶子结点都在同一层。

二叉树结构

public class BinaryTreeNode {

    private int data;
    private BinaryTreeNode leftNode;
    private BinaryTreeNode rightNode;

    public BinaryTreeNode(int data, BinaryTreeNode leftNode, BinaryTreeNode rightNode) {
        super();
        this.data = data;
        this.leftNode = leftNode;
        this.rightNode = rightNode;
    }
    public BinaryTreeNode() {

    }
    public BinaryTreeNode(int data) {
        this.data = data;
        this.leftNode = null;
        this.rightNode = null;
    }
    public int getData() {
        return data;
    }
    public void setData(int data) {
        this.data = data;
    }
    public BinaryTreeNode getLeftNode() {
        return leftNode;
    }
    public void setLeftNode(BinaryTreeNode leftNode) {
        this.leftNode = leftNode;
    }
    public BinaryTreeNode getRightNode() {
        return rightNode;
    }
    public void setRightNode(BinaryTreeNode rightNode) {
        this.rightNode = rightNode;
    }

}

前序遍历

    /**
     * 递归方式 前序遍历
     * @param rootNode 根节点
     */
    private void preOrder(BinaryTreeNode rootNode) {
        if (rootNode != null) {
            System.out.print(rootNode.getData() + "  ");
            preOrder(rootNode.getLeftNode());
            preOrder(rootNode.getRightNode());
        }
    }

中序遍历

/**
 * 递归方式 中序遍历
 * @param rootNode 根节点
 */
private void inOrder(BinaryTreeNode rootNode) {
    if (rootNode != null) {
        inOrder(rootNode.getLeftNode());
        System.out.print(rootNode.getData() + "  ");
        inOrder(rootNode.getRightNode());
    }
}

后序遍历

/**
     * 递归方式 后序遍历
     * @param rootNode 根节点
     */
    private void postOrder(BinaryTreeNode rootNode) {
        if (rootNode != null) {
            postOrder(rootNode.getLeftNode());
            postOrder(rootNode.getRightNode());
            System.out.print(rootNode.getData() + "  ");
        }
    }

初始化二叉树

    /**
     * 初始化二叉树
     * @param firstNode 根节点
     */

    private void initData(BinaryTreeNode firstNode) {
        BinaryTreeNode node2 = new BinaryTreeNode(2, null, null);
        BinaryTreeNode node3 = new BinaryTreeNode(3, null, null);
        BinaryTreeNode node4 = new BinaryTreeNode(4, null, null);
        BinaryTreeNode node5 = new BinaryTreeNode(5, null, null);
        BinaryTreeNode node6 = new BinaryTreeNode(6, null, null);
        BinaryTreeNode node7 = new BinaryTreeNode(7, null, null);
        firstNode.setLeftNode(node2);
        firstNode.setRightNode(node3);
        node2.setLeftNode(node4);
        node2.setRightNode(node5);
        node3.setLeftNode(node6);
        node3.setRightNode(node7);
    }

执行结果

这里写图片描述

这里写图片描述

层次遍历

/**
     * 层次遍历
     * @param firstNode
     */
    private void levelOrder(BinaryTreeNode firstNode) {
        Queue<BinaryTreeNode> queue = new LinkedList<BinaryTreeNode>();
        BinaryTreeNode temp;
        if (firstNode == null) {
            return;
        }
        queue.add(firstNode);
        while(!queue.isEmpty()) {
            temp = queue.remove();
            System.out.print(temp .getData()+"  ");
            if (temp.getLeftNode() != null) {
                queue.add(temp.getLeftNode());
            }
            if (temp.getRightNode() != null) {
                queue.add(temp.getRightNode());
            }
        }
        queue.clear();
    }

层次遍历结果: 1 2 3 4 5 6 7

查找最大

    private int maxNum = 0;;
    private void findMax(BinaryTreeNode rootNode) {
        if (rootNode != null) {
            if (maxNum <= rootNode.getData()) {
                maxNum = rootNode.getData();
            }
            findMax(rootNode.getLeftNode());
            findMax(rootNode.getRightNode());
        }
    }

代码下载地址:
http://download.csdn.net/download/z740852294/10024289

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Owen_le

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值