⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 btpanel.java

📁 The applet illustrates the behaviour of binary search trees, Searching and Sorting Algorithms, Self-
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
            {
                displayMessage(data.getKey(), "Inserting {0}. ", 1);
                target = node;
                continueFID();
            }
            break;

        case 6: // '\006'
            if(node == target)
            {
                addMessage(data.getKey(), "{0} found. ", 2);
                pinball.setEvent(104);
                processEvent();
                break;
            }
            if(node == null)
            {
                addMessage(data.getKey(), "{0} not found. ", 3);
                pinball.setEvent(105);
                processEvent();
            } else
            {
                displayMessage(data.getKey(), "Locating {0}. ", 1);
                target = node;
                continueFID();
            }
            break;
        }
    }

    public void startFID(int mode)
    {
        animation = 4;
        pinball.setMode(mode);
        continueFID();
    }

    public void continueFID()
    {
        count = 0;
    }

    public void stopFID()
    {
        animation = 0;
        pinball.reset();
    }

    public synchronized void animateTDA()
    {
        int mode = pinball.getMode();
        if(!pinball.arrivedTo(target))
        {
            pinball.moveTo(target, speed);
            return;
        }
        if(thoughtful)
        {
            pinball.push(0, -6);
            target.push(0, 6);
            if(count++ < 1)
                return;
        } else
        {
            target.push(pinball.getVx() / 2, pinball.getVy() / 2);
        }
        switch(mode)
        {
        default:
            break;

        case 7: // '\007'
            BTNode node = target.nextPoO();
            remove(target);
            if(node == null)
            {
                displayMessage("", "All nodes deleted. ", 2);
                stopTDA();
            } else
            {
                displayMessage(target.getKey(), "{0} deleted. ", 1);
                target = node;
                pinball.setData(target.getData());
                continueTDA();
            }
            break;

        case 8: // '\b'
            if(target == lastnode)
            {
                displayMessage("", "Traversal completed. ", 2);
                stopTDA();
            } else
            {
                target = target.getNextIn(order);
                pinball.setData(target.getData());
                displayMessage(target.getKey(), "Traversing the tree. Next node {0}... ", 1);
                continueTDA();
            }
            break;
        }
    }

    public void startTDA(int mode)
    {
        animation = 5;
        pinball.setMode(mode);
        continueTDA();
    }

    public void continueTDA()
    {
        count = 0;
    }

    public void stopTDA()
    {
        animation = 0;
        pinball.reset();
    }

    public synchronized void animateSWP()
    {
        if(isThinking())
        {
            pinball.setMode(0);
            pinfall.setMode(0);
            target.setMode(3);
            swapnode.setMode(3);
            return;
        }
        pinball.setMode(11);
        pinfall.setMode(11);
        target.setMode(0);
        swapnode.setMode(0);
        if(!pinball.arrivedTo(target))
            pinball.moveTo(target, speed);
        if(!pinfall.arrivedTo(swapnode))
            pinfall.moveTo(swapnode, speed);
        if(!pinball.arrivedTo(target) || !pinfall.arrivedTo(swapnode))
        {
            return;
        } else
        {
            swapnode.setData(pinfall.getData());
            target.setData(pinball.getData());
            stopSWP();
            pinfall.reset();
            pinball.reset();
            pinball.setEvent(106);
            addMessage("", "", 2);
            processEvent();
            return;
        }
    }

    public void startSWP()
    {
        animation = 6;
        swapnode = target;
        pinball.setData(swapnode.getData());
        pinball.setLocation(swapnode);
        target = tree.getSuccessor(target, minmax);
        pinfall.setData(target.getData());
        pinfall.setLocation(target);
        addMessage(target.getKey(), "Swap with {0}. ", 0);
        continueSWP();
    }

    public void continueSWP()
    {
        count = 0;
    }

    public void stopSWP()
    {
        animation = 0;
    }

    public synchronized void animateAVL()
    {
        int event = pinball.getEvent();
        if(!pinball.arrivedTo(target))
        {
            pinball.moveTo(target, speed);
            return;
        }
        if(rotating)
            return;
        int in;
        if(event == 101)
            in = 1;
        else
        if(event == 102)
        {
            in = -1;
        } else
        {
            stopAVL();
            return;
        }
        int side = pinball.getBalance();
        int balance;
        if(!rotatequeued)
        {
            balance = target.getBalance();
            BTNode child = target.getChild(balance);
            int mode;
            if(balance != in * side)
                mode = 3;
            else
                mode = child.getBalance() != -balance ? 1 : 2;
            if(isThinking())
            {
                switch(mode)
                {
                case 3: // '\003'
                    target.setMode(3);
                    break;

                case 1: // '\001'
                    target.setRotate(1, -balance);
                    break;

                case 2: // '\002'
                    target.setRotate(2, -balance);
                    child.setRotate(1, balance);
                    break;
                }
                return;
            }
            target.reset();
            if(child != null)
                child.reset();
            switch(mode)
            {
            case 3: // '\003'
                addMessage(target.getKey(), "{0} balance: " + MSG.BALANCENAME[balance + in * side + 1], 1);
                target.setBalance(balance + in * side);
                break;

            case 1: // '\001'
                tree.balanceAVL(target);
                addMessage(target.getKey(), "{0} rotated " + getSideName(-balance), 1);
                target = rotate(target, -balance);
                break;

            case 2: // '\002'
                tree.balanceAVL(target);
                addMessage(child.getKey(), "{0} rotated " + getSideName(balance), 1);
                rotate(child, balance);
                pinball.setBalance(-balance);
                rotatequeued = true;
                continueAVL();
                return;
            }
        } else
        {
            rotatequeued = false;
            addMessage(target.getKey(), "{0} rotated " + getSideName(pinball.getBalance()), 1);
            target = rotate(target, pinball.getBalance());
        }
        balance = target.getBalance();
        if(in == 1 && balance == 0 || in == -1 && balance != 0)
        {
            stopAVL();
        } else
        {
            pinball.setBalance(target.getSide());
            target = target.getParent();
            if(target == null)
                stopAVL();
            else
                continueAVL();
        }
    }

    public void startAVL()
    {
        animation = 1;
        pinball.setMode(9);
        continueAVL();
    }

    public void continueAVL()
    {
        pinball.setData(target.getData());
        count = 0;
    }

    public void stopAVL()
    {
        animation = 0;
        pinball.reset();
    }

    public synchronized void animateSPL()
    {
        int mode = pinball.getMode();
        if(mode == 5)
        {
            BTNode lchild = target.getChild(-1);
            BTNode rchild = target.getChild(1);
            if(isThinking())
            {
                if(lchild != null)
                    lchild.setMode(3);
                if(rchild != null)
                    rchild.setMode(3);
                return;
            }
            if(lchild != null)
                lchild.setMode(0);
            if(rchild != null)
                rchild.setMode(0);
            if(!pinball.arrivedTo(target))
            {
                pinball.moveTo(target, speed);
                return;
            } else
            {
                stopSPL();
                return;
            }
        }
        if(!pinball.arrivedTo(target))
        {
            pinball.setLocation(target);
            return;
        }
        if(rotating)
            return;
        BTNode parent = target.getParent();
        if(parent == null)
        {
            stopSPL();
            return;
        }
        int side = target.getSide();
        if(rotatequeued)
        {
            rotate(parent, -side);
            rotatequeued = false;
            continueSPL();
            return;
        }
        mode = 0;
        BTNode grandparent = parent.getParent();
        if(grandparent == null)
            mode = 1;
        else
            mode = parent.getSide() != side ? 3 : 2;
        if(isThinking())
        {
            switch(mode)
            {
            case 1: // '\001'
                parent.setRotate(1, -side);
                break;

            case 2: // '\002'
                grandparent.setRotate(1, -side);
                parent.setRotate(2, -side);
                break;

            case 3: // '\003'
                parent.setRotate(1, -side);
                grandparent.setRotate(2, side);
                break;
            }
            return;
        }
        parent.resetRotate();
        if(grandparent != null)
            grandparent.resetRotate();
        switch(mode)
        {
        case 1: // '\001'
            rotate(parent, -side);
            addMessage("", MSG.ZIG[side + 1], 1);
            break;

        case 2: // '\002'
            rotate(grandparent, -side);
            addMessage("", MSG.ZIGZIG[side + 1], 1);
            rotatequeued = true;
            break;

        case 3: // '\003'
            rotate(parent, -side);
            addMessage("", MSG.ZIGZAG[side + 1], 1);
            rotatequeued = true;
            break;
        }
        continueSPL();
    }

    public void startSPL()
    {
        animation = 2;
        pinball.setData(target.getData());
        continueSPL();
    }

    public void continueSPL()
    {
        count = 0;
    }

    public void stopSPL()
    {
        animation = 0;
        int event = pinball.getEvent();
        processSPL();
    }

    public void animateRBL()
    {
        if(target != null && !pinball.arrivedTo(target))
            pinball.setLocation(target);
        if(rotating)
            return;
        int event = pinball.getEvent();
        if(event == 101)
        {
            BTNode parent = target.getParent();
            int mode = 0;
            if(parent == null || parent.getColor() == 2)
            {
                BTNode node = tree.getRoot();
                node.setColor(2);
                stopRBL();
                return;
            }
            BTNode grandparent = parent.getParent();
            int side = parent.getSide();
            BTNode uncle = grandparent.getChild(-side);
            if(uncle != null && uncle.getColor() == 1)
                mode = 1;
            else
                mode = target.getSide() != -side ? 3 : 2;
            if(isThinking())
            {
                switch(mode)
                {
                case 1: // '\001'
                    parent.setPaint(2);
                    uncle.setPaint(2);
                    grandparent.setPaint(1);
                    break;

                case 2: // '\002'
                    parent.setRotate(1, side);
                    break;

                case 3: // '\003'
                    parent.setPaint(2);
                    grandparent.setPaint(1);
                    grandparent.setRotate(1, -side);
                    break;
                }
                return;
            }
            parent.reset();
            grandparent.reset();
            if(uncle != null)
                uncle.reset();
            switch(mode)
            {
            case 1: // '\001'
                parent.setColor(2);
                uncle.setColor(2);
                grandparent.setColor(1);
                addMessage("", "P " + uncle.getKey() + "B " + parent.getKey() + "B " + grandparent.getKey() + "R. ", 0);
                target = grandparent;
                break;

            case 2: // '\002'
                rotate(parent, side);
                addMessage(parent.getKey(), "R {0} " + getSideName(side), 0);
                target = parent;
                break;

            case 3: // '\003'
                parent.setColor(2);
                grandparent.setColor(1);
                addMessage("", "P " + parent.getKey() + "B " + grandparent.getKey() + "R. ", 0);
                rotate(grandparent, -side);
                addMessage(grandparent.getKey(), "R {0} " + getSideName(-side), 0);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -