二叉树

1. 为什么需要树这种数据结构

1)数组存储方式的分析
优点:通过下标方式访问元素,速度快。对于有序数组,还可使用二分查找提高检索速度。
缺点:如果要检索具体某个值,或者插入值(按--定顺序)会整体移动,效率较低
2)链式存储方式的分析
优点:在一定程度上对数组存储方式有优化(比如:插入一个数值节点,只需要将插入节点,链接到链表中即可,删除效率也很好)。
缺点:在进行检索时,效率仍然较低,比如(检索某个值,需要从头节点开始遍历)

3)树存储方式的分析
能提高数据存储,读取的效率,比如利用二叉排序树(Binary Sort Tree),既可以保证数据的检索速度,同时也可以保证数据的插入,删除,修改的速度。

2. 二叉树的概念

  • 1)树有很多种,每个节点最多只能有两个子节点的一-种形式称为二叉树。
  • 2)二叉树的子节点分为左节点和右节点
  • 3)如果该二叉树的所有叶子节点都在最后一层,并且结点总数= 2^n-1,n为层数,则我们称为满二叉树。
  • 4)如果该二叉树的所有叶子节点都在最后一层或者倒数第二层,而且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续,我们称为完全二叉树

3. 二叉树遍历

  • 1)前序遍历:先输出父节点,再遍历左子树和右子树
  • 2)中序遍历:先遍历左子树,再输出父节点,再遍历右子树
  • 3)后序遍历:先遍历左子树,再遍历右子树,最后输出父节点
  • 4)小结:看输出父节点的顺序,就确定是前序,中序还是后序

    public void preOrder(){
        System.out.println(this);
        if(this.left!=null){
            this.left.preOrder();
        }
        if(this.right!=null){
            this.right.preOrder();
        }
    }

    public void infixOrder(){
        if(this.left!=null){
            this.left.infixOrder();
        }
        System.out.println(this);
        if(this.right!=null){
            this.right.infixOrder();
        }
    }

    public void postOrder(){
        if(this.left!=null){
            this.left.postOrder();
        }
        if(this.right!=null){
            this.right.postOrder();
        }
        System.out.println(this);
    }

4. 二叉树查找

 //先序查找
    public HeroNode preOrderSearch(int no){
        //判断比较次数
        System.out.println("preOrderSearch()..+1");
        HeroNode resNode=null;
        if(this.id==no){
            return this;
        }
        if(this.left!=null){
            resNode=this.left.preOrderSearch(no);
        }
        if(resNode!=null){
            return resNode;
        }
        if(this.right!=null){
            resNode=this.right.preOrderSearch(no);
        }
        return resNode;
    }

    //中序查找
    public HeroNode infixOrderSearch(int no){

        HeroNode resNode=null;
        if(this.left!=null){
            resNode=this.left.infixOrderSearch(no);
        }
        if(resNode!=null){
            return resNode;
        }
        System.out.println("infixOrderSearch() ..+1");
        if(this.id==no){
            return this;
        }
        if(this.right!=null){
            resNode=this.right.infixOrderSearch(no);
        }
        return resNode;
    }

    //后续查找
    public HeroNode postOrderSearch(int no){
        HeroNode resNode=null;
        if(this.left!=null){
            resNode=this.left.postOrderSearch(no);
        }
        if(resNode!=null){
            return resNode;
        }
        if(this.right!=null){
            resNode=this.right.postOrderSearch(no);
        }
        if(resNode!=null){
            return resNode;
        }
        System.out.println("postOrderSearch() ..+1");

        if(this.id==no){
            return this;
        }
        return resNode;
    }

5. 删除节点 

    //如果删除的节点是叶子节点,则删除该节点
    //如果删除的节点是非叶子节点,则删除该子树.

    //删除节点
    public void deleteNode(int no){
        if(this.left!=null&&this.left.id==no){
            this.left=null;
            return;
        }
        if(this.right!=null&&this.right.id==no){
            this.right=null;
            return;
        }

        if(this.left!=null){
            this.left.deleteNode(no);
        }
        if(this.right!=null){
            this.right.deleteNode(no);
        }
    }

 完整代码实现

package com.zx.ds.tree;

class HeroNode{
    private int id;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public HeroNode getLeft() {
        return left;
    }

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

    public HeroNode getRight() {
        return right;
    }

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

    public HeroNode(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "HeroNode{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    public void preOrder(){

        System.out.println(this);
        if(this.left!=null){
            this.left.preOrder();
        }
        if(this.right!=null){
            this.right.preOrder();
        }
    }

    public void infixOrder(){
        if(this.left!=null){
            this.left.infixOrder();
        }
        System.out.println(this);
        if(this.right!=null){
            this.right.infixOrder();
        }
    }

    public void postOrder(){
        if(this.left!=null){
            this.left.postOrder();
        }
        if(this.right!=null){
            this.right.postOrder();
        }
        System.out.println(this);
    }

    //先序查找
    public HeroNode preOrderSearch(int no){
        //判断比较次数
        System.out.println("preOrderSearch()..+1");
        HeroNode resNode=null;
        if(this.id==no){
            return this;
        }
        if(this.left!=null){
            resNode=this.left.preOrderSearch(no);
        }
        if(resNode!=null){
            return resNode;
        }
        if(this.right!=null){
            resNode=this.right.preOrderSearch(no);
        }
        return resNode;
    }

    //中序查找
    public HeroNode infixOrderSearch(int no){

        HeroNode resNode=null;
        if(this.left!=null){
            resNode=this.left.infixOrderSearch(no);
        }
        if(resNode!=null){
            return resNode;
        }
        System.out.println("infixOrderSearch() ..+1");
        if(this.id==no){
            return this;
        }
        if(this.right!=null){
            resNode=this.right.infixOrderSearch(no);
        }
        return resNode;
    }

