【问题标题】:Implement pointer style BST in Java?在 Java 中实现指针样式 BST?
【发布时间】:2015-03-21 17:49:40
【问题描述】:

现在,我清楚地明白 Java 引用是“按值传递”

还有那个

Ref r=null;
....
method m1(Ref r)
{
r=r2;
..
}

..//r remains null

但是我刚刚用 Java 编写了一个 C 指针样式的 BST 程序,其中包含 3 个 in、post、pre order 遍历和其他方法,现在有什么方法可以通过修改几行来调整它,我想不到的……

请提出一些方法来让它工作

Java 中是否有任何语言结构可以让我们将 Java 引用视为类似于 C 指针?

Node.java

public class Node{


private int key;
private Node left;
private Node right;

Node(int key)
{
    this.key=key;
    left=null;
    right=null;//redundant as java by default sets to null

}

public int getKey()
{
    return key;
}

public void setKey(int value)
{
    key=value;
}


public Node getLeft()
{
    return left;
}
public  void setLeft(Node left)
{
    this.left=left;
}



public Node  getRight()
{
    return right;
}
public  void setRight(Node right)
{
    this.right=right;
}




}//class Node ends

BST.java

import java.io.*;
import java.util.*;

public class BST {

    private Node root;

    BST()
    {
        root=null;
    }

    public void insert(int value,Node n)
    {
        if(n==null)//found place to enter ,so enter the value there
        {   System.out.println("\nInserting "+value);
            n=new Node(value);
            //n.setKey(value);
        }

        else//need to traverse accordingy
        {
            if(value<n.getKey())//go left
            {
                insert(value,n.getLeft());
                System.out.println("Left of");
            }

            else if (value>n.getKey())//go right
            {
                insert(value,n.getRight());

            }
            else//its a duplicate
            {
                System.out.println("duplicate");
            }


        }

    }

    void preorder(Node n)
    {   System.out.println("Inside Preorder");

        if(n!=null)
        {   System.out.println("Traversing Inorder");
            System.out.println(" "+n.getKey());
            preorder(n.getLeft());
            preorder(n.getRight());
        }
    }

    void postorder(Node n)
    {

        if(n!=null)
        {
            postorder(n.getLeft());
            postorder(n.getRight());
            System.out.println(" "+n.getKey());
        }
    }



    void inorder(Node n)
    {

        if(n!=null)
        {
            inorder(n.getLeft());
            System.out.println(" "+n.getKey());
            inorder(n.getRight());
        }
    }

public Node getroot()
{
    return root;
}


public static void main(String[] args)
{
BST bst=new BST();
int ch=0;

Scanner sc=new Scanner(System.in);

while(true)
{
    System.out.println("Enter choice 1:Insert ,2:Pre Order,3:PostOrder,4:Inorder 0:Exit");
    ch=sc.nextInt();

    switch(ch)
    {

    case 1:
    System.out.println("\nENter the number of elements");

    int n=sc.nextInt();
    int[] temp=new int[n];

    System.out.println("Enter elements to insert");

    for(int i=0;i<n;i++)
    {
        temp[i]=sc.nextInt();
    }


    for(int ele:temp)
    {
    bst.insert(ele,bst.getroot());
    }
    //done inserting all elements
    break;

    case 2:
    System.out.println("PreOrder traversal is \n");
    bst.preorder(bst.getroot());
    break;

    case 3:
    System.out.println("PostOrder traversal is \n");
    bst.postorder(bst.getroot());
    break;

    case 4:
    System.out.println("InOrder traversal is \n");
    bst.inorder(bst.getroot());
    break;


    case 0:
    System.out.println("Exiting");
    System.exit(0);
    break;

    default:
    System.out.println("Enter valid choice");
    }//switch ends
}//loop ends

}// main ends
}//class BST ends

【问题讨论】:

  • 我想我提到过,在不重写整个东西的情况下更改代码的最简单方法是什么,因为插入方法不会添加新节点,因为方法退出后根仍然为空跨度>
  • 为什么会令人惊讶?你从未修改过root(我可以看到);如果你想将它设置为新的node,为什么不在函数的某个位置说root = node

标签: java pointers reference binary-search-tree traversal


【解决方案1】:

不知道是不是理解正确,不过问题似乎出在insert方法(int value, Node n)上,其中n变量在这个方法中接收了一个实例。

如果问题确实是您可以返回实例而不是在方法内分配变量,那么您可以使用返回的值,然后设置您作为参数传递的变量。看起来像:

  public Node insert(int value,Node n){
    if(n==null){
        return new Node(value);
    }else{
        if(value<n.getKey()){
            Node newNode = insert(value,n.getLeft());
            if(n.getLeft()==null){
              n.setLeft(newNode);
            }
            return newNode;
        }else if (value>n.getKey()){
            Node newNode = insert(value,n.getRight());
            if(n.getRight()==null){
              n.setRight(newNode);
            }
            return newNode;
        }else{
            System.out.println("duplicate");
            return null;
        }
    }
}

我要说明的另一点是,Java 中已经存在 BST 实现,除非您使用此模型进行训练,否则仅建议您使用已经存在的实现。

【讨论】:

  • 谢谢,这是我的问题,是的,我只是用它来训练,您能否对代码进行这些更改并作为答案发回..这会很有帮助
  • 嗨@Droid Ics,你能理解我的建议吗?对应你需要什么?
  • 感谢我进行了更改并更改了插入方法以返回一个值,现在它可以工作了,不过我做了一点不同
猜你喜欢
  • 2021-07-14
  • 1970-01-01
  • 1970-01-01
  • 2017-09-17
  • 1970-01-01
  • 1970-01-01
  • 2014-12-11
  • 1970-01-01
  • 2012-10-31
相关资源
最近更新 更多