【问题标题】:Static or Extra Function in BSTBST 中的静态或额外功能
【发布时间】:2021-07-15 17:20:12
【问题描述】:

我正在学习 BST 实现,这是我的插入和中序函数代码。我对中序函数有疑问

public class BSTtry {
    
    Node root;
    
    class Node{
        int data;
        Node left,right;
        Node(int data){
            this.data=data;
            left=right=null;
        }
    }
    
    public void insert(int data) {
        root=insertdata(root,data);
    }
    
    public void inorder(){
     printinorder(root);
    }
    public Node insertdata(Node root,int data) {
        if(root==null) {
            root=new Node(data);
            return root;
        }
        if(data<root.data) {
            root.left=insertdata(root.left,data);
        }
        if(data>root.data) {
            root.right=insertdata(root.right,data);
        }
        return root;
    }
    public void printinorder(Node root) {
        if(root!=null) {
            printinorder(root.left);
            System.out.print(root.data+" ");
            printinorder(root.right);
        }
    }
    
    public static void main(String[] args) {
     BSTtry bst=new BSTtry();
     //Inserted some values in the tree
     bst.printinorder(root);
    }

}

所以当我尝试使用 bst.printinorder(root); 时,会抛出错误Cannot make a static reference to the non-static field root

那么我可以通过调用 inorder() 函数将根更改为静态或打印中序。哪个是更好的方法??

【问题讨论】:

标签: java static binary-search-tree


【解决方案1】:

主要方法是静态的,root是BSTtry的一个字段。 是的,您可以将字段更改为静态,但这不是您想要的。

您应该使用设置器或构造器初始化字段,例如使用节点参数。

最好的问候

【讨论】:

    【解决方案2】:

    只需改用inorder(),它调用printinorder() 并以root 作为第一个参数。

    bst.inorder();
    

    【讨论】:

      【解决方案3】:

      从 BSTtry 类中移除 Node 类, 它会工作

        class Node {
        int data;
        Node left, right;
      
        Node(int data) {
          this.data = data;
          left = right = null;
        }
      }
      
      
      public class BSTtry {
      
        Node root;
      
        public void insert(int data) {
          root = insertdata(root, data);
        }
      
        public void inorder() {
          printinorder(root);
        }
      
        public Node insertdata(Node root, int data) {
          if (root == null) {
            root = new Node(data);
            return root;
          }
          if (data < root.data) {
            root.left = insertdata(root.left, data);
          }
          if (data > root.data) {
            root.right = insertdata(root.right, data);
          }
          return root;
        }
      
        public void printinorder(Node root) {
          if (root != null) {
            printinorder(root.left);
            System.out.print(root.data + " ");
            printinorder(root.right);
          }
        }
      
      
      
        public static void main(String[] args) { 
          
          BSTtry bst=new BSTtry();
          //Inserted some values in the tree
          Node a = new Node(1);
          a.left = new Node(2);
          bst.printinorder(a);
      
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2010-09-08
        • 1970-01-01
        • 1970-01-01
        • 2021-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多