树1--二叉树的实现和遍历

树(Tree)形结构是一种重要的非线性结构。树形结构反映了数据元素之间的层次关系和分支关系。

基本术语

​ 一个结点的子树个数称为该结点的度(degree),一棵树中结点度的最大值称为该树的度。度为0的结点称为叶子结点或者终端结点。

​ 树中结点的后继结点称为儿子结点,结点的前趋结点称为儿子的父亲结点。

​ 结点的层数使从根开始算起的。设根节点的层数为1,其他结点的层数等于父亲结点的层数加1.

​ 若把树中每个结点的各子树看成从左到右有次序的(即不可互换的),则称该树使有序树。

​ 树中任一结点都可以有零个或者多个后继结点。但至多有一个前趋结点。树中只有根节点无前趋结点,叶子结点无后继结点。

二叉树

二叉树是由n结点组成的有限集合。此集合或者为空,或者有一个根节点加上两颗左右子树。

二叉树的性质:

1、二叉树第i(i>0)层上的结点最多为
2 i − 1 2^{i-1} 2i1
2、高度为k的二叉树最多有的节点数
2 k − 1 2^k-1 2k1
3、对任何二叉树T,n0 ,n1 ,n2 分别表示度数为0、1、2的结点个数。则n0 =n2 +1;

4、具有n个结点的完全二叉树(包括满二叉树)的高度为
⌊ log ⁡ n ⌋ + 1 或 者 ⌈ log ⁡ ( n + 1 ) ⌉ \lfloor \log n \rfloor + 1 或者 \lceil \log (n+1) \rceil logn+1logn+1
5、满二叉树原理:非空满二叉树的叶结点数等于其分支结点数加1

二叉树的实现

通过链表来实现二叉树

在这里插入图片描述

//定义结点
class Node{
    //结点值
    private int no;
    //结点名字
    private String name;
    //左节点
    private Node left;
    //右节点
    private Node right;

    public Node() {
    }

    public Node(int no, String name, Node left, Node right) {
        this.no = no;
        this.name = name;
        this.left = left;
        this.right = right;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Node getLeft() {
        return left;
    }

    public void setLeft(Node left) {
        this.left = left;
    }

    public Node getRight() {
        return right;
    }

    public void setRight(Node right) {
        this.right = right;
    }
}

class BinaryTree{
    //根结点
    private Node root;

    public BinaryTree() {
    }

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

    public Node getRoot() {
        return root;
    }

    public void setRoot(Node root) {
        this.root = root;
    }
}

测试:

public static void main(String[] args) {

    //创建一颗树
    BinaryTree binaryTree = new BinaryTree();

    //定义结点
    Node node3 = new Node(3, "3", null, null);

    Node node1 = new Node(1, "1", node3, null);

    Node node2 = new Node(2, "2", null, null);

    Node root = new Node(0, "0", node1, node2);

    binaryTree.setRoot(root);

    System.out.println(root.getLeft().getLeft().getName());  //"3"

}

创建树的结构:
在这里插入图片描述

二叉树的遍历

二叉树的遍历分成前序,中序,后序和层次遍历。

前序,中序,后序遍历需要用到递归的思想;

而层次遍历,需要借助队列来实现;类似图的广度优先遍历算法

//定义结点
class Node{
    //结点值
    private int no;
    //结点名字
    private String name;
    //左节点
    private Node left;
    //右节点
    private Node right;

    //前序遍历
    public void preOrderNode(){
        //遍历
        System.out.println("结点名字是" + this.getName() + ",值是" + this.getNo());
        //查看它的左节点能否遍历
        if(this.left != null){
            this.left.preOrderNode();
        }
        //查看它的右节点能否遍历
        if(this.right != null){
            this.right.preOrderNode();
        }

    }
    //中序
    public void infixOrderNode(){
        if(this.left != null){
            this.left.infixOrderNode();
        }

        System.out.println("结点名字是" + this.getName() + ",值是" + this.getNo());

        if (this.right != null){
            this.right.infixOrderNode();
        }
    }
    //后序
    public void postOrderNode(){
        if (this.left != null){
            this.left.postOrderNode();
        }

        if (this.right != null){
            this.right.postOrderNode();
        }

        System.out.println("结点名字是" + this.getName() + ",值是" + this.getNo());
    }
}
class BinaryTree{
    //根结点
    private Node root;

    //前序遍历
    public void preOrder(){
        if(this.root != null){
            this.root.preOrderNode();
        }else{
            System.out.println("二叉树为空");
        }
    }

    //中序遍历
    public void infixOrder(){
        if(this.root != null){
            this.root.infixOrderNode();
        }else{
            System.out.println("二叉树为空");
        }
    }

    //后序遍历
    public void postOrder(){
        if(this.root != null){
            this.root.postOrderNode();
        }else{
            System.out.println("二叉树为空");
        }
    }


    //层次遍历

    public void LevelOrder(){
        ArrayQueue<Node> queue = new ArrayQueue<Node>(10);

        queue.add(root);

        while(queue.size() != 0){
            Node node = queue.remove(0);
            System.out.println("结点名字是" + node.getName() + ",值是" + node.getNo());

            if(node.getLeft() != null){
                queue.add(node.getLeft());
            }
            if (node.getRight() != null){
                queue.add(node.getRight());
            }
        }
    }


}

测试:

public static void main(String[] args) {

    //创建一颗树
    BinaryTree binaryTree = new BinaryTree();

    //定义结点
    Node node3 = new Node(3, "3", null, null);

    Node node1 = new Node(1, "1", node3, null);

    Node node2 = new Node(2, "2", null, null);

    Node root = new Node(0, "0", node1, node2);

    binaryTree.setRoot(root);

    binaryTree.preOrder();
    System.out.println("********************************************************");

    binaryTree.infixOrder();

    System.out.println("********************************************************");

    binaryTree.postOrder();

    System.out.println("********************************************************");

    binaryTree.LevelOrder();

}

结果

结点名字是0,值是0
结点名字是1,值是1
结点名字是3,值是3
结点名字是2,值是2
********************************************************
结点名字是3,值是3
结点名字是1,值是1
结点名字是0,值是0
结点名字是2,值是2
********************************************************
结点名字是3,值是3
结点名字是1,值是1
结点名字是2,值是2
结点名字是0,值是0
********************************************************
结点名字是0,值是0
结点名字是1,值是1
结点名字是2,值是2
结点名字是3,值是3
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值