我的任务是创建一个存储斐波那契数字的树。我以前回收了一些代码,用于创建一个充满名称的树和每个节点的密钥。我似乎无法弄清楚为什么当我执行任何遍历时,它不会打印出存储的节点和值的密钥。我觉得我错过了这么简单的事情。谢谢你的时间!或许我误解了这项任务。我不期待直接回答,因为这与我的教育有关!
原始说明
创建存储您的呼叫的二叉树。每个内部节点会调用多少个调用?对于递归版本的解决方案,您将拥有多少个内部节点?现在,这个数字有多大?如果你调用5“n”并想到Fib(n)怎么办?您的解决方案的运行时复杂性是多少?如果你只是简单地取消递归并迭代计算Fibonacci系列,你认为你能做得更好吗?编写非递归解决方案并对其执行类似的分析。你会用“n”执行多少行代码?是好还是坏?为什么你认为这是真的?
class Node {
int key;
int value;
Node leftChild;
Node rightChild;
Node(int key, int value) {
this.key = key;
this.value = value;
}
}
这是剩下的
package bigobinarytree;
public class BigOBinaryTree {
Node root;
public void addNode(int key, int value) {
Node newNode;
newNode = new Node(key, value);
if (root == null) {
root = newNode;
}
else {
Node focusNode = root;
Node parent;
while (true) {
parent = focusNode;
if (key < focusNode.key) {
focusNode = focusNode.leftChild;
if (focusNode == null) {
parent.leftChild = newNode;
return;
}
}
else
{
focusNode = focusNode.rightChild;
if (focusNode == null) {
parent.rightChild = newNode;
return;
}
}
}
}
}
public void inOrderTraverse(Node focusNode) {
if (focusNode != null) {
inOrderTraverse(focusNode.leftChild);
System.out.println(focusNode);
inOrderTraverse(focusNode.rightChild);
}
}
public void preorderTraverse(Node focusNode) {
if (focusNode != null) {
System.out.println(focusNode);
preorderTraverse(focusNode.leftChild);
preorderTraverse(focusNode.rightChild);
}
}
public void postOrderTraverse(Node focusNode) {
if (focusNode != null) {
postOrderTraverse(focusNode.leftChild);
postOrderTraverse(focusNode.rightChild);
System.out.println(focusNode);
}
}
public Node findNode(int key) {
Node focusNode = root;
while (focusNode.key != key) {
if (key < focusNode.key) {
focusNode = focusNode.leftChild;
} else {
focusNode = focusNode.rightChild;
}
if (focusNode == null)
return null;
}
return focusNode;
}
public Node findValue(int value) {
Node focusNode = root;
while (focusNode.value != value) {
if (value != focusNode.value) {
focusNode = focusNode.leftChild;
} else {
focusNode = focusNode.rightChild;
}
if (focusNode == null)
return null;
}
return focusNode;
}
public boolean remove(int key) {
Node focusNode = root;
Node parent = root;
boolean isItALeftChild = true;
while (focusNode.key != key) {
parent = focusNode;
if (key < focusNode.key) {
isItALeftChild = true;
focusNode = focusNode.leftChild;
} else {
isItALeftChild = false;
focusNode = focusNode.rightChild;
}
if (focusNode == null)
return false;
}
if (focusNode.leftChild == null && focusNode.rightChild == null) {
if (focusNode == root)
root = null;
else if (isItALeftChild)
parent.leftChild = null;
else
parent.rightChild = null;
}
else if (focusNode.rightChild == null) {
if (focusNode == root)
root = focusNode.leftChild;
else if (isItALeftChild)
parent.leftChild = focusNode.leftChild;
else
parent.rightChild = focusNode.leftChild;
}
else if (focusNode.leftChild == null) {
if (focusNode == root)
root = focusNode.rightChild;
else if (isItALeftChild)
parent.leftChild = focusNode.rightChild;
else
parent.rightChild = focusNode.rightChild;
}
else {
Node replacement = getReplacementNode(focusNode);
if (focusNode == root)
root = replacement;
else if (isItALeftChild)
parent.leftChild = replacement;
else
parent.rightChild = replacement;
replacement.leftChild = focusNode.leftChild;
}
return true;
}
public Node getReplacementNode(Node replacedNode) {
Node replacementParent = replacedNode;
Node replacement = replacedNode;
Node focusNode = replacedNode.rightChild;
while (focusNode != null) {
replacementParent = replacement;
replacement = focusNode;
focusNode = focusNode.leftChild;
}
if (replacement != replacedNode.rightChild) {
replacementParent.leftChild = replacement.rightChild;
replacement.rightChild = replacedNode.rightChild;
}
return replacement;
}
public static void main(String[] args) {
BigOBinaryTree theTree = new BigOBinaryTree();
int fib1=1, fib2=1, nacci=1;
int key = 0;
for (int i=3; i <= 50; i++ ){
nacci = fib1 + fib2;
fib1 = fib2;
fib2 = nacci;
theTree.addNode(key, nacci);
key++;
}
System.out.println();
System.out.println("preorderTraverse");
System.out.println();
theTree.preorderTraverse(theTree.root);
System.out.println("___________________");
}
}
答案 0 :(得分:0)
您在main方法中调用preorder方法。
if (focusNode != null) {
System.out.println(focusNode);
preorderTraverse(focusNode.leftChild);
preorderTraverse(focusNode.rightChild);
}
这会给你任何输出吗?如果你得到一些输出,根据你的代码,它将只是focusNode对象的标准toString()方法。如果要打印对象的字段值(如键),则必须访问这些字段或覆盖toString()方法。像:
System.out.println(focusNode.key);
答案 1 :(得分:0)
你是对的,这很简单,苦乐参半。你已经打电话了
System.out.println(focusNode);
但是,对于Objects(如focusNode),System.out.println()
会自动调用该对象的toString()
方法。对于大多数对象来说,这只是意味着打印出他们的对象ID,对于大多数对象来说,这并不是非常有用。至少不适合这样的节目。不过,好消息是有两个修复!首先,您可以覆盖您的节点toString()
。像:
@Override
public String toString() {
return "Key:" + key + " Value:" + value;
}
您只需将其放入Node类中即可。然后,每次调用System.out.println(focusNode)
时,它都会在Node类中实际打印toString()
方法的输出。或者,改为使用:
System.out.println(focusNode);
你可以使用:
System.out.println("Key:" + focusNode.key + " Value:" + focusNode.value);