首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >堆积式书盒包装

堆积式书盒包装
EN

Stack Overflow用户
提问于 2011-11-13 03:36:02
回答 1查看 154关注 0票数 1

我正在尝试编写一个程序,按顺序阅读书籍,将它们存储在一个堆中,并实现一种贪婪算法,根据重量将书籍有效地打包到盒子中;

我在正确实现堆时遇到了问题。

我用来添加到堆中的方法叫做addLastChild()。它应该找到堆中的下一个点,并插入新书并根据其权重进行重组。

下面是添加代码:

代码语言:javascript
复制
public void addLastChild(Book newBook)
{
    Book[] pathList = new Book[30];
    int tracker = 0;

    int cnt = BookCnt+1;
    String path = "";
    while(cnt >= 1) 
    {
        path = (cnt %2) + path;
        cnt = cnt / 2;
    }

    Book c = root;

    if(root!=null)
    {
        pathList[tracker]=root;
        tracker++;
    }

    for(int i = 1; i < path.length()-1; i++){
        if(path.charAt(i)== '0') {

            c = c.left;
            pathList[tracker]=c;
            tracker++;
        } else {

            c = c.right;
            pathList[tracker]=c;
            tracker++;
        }
    }
    if(path.length() == 1)
    {
        root = newBook;
    } 
    else if(path.charAt(path.length()-1)== '0') {
        c.left = newBook;
        pathList[tracker]=c.left;
        tracker++;

    } 
    else
    {
        c.right = newBook;
        pathList[tracker]=c.right;
        tracker++;
    }
    BookCnt++;

    boolean doTrickle = false;
    if(tracker>=2)
    {
        doTrickle = true;
    }

    while(doTrickle == true)
    {
        Book temp = new Book(pathList[tracker-2].refNumber, pathList[tracker-2].weight, pathList[tracker-2].title, null,null);
        //int refNumber, int weight, String title, Book left, Book right
        print(root,"    ");

        if(pathList[tracker-1].weight > pathList[tracker-2].weight)
        {

            pathList[tracker-2].refNumber=pathList[tracker-1].refNumber;

            pathList[tracker-2].title=pathList[tracker-1].title;
            pathList[tracker-2].weight=pathList[tracker-1].weight;

            if(pathList[tracker-2].left == pathList[tracker-1])
            {
                pathList[tracker-2].left = temp;
            }
            if(pathList[tracker-2].right == pathList[tracker-1])
            {
                pathList[tracker-2].right = temp;
            }

            tracker--;

            System.out.println("we trickled");
            print(root,"    ");
        }
        else
        {
            doTrickle =false;
        }
    }

}

我用来从堆中删除的两个方法是removeLastChild()和remove()。removeLastChild()方法返回堆中的最后一本书,remove()应该返回权重最大的那本书,并用最后一本书替换根,然后相应地重新构造堆。

下面是给我带来麻烦的删除代码:

代码语言:javascript
复制
Book removeLastChild() {
    int cnt = BookCnt;
    String path = "";
    while(cnt >= 1) 
    {
        path = (cnt %2) + path;
        cnt = cnt / 2;
    }

    Book returnBook = null;
    Book c = root;
    for(int i = 1; i < path.length()-1; i++){
        if(path.charAt(i)== '0') {
            c = c.left;
        } else {
            c = c.right;
        }
    }
    if(path.length() == 1)
    {
        returnBook = root;
        root = null;
    } 
    else if(path.charAt(path.length()-1)== '0') {
        returnBook = c.left;
        c.left = null;
    } 
    else
    {
        returnBook = c.right;
        c.right = null;
    }
    BookCnt--;
    return returnBook;
}

Book remove()
{

    Book largest =root; 
    root = removeLastChild();

    if(largest.left!= null)
    {
        root.left = largest.left;
    }
    if(largest.right!= null)
    {
        root.right = largest.right;
    }



    Book cur = root;

    if(root!= null)
    {
        while(cur.left !=null && cur.right!= null)
        {
            if(cur.weight<cur.left.weight || cur.weight<cur.right.weight)
            {
                Book temp = new Book(cur.refNumber, cur.weight, cur.title, null, null);
                //int refNumber, int weight, String title, Book left, Book right

                if(cur.left.weight>cur.right.weight)
                {
                        cur.refNumber = cur.left.refNumber;

                    cur.title = cur.left.title;
                    cur.weight = cur.left.weight;


                    cur.left.refNumber = temp.refNumber;
                    cur.left.weight = temp.weight;
                    cur.left.title = temp.title;
                    cur = cur.left;

                }
                else
                {

                    cur.refNumber = cur.right.refNumber;

                    cur.title = cur.right.title;
                    cur.weight = cur.right.weight;

                    cur.right.refNumber = temp.refNumber;
                    cur.right.weight = temp.weight;
                    cur.right.title = temp.title;
                    cur = cur.right;

                }

            }
            else
            {
                return largest;
            }
        }
    }
    return largest;

}

谢谢你的帮助!

我很乐意澄清任何我没有清楚沟通的事情。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-13 07:38:59

如果我可以为您的堆实现提供一个替代方案,并且考虑到您的目标是解决背包问题的贪婪算法,为什么不简单地使用PriorityQueue呢?

来自文档:“基于优先级堆的无界优先级队列。优先级队列的元素根据它们的自然顺序排序,或通过队列构造时提供的比较器排序(...)”

如果您的book类实现了类似的接口(示例中的Book非常简单):

代码语言:javascript
复制
    class Book implements Comparable<Book>{
        public String title;
        public int weight;

        public Book(int weight, String title) {
            this.weight = weight;
            this.title = title;
        }

        @Override
        public int compareTo(Book anotherBook) {
            return weight - anotherBook.weight;
        }
    }

你的书的自然顺序应该是从最轻的书到最重的书。

在优先级队列中使用Book类:

代码语言:javascript
复制
    public static void main(String[] args) {
        Book book1 = new Book(10,"a");
        Book book2 = new Book(11,"b");
        Book book3 = new Book(20,"c");
        Book book4 = new Book(20,"d");
        Book book5 = new Book(11,"e");

        PriorityQueue<Book> bookQueue = new PriorityQueue<Book>();
        bookQueue.add(book1);
        bookQueue.add(book2);
        bookQueue.add(book3);
        bookQueue.add(book4);
        bookQueue.add(book5);

        while(!bookQueue.isEmpty()){
            Book book = bookQueue.poll();
            System.out.println(book.title + " - " + book.weight);
        }
    }

您应该能够按照需要将书籍放入框中的方式来迭代书籍。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8107012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档