cj云 2023-03-12 20:16 采纳率: 40%
浏览 24

Java写删除二叉树节点,但头节点删不掉只能删掉子节点,求CSDN的技术大拿帮我看看

用java写二叉树删除节点,子节点多可以删掉,但头节点不知道怎么处理
BinaryTree里创建二叉树,删除头节点的方法我也写在这,但方法却删出不了头节点,有CSDN的技术大拿知道怎么解决吗

public class BinaryTree {


    private Hero root;
    Scanner myScanner =new Scanner(System.in);

    public Hero createBinary() {

        Hero hero = null;

        System.out.print("你是否需要创造新节点  y/n: ");
        String key = myScanner.next();
        if ("y".equals(key)) {
            System.out.print("请输入 id = ");
            int id = myScanner.nextInt();
            System.out.print("请输入 name = ");
            String name = myScanner.next();
            hero = new Hero(id, name);
        } else if ("n".equals(key)) {
            return null;
        }

        hero.setLeftChild(createBinary());
        hero.setRightChild(createBinary());
        return hero;

        }

    public void del(Hero tree, int id) {
        this.setRoot(tree);
        Hero parent = new Hero();
        parent.setLeftChild(tree);
        Hero  pa = parent;
        Hero chileNode = root;
        if (chileNode.getId() == id && pa.getLeftChild() == chileNode) {
           pa.setLeftChild(null);
        } else {
            new DeleteTree().del(tree, id);
        }
    }

    public Hero getRoot() {
        return root;
    }

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

}

TraMain运行主方法

public class TraMain {

    static Scanner myScanner = new Scanner(System.in);

    public static void main(String[] args) {

        TraMain traMain = new TraMain();
        BinaryTree binaryTree = new BinaryTree();
        Hero BinaryTree = binaryTree.createBinary();//这样子一颗二叉树就搞好了
        System.out.println("===============递归前序遍历===============");
        traMain.preTravers(BinaryTree);//前序遍历

        binaryTree.del(BinaryTree, 1);
//        DeleteTree.del(BinaryTree, 2);

        System.out.println("===============递归前序遍历===============");
        traMain.preTravers(BinaryTree);//前序遍历
        /**
        System.out.println("===============递归中序遍历===============");
        traMain.inorderTra(BinaryTree);
        System.out.println("===============递归后序遍历===============");
        traMain.postTra(BinaryTree);
        System.out.println("===============非递归前序遍历===============");
        traMain.nonRePreTra(BinaryTree);
        System.out.println("===== 中序非递归遍历(order) / 后序非递归遍历(post) =====");
        System.out.print("请输入 order 或 post: ");
        String key = myScanner.next();
        if ("order".equals(key)) {
            System.out.println("===============非递归中序遍历===============");
            traMain.nonReorderTra(BinaryTree);
        } else {
            System.out.println("===============非递归后序遍历===============");
            traMain.nonRepostTra(BinaryTree);
        }
**/
    }

    //前序遍历二叉树的方法
    public void preTravers(Hero tree) {
        Hero p = tree;

        if (p == null) {
            return;
        }

        System.out.println(p);

        preTravers(p.getLeftChild());
        preTravers(p.getRightChild());
    }

    public void inorderTra(Hero tree) {
        Hero p = tree;

        if (p == null) {
            return;
        }

        inorderTra(p.getLeftChild());
        System.out.println(p);
        inorderTra(p.getRightChild());

    }

    public void postTra(Hero tree) {
        Hero p = tree;

        if (p == null) {
            return;
        }

        postTra(p.getLeftChild());
        postTra(p.getRightChild());
        System.out.println(p);
    }

    public void nonRePreTra(Hero tree) {
        Stack<Hero> stack = new Stack<Hero>();
        Hero p = tree;

        if (p == null) {
            return;
        }

        while (p != null) {

            if (p.getRightChild() != null) {
                stack.push(p.getRightChild());
            }
            System.out.println(p);
            if (p.getLeftChild() != null) {
                p = p.getLeftChild();
            } else if (stack.size() > 0) {
                p = stack.pop();
            } else {
                    p = null;
            }

        }
    }

    public void nonReorderTra(Hero tree) {
        Stack<Hero> stack = new Stack<Hero>();
        Hero p = tree;

        if (p == null) {
            return;
        }

        while (p != null) {
            while (p.getLeftChild() != null && p.getLeftChild().getTag() == 0) {
                stack.push(p);
                p = p.getLeftChild();
            }
            System.out.println(p);
            p.setTag(1);
            if (p.getRightChild() != null) {
                p = p.getRightChild();
            } else if (stack.size() != 0){
                p = stack.pop();
            } else {
                p = null;
            }
        }
    }

    public void nonRepostTra(Hero tree) {
        Stack<Hero> stack = new Stack<Hero>();
        Hero p = tree;

        if (p == null) {
            return;
        }
        while (p != null) {
            while (p.getLeftChild() != null && p.getLeftChild().getTag() == 0) {
                stack.push(p);
                p = p.getLeftChild();
            }
            if (p.getRightChild()  != null && p.getRightChild().getTag() == 0) {
                stack.push(p);
                p = p.getRightChild();
            } else {
                System.out.println(p);
                p.setTag(1);
                if (stack.size() != 0) {
                    p = stack.pop();
                } else if (stack.size() == 0) {
                    p = null;
                }
            }
        }
    }

}

Hero定义节点类


public class Hero {
    private Hero leftChild;
    private Hero rightChild;
    private int id;
    private String name;
    private int tag = 0;//0为未遍历,1为已遍历

    public int getTag() {
        return tag;
    }

    public void setTag(int tag) {
        this.tag = tag;
    }

    public Hero() {
    }

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

    public Hero getLeftChild() {
        return leftChild;
    }

    public void setLeftChild(Hero leftChild) {
        this.leftChild = leftChild;
    }

    public Hero getRightChild() {
        return rightChild;
    }

    public void setRightChild(Hero rightChild) {
        this.rightChild = rightChild;
    }

    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 "Hero{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

}

DeleteTree 类可以删除头节点以外的子节点,而删除头节点的方法为写在BinaryTree类里

public class DeleteTree {

    public void del(Hero tree, int id) {
        Stack<Hero> stack = new Stack<>();
        Stack<Hero> parentStack = new Stack<>();

        Hero parent = null;
        Hero p = tree;

        if (p == null) {
            System.out.println("该二叉树为空...");
            return;
        } else {
            while (p != null && p.getId() != id) {
                parent = p;
                if (p.getLeftChild() == null && p.getRightChild() == null && !parentStack.empty()) {
                    parent = parentStack.pop();
                }
                if (p.getLeftChild() !=  null && p.getLeftChild().getId() == id) {
                    p = p.getLeftChild();
                } else {
                    if (p.getLeftChild() != null) {
                        parentStack.push(parent);
                        if (p.getRightChild() != null) {
                            stack.push(p.getRightChild());
                        }
                        p = p.getLeftChild();
                    } else if (p.getRightChild() != null){
                        p = p.getRightChild();
                    } else if (!stack.empty()) {
                        p = stack.pop();
                    } else {
                        break;
                    }
                }
            }
            if (p.getId() == id && parent.getLeftChild() == p) {
                parent.setLeftChild(null);
            } else if (p.getId() == id && parent.getRightChild() == p) {
                parent.setRightChild(null);
            } else {
                System.out.println(id + " 节点不存在...");
            }
        }
    }
}
  • 写回答

2条回答 默认 最新

  • threenewbee 2023-03-12 21:30
    关注

    看下你的头节点的定义,最好是定义一个header,指向真正的头节点,这样就把特殊问题转换成了一般问题。

    评论

报告相同问题?

问题事件

  • 创建了问题 3月12日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。