ZBLOG

import java.util.List; import java.util.Scanner; class Node{ Node left; Node right; int data; public Node(int data){ this.left = null; this.right = null; this.data = data; ...

根据您的要求,这是修改后的代码:

import java.util.Scanner;

class Node {
    Node left;
    Node right;
    int data;

    public Node(int data) {
        this.left = null;
        this.right = null;
        this.data = data;
    }
}

class BinaryTree {
    Node root;

    public BinaryTree() {
        root = null;
    }

    public void buildTree(Node node, int data) {
        if (root == null) {
            root = new Node(data);
        } else {
            if (data < node.data) {
                if (node.left == null) {
                    node.left = new Node(data);
                } else {
                    buildTree(node.left, data);
                }
            } else {
                if (node.right == null) {
                    node.right = new Node(data);
                } else {
                    buildTree(node.right, data);
                }
            }
        }
    }

    public Node parent(int key) {
        return parent(root, key);
    }

    public Node parent(Node node, int key) {
        if (node == null) {
            return null;
        }
        if ((node.left != null && node.left.data == key) || (node.right != null && node.right.data == key)) {
            return node;
        }
        
        Node N;
        
        if ((N = parent(node.left, key)) != null) { 
            return N; 
         } else { 
             return parent(node.right, key); 
         }
     }

     public Node findNode(int key){
         return findNode(root,key);
     }

     public Node findNode(Node root,int key){
         if(root==null){
             return null;
         }
         
         if(root.data==key){
             return root;
         }

         Node N;

         if((N=findNode(root.left,key))!=null){
             return N;
         } else {
             root=root.right;
             
             if((N=findNode(root,key))==null){
                 return null;
             } else{
                 return N;
             }
         }
     }

     public void preOrder(Node node, StringBuilder str){
         if(node != null){
             str.append(node.data + ",");
             preOrder(node.left, str);
             preOrder(node.right, str);
         }
     }

     public void inOrder(Node node, StringBuilder str){
         if(node != null){
             inOrder(node.left, str);
             str.append(node.data + ",");
             inOrder(node.right, str);
         }
     }

    public void postOrder(Node node,StringBuilder str){
        if (node != null) {
            postOrder(node.left,str);
            postOrder(node.right,str);
            str.append(node.data + ",");
        }
    }
}

public class Main{
    public static void main(String[] args) {
        StringBuilder str = new StringBuilder();
        Scanner scanner = new Scanner(System.in);

        int count = scanner.nextInt();
        
        String[] a = scanner.next().split(",");
        
        int key = scanner.nextInt();
        
        BinaryTree bTree = new BinaryTree();
        
        for (int i = 0; i < a.length; i++) {
            bTree.buildTree(bTree.root, Integer.valueOf(a[i]));
        }
        
        Node parent = bTree.parent(key);
        
        if (parent == null) {
            System.out.println(key + "无双亲");
        } else {
            System.out.println(key + "的双亲是" + parent.data);
        }
        
       Node currNode = bTree.findNode(key);
       
       if (currNode == null) {
           System.out.println("不存在当前节点");
       } else {
           if(currNode.left == null && currNode.right == null) {
               System.out.println(key + "无孩子");
           } else {
               System.out.print(key + (currNode.left == null ? "无左孩子" : "的左孩子是" + currNode.left.data));
               System.out.println(currNode.right == null ? "无右孩子" : "右孩子是" + currNode.right.data);
           }
       }
       
       if(parent == null){
           System.out.println(key + "无兄弟");
       } else {
           if (parent.right == null || parent.left == null) {
               System.out.println(key + "无兄弟");
           } else {
                System.out.println(key + "的兄弟是" + (parent.right.data == currNode.data ? parent.left.data : parent.right.data));
            }
        }
        
        bTree.preOrder(bTree.root, str);
        System.out.println("前序结果:" + str.toString().substring(0, str.length()-1));
        
        str.setLength(0);
        
        bTree.inOrder(bTree.root, str);
        System.out.println("中序结果:" + str.toString().substring(0, str.length()-1));

        str.setLength(0);

        bTree.postOrder(bTree.root, str);
        System.out.print("后序结果:" + str.toString().substring(0,str.length()-1));
    }
}

这段代码实现了一个二叉树类(BinaryTree),包含构建二叉树、查找节点的父节点、查找指定值的节点、以及三种遍历方式(前序、中序和后序)等功能。在 main 方法中,可以输入要构建的二叉树节点数量、具体节点值、以及需要查找的关键字,然后输出相应的结果。请根据您的需求进行修改和运行。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=15239

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?