【问题标题】:Binary Tree Testing?二叉树测试?
【发布时间】:2016-02-23 02:46:06
【问题描述】:

我有一个 BinaryTree 类、一个 TreeNode 类和一个 BinaryTestClass,它们创建了一个 BinaryTree,其中值可以作为根插入到 TreeNode 中。但是,当我在测试类上运行程序时,它并没有显示应该显示的所有内容。

二叉树类:

public class BinaryTree <T extends Comparable<T>> implements BTree<T>{


TreeNode<T> root;

public BinaryTree(){
    root = null;
}


public BTree<T> left() {
    // TODO Auto-generated method stub
    return left();
}

public BTree<T> right() {
    // TODO Auto-generated method stub
    return right();
}

public void insert(T value) {
    if (root == null){
        root = new TreeNode(value);
    }else if (value.compareTo(value()) < 0){
        root.left().insert(value);
    } else{
        root.right().insert(value);
    }

}

public T value() {
    return value();
  }

}

TreeNode 类:

public class TreeNode<T extends Comparable<T>>{

 T value;
 BinaryTree<T> left, right;


public TreeNode(T value) {
    this.value = value;
    left = new BinaryTree<T>();
    right = new BinaryTree<T>();
}


public T value() {
  return value;
}

public BinaryTree<T> left() {
    return left;
}

public BinaryTree<T> right() {
    return right;
  }

 }

在这个测试类中,我有实例化一个空 BinaryTree 对象的方法。但是,当我运行测试方法时,它只出现在“插入值:”部分代码中。

public class BinaryTreeTest {

/**
 * @throws java.lang.Exception
 */
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}

/**
 * @throws java.lang.Exception
 */
@AfterClass
public static void tearDownAfterClass() throws Exception {
}

/**
 * @throws java.lang.Exception
 */
@Before
public void setUp() throws Exception {
}

/**
 * @throws java.lang.Exception
 */
@After
public void tearDown() throws Exception {
}

@Test
public void test() {
       BinaryTree tree = new BinaryTree(); // empty tree
          int intVal;
          // insert 10 random ints
          System.out.print("Inserting the values: ");
          for (int i = 1; i <= 10; i++) {
             intVal = (int) (Math.random()*100);
             System.out.print(intVal + " ");
             tree.insert(intVal);
          }
          System.out.println(tree);
      } 
  }

我该怎么做才能显示放入树中的完整值列表?

【问题讨论】:

    标签: java tree binary-tree


    【解决方案1】:

    只需执行System.out.println(tree) 不会打印出树中的值。您需要在 BinaryTree 类中创建一个诸如 printTree() 之类的方法,该方法将遍历您的树并打印数字。

    【讨论】:

      【解决方案2】:

      您不能只说 System.out.println(tree),您必须编写自定义方法。我已经起草了一个基本的方法,你可以参考。这可能并不完全正确。

      public static void printTree(Treenode root)
      {
      if(root == null)
      return;
      else
          {
              System.out.println(root.value);
              printTree(root.left());
              printTree(root.right());
           }
      }
      

      这是预购的。你可以写postorder或inorder

      【讨论】:

        【解决方案3】:

        如果你想打印你的类,你必须重写 Object 的 toString() 方法。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多