我正在尝试在具有15个节点的完整二叉树上应用动态编程,其中每个节点都是Employee类型,并且具有double类型和ID的值(Evalution_Score)。
我创建了一个double类型的数组来存储最大值。方法find返回ID = j或I
的节点public class BT<T> {
BTNode<T> root, current;
public BT() {
root = current = null;
}
public void max(){
BTNode<Employee> tmproot = (BTNode<Employee>)root;
BTNode<Employee> tmp=null;
double x[] = new double[33];
Employee p[] = new Employee[33];
for(int i=14;i>=0;i--) {
tmp = search(i+1, tmproot);
x[i] = Math.max(x[i+1], (tmp.data.Evaluation_Score + x[i+2+2]));
}
for(int j=14;j>=0;j--) {
tmp = search(j+1, tmproot);
if(x[j+1]<x[j])
p[j] = tmp.data;
}
for(Employee e: p)
System.out.println(e);
}
public BTNode<Employee> search(int id, BTNode<Employee> node){
if(node != null){
if(node.data.ID ==id){
return node;
} else {
BTNode<Employee> foundNode = search(id, node.left);
if(foundNode == null) {
foundNode = search(id, node.right);
}
return foundNode;
}
} else {
return null;
}
}
}
class BTNode <T> {
public T data;
public BTNode<T> left, right;
/** Creates a new instance of BTNode */
public BTNode(T val) {
data = val;
left = right = null;
}
public BTNode(T val, BTNode<T> l, BTNode<T> r){
data = val;
left = l;
right = r;
}
}
class Employee {
int ID_of_parent;
String Name;
int ID;
double Evaluation_Score;
public Employee(int ID_of_parent , String Name , int ID , double Evaluation_Score) {
this.ID_of_parent = ID_of_parent;
this.Name = Name;
this.ID = ID;
this.Evaluation_Score = Evaluation_Score;
}
public String toString() {
return ID_of_parent + ":" + Name + ":" + ID + ":" + Evaluation_Score + "\n";
}
}
但由于某种原因,它不能完全打印所需的输出。