栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

有趣的二叉树

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

有趣的二叉树

有趣的二叉树

思路:
1、自定义一个有趣的二叉树
2、按照前、中、后序三种遍历,并删除一个结点

给出图片:

代码实现:

public class BinaryTreeTest {
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
        PeopleNode root = new PeopleNode(1,"林黛玉");
        PeopleNode node2 = new PeopleNode(2,"司马懿");
        PeopleNode node3= new PeopleNode(3,"薛宝钗");
        PeopleNode node4= new PeopleNode(4,"诸葛亮");
        PeopleNode node5= new PeopleNode(5,"武媚娘");
        root.setLeft(node2);
        root.setRight(node3);
        node3.setLeft(node4);
        node3.setRight(node5);
        binaryTree.setRoot(root);
        System.out.println("前序遍历");
        binaryTree.preOrder();
        System.out.println("中序遍历");
        binaryTree.midOrder();
        System.out.println("后序遍历");
        binaryTree.postOrder();
        binaryTree.delNode(2);
        System.out.println("删除结点2,前序遍历");
        binaryTree.preOrder();
    }
}

class BinaryTree{
    private PeopleNode root;

    public PeopleNode getRoot(){
    return root;
}

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

public void preOrder() {
    if(this.root != null) {
        this.root.preOrder();
    } else {
        System.out.println("二叉树为空,不能遍历");
    }
}

    public void midOrder() {
        if(this.root != null) {
            this.root.midOrder();
        } else {
            System.out.println("二叉树为空,不能遍历");
        }
    }

    public void postOrder() {
        if(this.root != null) {
            this.root.postOrder();
        } else {
            System.out.println("二叉树为空,不能遍历");
        }
    }

    public void delNode(int no) {
    if(root != null) {
        if(root.getNo() == no) {
            root = null;
        } else {
            root.delNode(no);
        }
    } else {
        System.out.println("空树,不能删除");
    }
    }
}

class PeopleNode {
    private int no;
    private String name;
    private PeopleNode left;
    private PeopleNode right;

    public PeopleNode(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 String setName(String name) {
        return this.name = name;
    }

    public PeopleNode getLeft() {
        return left;
    }

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

    public PeopleNode getRight() {
        return right;
    }

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

    @Override
    public String toString() {
        return "PeopleNode[No=" + no + ",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 midOrder() {
        if (this.left != null) {
            this.left.midOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.midOrder();
        }
    }

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

    public void delNode(int no) {
        if (this.left != null && this.left.no == no) {
            this.left = null;
            return;
        }
        if (this.right != null && this.right.no == no) {
            this.right = null;
            return;
        }
        if (this.left != null) {
            this.left.delNode(no);
        }
        if (this.right != null) {
            this.right.delNode(no);
        }
    }
}

运行截图:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/885970.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号