简体   繁体   中英

writing a method for a seperate class file

I have a .class file named UBT.class (which i do not have access to the source code). I need to retrieve data from the UBT.class file. I have access to methods such as .getRoot() & .getLeft() & .getRight() from the UBT class (Not using the methods from the TreeNode class).

I tried writing an inOrder traversal method using recursion like this but its giving me errors like below although i specifiy it to be UBT not TreeNode

Error: incompatible types: TreeNode cannot be converted to UBT

//From main method

     public static void inOrder(UBT root)
      {
        if(root.getRoot() != null)
        {
          inOrder(root.getRoot().getLeft());
          System.out.println(root.getRoot().getData() + " ");
          inOrder(root.getRoot().getRight());
        }
      }

class TreeNode
{
  private int data;
  private TreeNode left, right;

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

  public int getData() {
    return data;
  }

  public void setData(int newData){
    this.data = newData;
  }

  public TreeNode getLeft() {
    return left;
  }

  public TreeNode getRight() {
    return right;
  }

  public void setLeft(TreeNode left) {
    this.left = left;
  }

  public void setRight(TreeNode right) {
    this.right = right;
  }
}

class BST // Typical BST implementation

It looks like you want to split it up so your main recursion is on TreeNodes, not the UBT object.

  public static void inOrder(TreeNode node) {
    if(node != null)
    {
      inOrder(node.getLeft());
      System.out.println(node.getData() + " ");
      inOrder(node.getRight());
    }
  }

  public static void inOrder(UBT root) {
    if (root.getRoot() != null) {
      inOrder(root.getRoot());
    }
  }

Using this, you'd call inOrder with your UBT, then it would grab the root TreeNode and do recursion on that with the TreeNode version of inOrder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM