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

树的遍历(java版)

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

树的遍历(java版)

  class TreeNode{
    int value;
    TreeNode leftNode;
    TreeNode rightNode;
    public TreeNode(int value)
    {
        this.value=value;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public TreeNode getLeftNode() {
        return leftNode;
    }

    public void setLeftNode(TreeNode leftNode) {
        this.leftNode = leftNode;
    }

    public TreeNode getRightNode() {
        return rightNode;
    }

    public void setRightNode(TreeNode rightNode) {
        this.rightNode = rightNode;
    }

    public void frontShow()
    {
        System.out.println(value);
        if(leftNode!=null)
        {
            leftNode.frontShow();
        }
        if(rightNode!=null)
        {
            rightNode.frontShow();
        }
    }
    public void middleShow()
    {
        if(leftNode!=null)
        {
            leftNode.middleShow();
        }
        System.out.println(value);
        if(rightNode!=null)
        {
            rightNode.middleShow();
        }
    }
    public void rightShow()
    {
        if(leftNode!=null)
        {
            leftNode.rightShow();
        }
        if(rightNode!=null)
        {
            rightNode.rightShow();
        }
        System.out.println(value);
    }
    //前序查找
    public TreeNode frontSearch( int target)
    {
        TreeNode targetNode=null;
        if(this.value==target)
        {
            return this;
        }
        else
        {
            if(leftNode!=null)
            {
                targetNode=leftNode.frontSearch(target);
            }
            if(targetNode!=null)
            {
                return targetNode;
            }
            if(rightNode!=null)
            {
                targetNode=rightNode.frontSearch(target);
            }
        }
        return targetNode;
    }
    //前序遍历删除
    public void delete(int target)
    {
        TreeNode parent=this;
        if(parent.leftNode.value==target)
        {
            parent.leftNode=null;
            return;
        }
        if(parent.rightNode.value==target)
        {
            parent.rightNode=null;
            return;
        }
        if(leftNode!=null)
        {
            parent=leftNode;
            parent.delete(target);
        }
        if(rightNode!=null)
        {
            parent=rightNode;
            parent.delete(target);
        }
    }

    public void dfs(TreeNode head)
    {
        if(head==null) return;
        Stack stack=new Stack<>();
        stack.push(head);
        while (!stack.isEmpty())
        {
            TreeNode treeNode=stack.pop();
            System.out.println(treeNode.value);
            System.out.println(" ");
            if(treeNode.rightNode!=null)
            {
                stack.push(treeNode.rightNode);
            }
            if(treeNode.leftNode!=null)
            {
                stack.push(treeNode.leftNode);
            }
        }
    }
    public void bfs(TreeNode head)
    {
        if(head==null)
        {
            return;
        }
        Queue queue=new linkedList<>();
        queue.add(head);
        while (!queue.isEmpty())
        {
            TreeNode treeNode=queue.poll();
            System.out.println(treeNode.value);
            System.out.println(" ");
            if(treeNode.leftNode!=null)
            {
                queue.add(treeNode.leftNode);
            }
            if(treeNode.rightNode!=null)
            {
                queue.add(treeNode.rightNode);
            }
        }
    }
}

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

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

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