我正在为课堂上的作业做作业,并且尝试使用教授提供的代码将平衡的二叉树变成堆。
他的基本指令是创建一个名为rebuildTree的方法,如果新节点大于父节点,则该方法将采用新添加的节点并将父节点交换为新节点。
到目前为止,我有一个rebuildTree的测试方法,当我执行Sysout来检查我的值时,它是半工作的,但是它似乎并没有将父子级更改为我想要的值。
/* This is the BalancedBTree class that the professor provided */
import java.util.Queue;
import java.util.LinkedList;
public class BalancedBTree {
private Node firstNode = null;
private Node currentParent = null;
public void addNode(int n) {
if(firstNode == null) {
firstNode = new Node(n);
currentParent = firstNode;
} else {
createNextNode(n);
}
}
public void createNextNode(int n) {
Node nextNode = new Node(n);
if(currentParent.getLeftNode() == null) {
nextNode.setParentNode(currentParent);
currentParent.setLeftNode(nextNode);
rebuildTree(nextNode,currentParent);
} else {
currentParent.setRightNode(nextNode);;
nextNode.setParentNode(currentParent);
rebuildTree(nextNode,currentParent);
}
}
/* --This is my rebuildTree method that I am trying to implement-- */
public void rebuildTree(final Node compareNode, final Node currentParent) {
Node nextNode = compareNode;
Node tempNode = currentParent;
int nxtNode = nextNode.getPayload();
int currParent = currentParent.getPayload();
if(nextNode.getParentNode() != null){
if(nxtNode > currParent){
currentParent.setParentNode(nextNode);
System.out.println(currentParent.getParentNode().getPayload());
nextNode.setLeftNode(currentParent);
System.out.println(nextNode.getLeftNode().getPayload());
}
} else {
System.out.println("false");
}
}
public void updateCurrentParent() {
if(currentParent == firstNode)
currentParent = currentParent.getLeftNode();
else {
// This code works only when called from a right child
// While you are a right child, move up a parent
while(currentParent.getParentNode().getRightNode() == currentParent) {
currentParent = currentParent.getParentNode();
// Check for first node which has no parent
if(currentParent.getParentNode() == null)
currentParent = currentParent.getLeftNode();
}
// Once you are a left hand child, move over to the right hand child.
currentParent = currentParent.getParentNode().getRightNode();
// Go down left hand side until left hanbd side is null
while(currentParent.getLeftNode() != null) {
currentParent = currentParent.getLeftNode();
}
// The currentParent pointer has now been updated
}
}
public void printBalancedBTree() {
Queue<Node> leftQ = new LinkedList<>();
Queue<Node> rightQ = new LinkedList<>();
if(firstNode != null)
leftQ.offer(firstNode);
while(!leftQ.isEmpty() || !rightQ.isEmpty()) {
if(!leftQ.isEmpty()) {
printLeftQ(leftQ,rightQ);
}
System.out.println();
if(!rightQ.isEmpty()) {
printRightQ(leftQ,rightQ);
}
System.out.println();
break;
}
}
public void printLeftQ(Queue<Node> leftQ, Queue<Node> rightQ) {
Node printNode = null;
while(!leftQ.isEmpty()) {
printNode = leftQ.poll();
System.out.print(printNode.getPayload() + "\t");
if(printNode.getLeftNode() != null)
rightQ.offer(printNode.getLeftNode());
if(printNode.getRightNode() != null)
rightQ.offer(printNode.getRightNode());
}
}
public void printRightQ(Queue<Node> leftQ, Queue<Node> rightQ) {
Node printNode = null;
while(!rightQ.isEmpty()) {
printNode = rightQ.poll();
System.out.print(printNode.getPayload() + "\t");
if(printNode.getLeftNode() != null)
leftQ.offer(printNode.getLeftNode());
if(printNode.getRightNode() != null)
leftQ.offer(printNode.getRightNode());
}
}
}
/*This is the main method that I am testing with*/
public class TestBalancedBTree
{
public static void main(String[] args)
{
BalancedBTree bbt = new BalancedBTree();
bbt.addNode(5);
bbt.addNode(10);
bbt.printBalancedBTree();
}
}
/* Adding Node Class */
public class Node {
private int payload;
private Node parentNode = null;
private Node leftNode = null;
private Node rightNode = null;
public Node(int n) {
payload = n;
}
public int getPayload() {
return payload;
}
public void setPayload(int payload){
this.payload = payload;
}
public Node getParentNode() {
return parentNode;
}
public void setParentNode(Node parentNode) {
this.parentNode = parentNode;
}
public Node getLeftNode() {
return leftNode;
}
public void setLeftNode(Node leftNode) {
this.leftNode = leftNode;
}
public Node getRightNode() {
return rightNode;
}
public void setRightNode(Node rightNode) {
this.rightNode = rightNode;
}
}
当我执行rebuildTree方法时,我的输出为我提供:
10 5
但是随后printBalancedTree方法显示: 5 10
我不确定这是哪里出了问题,但是任何帮助都将不胜感激。
答案 0 :(得分:1)
希望这会有所帮助。我使用指定的输入对rebuildTree API进行了一些更改。如果没有帮助评论,那么它将尝试进行优化。但是,如果我们有两个以上元素,这是一个很好的起点。
public void rebuildTree(final Node compareNode, final Node currentParent) {
Node nextNode = compareNode;
Node tempNode = currentParent;
int nxtNode = nextNode.getPayload();
int currParent = currentParent.getPayload();
if(nextNode.getParentNode() != null){
if(nxtNode > currParent){
currentParent.setParentNode(nextNode);
currentParent.setLeftNode(null);
System.out.println(currentParent.getParentNode().getPayload());
nextNode.setLeftNode(currentParent);
nextNode.setParentNode(null);
firstNode=nextNode;
System.out.println(nextNode.getLeftNode().getPayload());
}
} else {
System.out.println("false");
}
}