我已经在这方面工作了几个星期,并且根本没有任何地方。我正在尝试创建一个应用程序,它将输入2个整数 n 和 k 作为输入。 应用程序应生成并输出具有结构不同的i节点的所有树的数量。最后它应该输出带有n个节点的第k个树。
我有一个例子:
在期刊中输入以下内容:
java differentTrees 5 31
结果是:
The number of structural different trees with 0 nodes is 0
The number of structural different trees with 1 nodes is 1
The number of structural different trees with 2 nodes is 2
The number of structural different trees with 3 nodes is 5
The number of structural different trees with 4 nodes is 14
The number of structural different trees with 5 nodes is 42
<BinaryTreeNode 5 <BinaryTreeNode 1 --> <BinaryTreeNode 3 <BinaryTreeNode 2 <BinaryTreeNode 1 -->->->>
我必须使用BinaryTreeNode类中的代码:
import java.lang.Math;
public class BinaryTreeNode
{
protected Object val; // value associated with node
protected BinaryTreeNode parent; // parent of node
protected BinaryTreeNode left; // left child of node
protected BinaryTreeNode right; // right child of node
public BinaryTreeNode(Object value)
// post: returns a tree referencing value with two null subtrees
{
val = value;
parent = left = right = null;
}
public BinaryTreeNode(Object value,
BinaryTreeNode left,
BinaryTreeNode right)
// post: returns a node referencing value & subtrees
{
this(value);
setLeft(left);
setRight(right);
}
public BinaryTreeNode left()
// post: returns reference to left subtree, or null
{
return left;
}
public BinaryTreeNode right()
// post: returns reference to right subtree, or null
{
return right;
}
public BinaryTreeNode parent()
// post: returns reference to parent node, or null
{
return parent;
}
public void setLeft(BinaryTreeNode newLeft)
// post: sets left subtree to newLeft
// re-parents newLeft if not null
{
if (left != null &&
(left.parent() == this)) left.setParent(null);
left = newLeft;
if (left != null) left.setParent(this);
}
public void setRight(BinaryTreeNode newRight)
// post: sets left subtree to newRight
// re-parents newRight if not null
{
if (right != null &&
(right.parent() == this)) right.setParent(null);
right = newRight;
if (right != null) right.setParent(this);
}
protected void setParent(BinaryTreeNode newParent)
// post: re-parents this node to parent reference, or null
{
parent = newParent;
}
public static int size(BinaryTreeNode n)
// post: returns the size of the subtree rooted at n
{
if (n == null) return 0;
return size(n.left()) + size(n.right()) + 1;
}
public static BinaryTreeNode root(BinaryTreeNode n)
// post: returns the root of the tree node n
{
if ((n == null) || (n.parent() == null)) return n;
else return root(n.parent());
}
public static int height(BinaryTreeNode n)
// post: returns the height of a node n in its tree
{
if (n == null) return -1;
return 1 + Math.max(height(n.left()),height(n.right()));
}
public static int depth(BinaryTreeNode n)
// post: returns the depth of a node in the tree
{
if (n == null) return -1;
return 1 + depth(n.parent());
}
public static boolean isFull(BinaryTreeNode n)
// post: returns true iff the tree rooted at n is full.
{
if (n == null) return true;
if (height(n.left()) != height(n.right())) return false;
return isFull(n.left()) && isFull(n.right());
}
public static boolean isComplete(BinaryTreeNode n)
// post: returns true iff the tree rooted at n is complete
{
int leftHeight, rightHeight;
boolean leftIsFull, rightIsFull;
boolean leftIsComplete, rightIsComplete;
if (n == null) return true;
leftHeight = height(n.left());
rightHeight = height(n.right());
leftIsFull = isFull(n.left());
rightIsFull = isFull(n.right());
leftIsComplete = isComplete(n.left());
rightIsComplete = isComplete(n.right());
// case 1: left is full, right is complete, heights same
if (leftIsFull && rightIsComplete &&
(leftHeight == rightHeight)) return true;
// case 2: left is complete, right is full, heights differ
if (leftIsComplete && rightIsFull &&
(leftHeight == (rightHeight + 1))) return true;
return false;
}
public static boolean isBalanced(BinaryTreeNode n)
// post: returns true iff the tree rooted at n is balanced
{
if (n == null) return true;
return (Math.abs(height(n.left())-height(n.right())) <= 1) &&
isBalanced(n.left()) && isBalanced(n.right());
}
public boolean isLeftChild()
// post: returns true if this is a left child of parent.
{
if (parent() == null) return false;
return this == parent().left();
}
public boolean isRightChild()
// post: returns true if this is a right child of parent.
{
if (parent() == null) return false;
return this == parent().right();
}
public Object value()
// post: returns value associated with this node.
{
return val;
}
public void setValue(Object value)
// post: sets the value associated with this node
{
val = value;
}
public String toString()
// post: returns string representation
{
StringBuffer s = new StringBuffer();
s.append("<BinaryTreeNode "+value());
if (left != null) s.append(" "+left());
else s.append(" -");
if (right != null) s.append(" "+right());
else s.append(" -");
s.append('>');
return s.toString();
}
}
我发现数字正在增加,如加泰罗尼亚数字,但无法弄清楚如何获得输出
感谢您的帮助。
答案 0 :(得分:4)