    //后续查找
    public HeroNode postOrderSearch(int no){
        HeroNode resNode=null;
        if(this.left!=null){
            resNode=this.left.postOrderSearch(no);
        }
        if(resNode!=null){
            return resNode;
        }
        if(this.right!=null){
            resNode=this.right.postOrderSearch(no);
        }
        if(resNode!=null){
            return resNode;
        }
        System.out.println("postOrderSearch() ..+1");

        if(this.id==no){
            return this;
        }
        return resNode;
    }

    //删除节点
    public void deleteNode(int no){
        if(this.left!=null&&this.left.id==no){
            this.left=null;
            return;
        }
        if(this.right!=null&&this.right.id==no){
            this.right=null;
            return;
        }

        if(this.left!=null){
            this.left.deleteNode(no);
        }
        if(this.right!=null){
            this.right.deleteNode(no);
        }
    }

}

class Tree{
    private HeroNode root;

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

    public void preOrder(){
        if(this.root==null){
            System.out.println("tree is null");
        }else{
            this.root.preOrder();
        }
    }

    public void infixOrder(){
        if(this.root==null){
            System.out.println("tree is null");
        }else{
            this.root.infixOrder();
        }
    }

    public void postOrder(){
        if(this.root==null){
            System.out.println("tree is null");
        }else{
            this.root.postOrder();
        }
    }

    public HeroNode infixOrderSearch(int no){
        if(root!=null){
            return this.root.infixOrderSearch(no);
        }else{
            return null;
        }
    }

    public HeroNode preOrderSearch(int no){
        if(root!=null){
            return this.root.preOrderSearch(no);
        }else{
            return null;
        }
    }

    public HeroNode postOrderSearch(int no){
        if(root!=null){
            return this.root.postOrderSearch(no);
        }else{
            return null;
        }
    }

    public void deleteNode(int no){
        if(root==null){
            System.out.println("null tree..");
        }else{
            if(this.root.getId()==no){
                this.root=null;
            }else{
                this.root.deleteNode(no);
            }
        }
    }

}

public class BinaryTreeDemo {

    public static void main(String[] args) {
        HeroNode heroNode=new HeroNode(1, "宋江");
        HeroNode heroNode2=new HeroNode(2,"吴用" );
        HeroNode heroNode3=new HeroNode(3,"卢俊义" );
        HeroNode heroNode4=new HeroNode(4,"公孙胜" );
        HeroNode heroNode5=new HeroNode(5,"关胜" );

        heroNode.setLeft(heroNode2);
        heroNode.setRight(heroNode3);
        heroNode3.setLeft(heroNode5);
        heroNode3.setRight(heroNode4);

        Tree tree=new Tree();
        tree.setRoot(heroNode);

        System.out.println("先序遍历:");
        //1 2 3 5 4
        tree.preOrder();

        System.out.println("中序遍历:");
        //2 1 5 3 4
        tree.infixOrder();

        System.out.println("后序遍历:");
        //2 5 4 3 1
        tree.postOrder();

        HeroNode res=tree.infixOrderSearch(5);
        System.out.println("中序查找:");
        if(res==null){
            System.out.println(" no is not exist..");
        }else{
            System.out.println(res);
        }

        HeroNode res2=tree.preOrderSearch(5);
        System.out.println("先序查找:");
        if(res2==null){
            System.out.println(" no is not exist..");
        }else{
            System.out.println(res2);
        }

        HeroNode res3=tree.postOrderSearch(5);
        System.out.println("后序查找:");
        if(res3==null){
            System.out.println(" no is not exist..");
        }else{
            System.out.println(res3);
        }

        System.out.println("先序遍历:");
        //1 2 3 5 4
        tree.preOrder();
        tree.deleteNode(3);
        System.out.println("先序遍历:");
        //1 2
        tree.preOrder();


    }
}
先序遍历:
HeroNode{id=1, name='宋江'}
HeroNode{id=2, name='吴用'}
HeroNode{id=3, name='卢俊义'}
HeroNode{id=5, name='关胜'}
HeroNode{id=4, name='公孙胜'}
中序遍历:
HeroNode{id=2, name='吴用'}
HeroNode{id=1, name='宋江'}
HeroNode{id=5, name='关胜'}
HeroNode{id=3, name='卢俊义'}
HeroNode{id=4, name='公孙胜'}
后序遍历:
HeroNode{id=2, name='吴用'}
HeroNode{id=5, name='关胜'}
HeroNode{id=4, name='公孙胜'}
HeroNode{id=3, name='卢俊义'}
HeroNode{id=1, name='宋江'}
infixOrderSearch() ..+1
infixOrderSearch() ..+1
infixOrderSearch() ..+1
中序查找:
HeroNode{id=5, name='关胜'}
preOrderSearch()..+1
preOrderSearch()..+1
preOrderSearch()..+1
preOrderSearch()..+1
先序查找:
HeroNode{id=5, name='关胜'}
postOrderSearch() ..+1
postOrderSearch() ..+1
后序查找:
HeroNode{id=5, name='关胜'}
先序遍历:
HeroNode{id=1, name='宋江'}
HeroNode{id=2, name='吴用'}
HeroNode{id=3, name='卢俊义'}
HeroNode{id=5, name='关胜'}
HeroNode{id=4, name='公孙胜'}
先序遍历:
HeroNode{id=1, name='宋江'}
HeroNode{id=2, name='吴用'}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值