二进制搜索树的一系列方法

时间:2015-03-24 05:57:00

标签: java methods data-structures tree binary-search-tree

我在这个BST方法上工作了很长时间,但显然我几乎把所有人都搞错了。它看起来应该工作正常。如果有人可以帮助我那会很棒。

BinaryTree(E item){
    data = item;
    left = new BinaryTree<E>();
    right = new BinaryTree<E>();
  }

  BinaryTree(E item, BinaryTree<E> left, BinaryTree<E> right)
  {
    data = item;
    this.left = left;
    this.right = right;
public int weightBalanceFactor(){
   int thisWeight = Math.abs(left.nodeCount() - right.nodeCount());
  int weirdWeight = Math.abs(left.weightBalanceFactor()
    - right.weightBalanceFactor());
  return Math.max(thisWeight, weirdWeight);
 }

public int nodeSum(){
    int thisInt = Integer.parseInt((String) data);
  return thisInt + left.nodeSum() + right.nodeSum();
    }

这个获得最大值的路径

 public int maxPathSum(){
  int thisInt = Integer.parseInt((String) data);
  return Math.max(thisInt + left.maxPathSum(),
    thisInt + right.maxPathSum());
    }

此其他方法应仅返回每个顺序的横向

  public String preOrder(){
 String buffer = data.toString();
  String leftBuffer = left.preOrder();
  String rightBuffer = right.preOrder();
  if (leftBuffer != "")
   buffer += "\n" + leftBuffer;
  if (rightBuffer != "")
   buffer += "\n" + rightBuffer;
  return buffer;


  }
public String inOrder(){
    String leftBuffer = left.inOrder();
  String buffer = "";
  String rightBuffer = right.inOrder();
  if (leftBuffer != "")
   buffer += leftBuffer + "\n";
  buffer += data.toString();
  if (rightBuffer != "")
   buffer += "\n" + rightBuffer;
  return buffer;}


public String postOrder(){
        String leftBuffer = left.postOrder();
  String rightBuffer = right.postOrder();
  String buffer = "";
  if (leftBuffer != "")
   buffer += leftBuffer + "\n";
  if (rightBuffer != "")
   buffer += rightBuffer + "\n";
  buffer += data.toString();
  return buffer;
       }

0 个答案:

没有答案