数据结构:树的遍历、查找和删除,整树创建

遍历

 

public class HeroNode {
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public HeroNode() {
    }

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

    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 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;
    }

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

    //前序遍历
    public void proOrder() {
        System.out.println(this);
        if (this.left != null) {
            this.left.proOrder();
        }
        if (this.right != null) {
            this.right.proOrder();
        }
    }

    //中序遍历
    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 class BinaryTree {
    private HeroNode root;

    public HeroNode getRoot() {
        return root;
    }

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

    //前序遍历
    public void preOderTree() {
        if (this.root != null) {
            this.root.proOrder();
        }
    }

    //中序遍历
    public void infixOrdeTree() {
        if (this.root != null) {
            this.root.infixOrder();
        }
    }

    //后序遍历
    public void postOderTree() {
        if (this.root != null) {
            this.root.postOrder();
        }
    }
}
public class BinaryTreeDemo {
    public static void main(String[] args) {
        BinaryTree bt = new BinaryTree();
        HeroNode node1 = new HeroNode(1, "宋江");
        HeroNode node2 = new HeroNode(2, "吴用");
        HeroNode node3 = new HeroNode(3, "卢俊义");
        HeroNode node4 = new HeroNode(4, "林冲");
        HeroNode node5 = new HeroNode(5, "关胜");
        bt.setRoot(node1);
        node1.setLeft(node2);
        node1.setRight(node3);
        node3.setLeft(node5);
        node3.setRight(node4);

        bt.preOderTree();//前序遍历:12354
        bt.infixOrdeTree();//中序遍历:21534
        bt.postOderTree();//后序遍历:25431
    }
}

查找

 public class HeroNode {
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

   //前序查找
    public HeroNode preOderSearch(int no) {
        System.out.println("进入前序查找");
        if (this.no == no) {
            return this;
        }
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.preOderSearch(no);
        }
        if(resNode!=null){
            return resNode;
        }
        if (this.right != null) {
            resNode = this.right.preOderSearch(no);
        }
        return resNode;
    }

    //中序查找
    public HeroNode infixOderSearch(int no) {
        HeroNode resNode=null;
        if(this.left!=null){
            resNode=this.left.infixOderSearch(no);
        }
        if(resNode!=null){
            return resNode;
        }
        System.out.println("进入中序查找");
        if(this.no==no){
            return this;
        }
        if(this.right!=null){
            resNode=this.right.infixOderSearch(no);
        }
        return resNode;
    }

    //后序查找
    public HeroNode postOderSearch(int no) {
        HeroNode resNode=null;
        if(this.left!=null){
            resNode=this.left.postOderSearch(no);
        }
        if(resNode!=null){
            return resNode;
        }
        if(this.right!=null){
            resNode=this.right.postOderSearch(no);
        }
        if(resNode!=null){
            return resNode;
        }
        System.out.println("进入后序查找");
        if(this.no==no){
            return this;
        }
        return resNode;
    }
}

public class BinaryTree {
    private HeroNode root;

    //前序查找
    public HeroNode preOderTreeSearch(int no) {
        if (this.root != null) {
            return this.root.preOderSearch(no);
        } else {
            return null;
        }
    }

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

        if (this.root != null) {
            return this.root.infixOderSearch(no);
        } else {
            return null;
        }
    }

    //后序查找
    public HeroNode postOderTreeSearch(int no) {
        if (this.root != null) {
            return this.root.postOderSearch(no);
        } else {
            return null;
        }
    }
}

删除

 public class BinaryTree {
    private HeroNode root;

    //删除
    public HeroNode delTreeNode(int no) {
        if (this.root == null) {
            System.out.println("此树为空");
            return null;
        } else {
            if (this.root.getNo() == no) {
                HeroNode resNode = null;
                this.root = null;
                return resNode;
            } else {
                return this.root.delNode(no);
            }
        }
    }

    //删除2
    public HeroNode delTreeNode2(int no) {
        if (this.root == null) {
            System.out.println("此树为空");
            return null;
        }
        if (this.root.getNo() == no) {
            HeroNode resNode = null;
            this.root = null;
            return resNode;
        }
        return this.root.delNode2(no);
    }
}

public class HeroNode {
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    //删除
    public HeroNode delNode(int no) {
        HeroNode resNode = null;
        if (this.left != null && this.left.no == no) {
            resNode = this.left;
            this.left = null;
            return resNode;
        }
        if (this.right != null && this.right.no == no) {
            resNode = this.right;
            this.right = null;
            return resNode;
        }
        if (this.left != null) {
            resNode = this.left.delNode(no);
        }
        if (this.right != null) {
            resNode = this.right.delNode(no);
        }
        return resNode;
    }

    //删除2
    public HeroNode delNode2(int no) {
        HeroNode resNode = null;
        if (this.left != null) {
            if (this.left.no == no) {
                resNode = this.left;
                this.left = null;
                return resNode;
            } else {
                resNode = this.left.delNode(no);
            }
        }
        if (this.right != null) {
            if (this.right.no == no) {
                resNode = this.right;
                this.right = null;
                return resNode;
            } else {
                resNode = this.right.delNode(no);
            }
        }
        return resNode;
    }
}

整树创建:通过先序遍历结果建立二叉树

 

public class ThreadedBinaryTreeDemo {
    public static void main(String[] args) {
//        ThreadedBinaryTree tbt = new ThreadedBinaryTree();
//        Node node1 = new Node(1);
//        Node node3 = new Node(3);
//        Node node6 = new Node(6);
//        Node node8 = new Node(8);
//        Node node10 = new Node(10);
//        Node node12 = new Node(12);
//        Node node14 = new Node(14);
//        Node node16 = new Node(16);
//        tbt.setRoot(node1);
//        node1.left = node3;
//        node1.right = node6;
//        node3.left = node8;
//        node3.right = node10;
//        node8.right = node12;
//        node6.left = node14;
//        node14.right = node16;
//        tbt.getRoot().preOrder();//1 3 8 12 10 6 14 16
//        System.out.println();
//        tbt.getRoot().infixOrder();//8 12 3 10 1 14 16 6
//        System.out.println();
//        tbt.getRoot().postOrder();//12 8 10 3 16 14 6 1
//        System.out.println();

        //根据先序遍历结果创建二叉树
        ThreadedBinaryTree tbt = new ThreadedBinaryTree();
        int[] arr = {1, 3, 8, 0, 12, 0, 0, 10, 0, 0, 6, 14, 0, 16, 0, 0, 0};
        Node root = tbt.createTree(arr);
        root.preOrder();//1 3 8 12 10 6 14 16
        System.out.println();
        root.infixOrder();//8 12 3 10 1 14 16 6
        System.out.println();
        root.postOrder();//12 8 10 3 16 14 6 1
    }
}

public class ThreadedBinaryTree {
    private Node root;

    public Node getRoot() {
        return root;
    }

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

    public static int num = 0;

    public Node createTree(int[] arr) {
        if (num  >= arr.length) {
            return null;
        }
        Node root = null;
        if (arr[num] != 0) {
            root = new Node(arr[num]);//创建当前节点
            num++;
        }
        if (arr[num] != 0) {
            root.left = createTree(arr);
        } else {
            root.left = null;
            num++;
        }
        if (arr[num] != 0) {
            root.right = createTree(arr);
        } else {
            root.right = null;
            num++;
        }
        return root;
    }
}

public class Node {

    public int no;
    public Node left;
    public Node right;

    public Node() {
    }

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

    public void preOrder() {
        System.out.print(this.no + " ");
        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.print(this.no + " ");
        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.print(this.no + " ");
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值