简体   繁体   中英

How To Create Leaf Nodes And Then Build A Tree

I have made a priority queue of integers in increasing order.

I now need to create a leaf node for each term in this priority queue in increasing order. Then, I need to use these leaf nodes to create a tree by taking the sum of the first two terms and replacing them with a leaf node that is their sum, and so on.

For example, if the leaf nodes are {1, 2, 3, 4, 5}, then, since 1 + 2 = 3, the new set of leaf nodes should be {3, 3, 4, 5}, where 3 has left and right children 1 and two respectively. Then, since 3 + 3 + 6, the new set of leaf nodes should be {4, 5, 6}, where the 6 has left and right children 3 and 3 respectively. Then, since 4 + 5 = 9, the new set of leaf nodes should be {6, 9}, where 9 has left and right children 4 and 5 respectively. Then, since 6 + 9 = 15, the new set of leaf nodes should be {15}, where 15 has left and right children 6 and 9 respectively.

Please help me understand how to go about this in Java. Thank you.

Java is an OOP language, meaning that you have to write a "recipes" (called classes) in order to create things (leaf Nodes, trees, anything...).

The standard ways of a tree are the following:

class Tree{

    class LeafNode{

        LeafNode children[];
        String data;        //data could be any Object or Generic

        LeafNode(int childrenNumber, String d){
            data = d;
            children = new LeafNode[childrenNumber];
        }
    }


  LeafNode root;

/*
 *  There can be the class Tree Constructor and put-get functions
 */

}

The leaf-sum idiom you mention can be implemented in the put method.

You first have to write your Leaf class:

public class Leaf {

    public int value = 0;
    public Leaf left = null;
    public Leaf right = null;

    public Leaf(int value) {
    this.value = value;
    }

    public String toString() {
    return "[" + value + ", left: " + left + ", right: " + right + "]";
    }
}

Then you have to implement your algorithm. Something like that should work:

static void doIt(List<Leaf> leaves) {

    if (leaves.size() == 1) {
        return;
    }
    Leaf head = leaves.get(0);
    Leaf next = leaves.get(1);
    Leaf sum = new Leaf(head.value + next.value);

    sum.left = head;
    sum.right = next;
    leaves.set(0, sum);
    leaves.remove(1);

    sortLeaves(leaves);
    doIt(leaves);
    }

    static void sortLeaves(List<Leaf> leaves) {
    Collections.sort(leaves, new Comparator<Leaf>() {
        @Override
        public int compare(Leaf o1, Leaf o2) {
        return o1.value - o2.value;
        }
    });
    }

This is what I remember from data structures

public class Tree
{

    //constructors and stuff

    private class BSTNode<T>
    {
        //constructors and stuff

        private T data;
        private BSTNode<T> left, right;
    }


}

I remember with trees the node class was always a private inner class to a larger tree class. The logic to determine is something is a leaf could be something like this.

public boolean isLeaf()
{
    return (left == null) && (right == null);
}

If the node doesn't have any children then it is a leaf.

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