数据结构与算法

 二叉树基本操作

public class BinaryTree {
    public static void main(String[] args) {
        TreeNode[] arr = new TreeNode[4];
        arr[0] = new TreeNode(1, "1");
        arr[1] = new TreeNode(0, "2");
        arr[2] = new TreeNode(3, "3");
        arr[3] = new TreeNode(4, "4");
        Tree tree = new Tree(arr[0]);
        tree.create(arr);
        tree.treepreorder();
        tree.treeinfixorder();
    }
}

//创建二叉树
class Tree {
    private TreeNode head;

    public Tree(TreeNode head) {
        this.head = head;
    }

    //创建树
    public void create(TreeNode[] arr) {
        if (head != null) {
            TreeNode treeNode = head;
            head.create(arr, treeNode);
        }
    }

    //前序遍历
    public void treepreorder() {
        if (head != null) {
            this.head.preorder();
        }

    }

    //中序遍历
    public void treeinfixorder() {
        if (head != null) {
            this.head.infixorder();
        }
    }

    //后序遍历
    public void treepostxorder() {
        if (head != null) {
            this.head.postorder();
        }
    }

    //先序查找
    public TreeNode searchpre(int num) {
        if (head != null) {
            return head.searchpre(num);
        } else {
            return null;
        }
    }

    //中序遍历
    public TreeNode searchord(int num) {
        if (head != null) {
            return head.searchord(num);
        } else {
            return null;
        }
    }

    //后序遍历
    public TreeNode searchpost(int num) {
        if (head != null) {
            return head.searchpost(num);
        } else {
            return null;
        }
    }

    //删除结点
    public void delNode(int num) {
        //判断根结点是否是要删除结点
        if (head != null) {
            if (head.getId() != num) {
                head.delNode(num);
            } else {
                head = null;
            }
        } else {
            System.out.println("空树,不可以删除");
        }
    }

}

//创建结点
class TreeNode {
    private int id;
    private String name;
    private TreeNode left;
    private TreeNode right;
    private static int i = 0;

    public TreeNode() {
    }

    public TreeNode(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;
    }

    public TreeNode getLeft() {
        return left;
    }

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

    public TreeNode getRight() {
        return right;
    }

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

    //创建二叉树
    public TreeNode create(TreeNode[] arr, TreeNode temp) {
        if (arr == null || arr.length == 0) {
            return null;
        }
        if (i < arr.length) {
            temp = arr[i++];
            if(temp.id==0){
                return null;
            }else{
                temp.left = create(arr, temp.left);
                temp.right = create(arr, temp.right);
            }
        }
        return temp;
    }

    //前序遍历
    public void preorder() {
        System.out.println(" id = " + this.id + " name = " + this.name);
        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(" id = " + this.id + " name = " + this.name);
        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(" id = " + this.id + " name = " + this.name);
    }

    //先序查找
    public TreeNode searchpre(int num) {
        //比较当前结点是否相等
        if (this.id == num) {
            return this;
        }
        //当前结点不相等,向左递归
        TreeNode temp = null;
        if (this.left != null) {
            temp = this.left.searchpre(num);
        }
        if (temp != null) {
            return temp;
        }
        //左子树不满足,向右递归
        if (this.right != null) {
            temp = this.right.searchpre(num);
        }
        return temp;
    }

    //中序查找
    public TreeNode searchord(int num) {
        //判断当前结点左子树是否满足
        TreeNode temp = null;
        if (this.left != null) {
            temp = this.left.searchord(num);
        }
        if (temp != null) {
            return this;
        }
        //判断当前结点是否满足
        if (this.id == num) {
            return this;
        }
        //判断当前结点右结点是否满足
        if (this.right != null) {
            temp = this.right.searchord(num);
        }
        return temp;
    }

    //后序遍历
    public TreeNode searchpost(int num) {
        //判断左结点是否满足
        TreeNode temp = null;
        if (this.left != null) {
            temp = this.left.searchpost(num);
        }
        if (temp != null) {
            return temp;
        }
        //判断右结点是否满足
        if (this.right != null) {
            temp = this.right.searchpost(num);
        }
        if (temp != null) {
            return temp;
        }
        //判断当前结点是否满足
        if (this.id == num) {
            return this;
        }
        return temp;
    }

    //删除结点
    public void delNode(int num) {
        //判断左结点是否满足
        if (this.left != null && this.left.id == num) {
            this.left = null;
            return;
        }
        //判断右结点是否满足
        if (this.right != null && this.right.id == num) {
            this.right = null;
            return;
        }
        //若都为满足,则继续递归
        if (this.left != null) {
            this.left.delNode(num);
        }
        if (this.right != null) {
            this.right.delNode(num);
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

爱敲代码的林先生

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

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

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

打赏作者

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

抵扣说明:

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

余额充值