简体   繁体   中英

Reassigning Parent Nodes and Child Nodes in Changing Binary Tree to Heap

I am working on a homework assignment for my class and I am trying to change a balanced binary tree into a heap using code that the professor provided.

His basic instructions were to create a method called rebuildTree, and this method would take the newly added node and swap the parent for the new node if the new node is greater than the parent.

I have a test method of rebuildTree so far that is semi-working when I do a Sysout to check what my values are, but it does not seem to change the parent and child to what I want them to be.

/* 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;
    }
}


My output when I do my rebuildTree method provides me with:

10 5

But then the printBalancedTree method shows: 5 10

I am not sure where I am going wrong with this, but any help is greatly appreciated.

Hope this helps. I did some changes to rebuildTree API with the input specified. If doesn't help comment so that will try to optimize. However, this is a good starting point in case if we have more than 2 elements.

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");
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM