gpt4 book ai didi

java - 打印树元素的数据和位置

转载 作者:行者123 更新时间:2023-12-02 06:27:08 24 4
gpt4 key购买 nike

我必须根据的级别打印元素。然而,我必须使用递归来完成这个目标;我只返回根数据。我显然忽视了 child

/**
* A binary tree in which each node has two children.
*/
public class BinaryTree {

private Node root;

/**
* Constructs an empty tree.
*/
public BinaryTree() {
root = null;
}

/**
* Constructs a tree with one node and no children.
*
* @param rootData the data for the root
*/
public BinaryTree(Object rootData) {
//
root = new Node();
root.data = rootData;
root.left = null;
root.right = null;
}

/**
* Constructs a binary tree.
*
* @param rootData the data for the root
* @param left the left subtree
* @param right the right subtree
*/
public BinaryTree(Object rootData, BinaryTree left, BinaryTree right) {
//
root = new Node();
root.data = rootData;
root.left = left.root;
root.right = right.root;
}

class Node {

public Object data;
public Node left;
public Node right;

public String printTree(int level) {
String strVal = "";
strVal += root.data;
if (root.left != null) {
strVal += this.left.printTree(level + 1);
}
return strVal;
}
}

/**
* Returns the height of the subtree whose root is the given node.
*
* @param n a node or null
* @return the height of the subtree, or 0 if n is null
*/
private static int height(Node n) {
//
if (n == null) {
return 0;
} else {
return 1 + Math.max(height(n.left), height(n.right));
}
}

/**
* Returns the height of this tree.
*
* @return the height
*/
public Object data() {
//
return root.data;
}

/**
* Gets the left subtree of this tree
*
* @return the left child of the root
*/
public BinaryTree left() {
//
BinaryTree result = new BinaryTree();
result.root = root.left;
return result;
}

/**
* Gets the right subtree of this tree
*
* @return the right child of the root
*/
public BinaryTree right() {
//
BinaryTree result = new BinaryTree();
result.root = root.right;
return result;
}

/**
* Sets a new right child
*
* @param child the new right child
*/
public void setRight(BinaryTree child) {
//
BinaryTree result = new BinaryTree();
result.root = root.right;
result = child;
}

/**
* Sets a new left child
*
* @param child the new left child
*/
public void setLeft(BinaryTree child) {
//
BinaryTree result = new BinaryTree();
result.root = root.left;
result = child;
}

public void setData(Object data) {
//
root.data = data;
}

public boolean isLeaf() {
//

if (root.left == null && root.right == null) {
return true;
} else {
return false;
}

}

public String printTree() {
//
return root.printTree(0);
}
}

我不明白为什么我只返回根。

最佳答案

您的 leftright 值永远不会被设置,它们将始终为 null

还有一个更好的方法是这样的

private String strVal = "";  // StringBuffer would be better

String result = printTree (root);

public String printTree(Node n) {

if (node.left != null) {
strVal += node.toString();
printTree (node.left);
}
if (node.right != null) {
strVal += node.toString();
printTree (node.right);
}
return strVal;
}

上面的方法使用传统的方式将节点传递给递归方法。

关于java - 打印树元素的数据和位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20416048/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com