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

📄 btpanel.java

📁 平衡二叉树 生成
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                	{
                		case 0:  
                			btnode4 = nextnode.nextInO();
                			if(btnode4 != null) System.out.println("btnode4 mode: " + btnode4.mode + " btnode4 data: " + btnode4.data.key);
                			break;
                		case 1:
                			btnode4 = nextnode.nextPrO();
                			if(btnode4 != null)System.out.println("btnode4 mode: " + btnode4.mode + " btnode4 data: " + btnode4.data.key);
                			break;
                		case 2:
                			btnode4 = nextnode.nextPoO();
                			break;
                	}
                	
                    //System.out.println("btnode4 mode: " + btnode4.mode + " btnode4 data: " + btnode4.data.key);
                    
                    if(btnode4 == null)
                    {
                    	treenode.mode = 0;
                    	nextnode.mode = 0;
                    	this.resetModes();
                    	//applet.playSound(2);
                    	return;
                    }
                    nextnode.mode = 0;
                    nextnode = btnode4;
                    nextnode.mode = 98;
                    treenode.X = nextnode.x;
                    treenode.Y = nextnode.y;
                    applet.playSound(1);
                    return;
                }
                break;

            }
    }

    public void placeNodes()
    {
        if(tree.root == null)
            return;
        BTNode btnode2;
        if(picknode == null)
        {
            btnode2 = tree.root;
            btnode2.X = center.x;
            btnode2.Y = center.y;
        } else
        {
            btnode2 = picknode;
        }
        BTNode btnode3 = btnode2.parent;
        btnode2.parent = null;
        if(applet.treeInOrder)
        {
            int k = 0;
            BTNode btnode;
            for(btnode = btnode2; btnode.prevInO() != null;)
            {
                btnode = btnode.prevInO();
                k--;
            }

            for(; btnode != null; btnode = btnode.nextInO())
            {
                int i = depth(btnode);
                btnode.X = btnode2.X + 16 * k;
                btnode.Y = btnode2.Y + 32 * i;
                k++;
            }

        } else
        {
            for(BTNode btnode1 = btnode2; btnode1 != null; btnode1 = btnode1.nextPrO())
            {
                int j = depth(btnode1);
                int l = xside(btnode1, j);
                btnode1.X = btnode2.X + l;
                btnode1.Y = btnode2.Y + 32 * j;
            }

        }
        btnode2.parent = btnode3;
    }

    public int depth(BTNode btnode)
    {
        int i;
        for(i = 0; btnode.parent != null; i++)
            btnode = btnode.parent;

        return i;
    }

    public int xside(BTNode btnode, int i)
    {
        boolean flag = false;
        double d = 0.0D;
        double d1 = 128D;
        for(int j = 0; j < i; j++)
            d1 /= 2D;

        for(int k = 0; k < i; k++)
        {
            BTNode btnode1 = btnode.parent;
            if(btnode == btnode1.lchild)
                d -= d1;
            else
                d += d1;
            btnode = btnode1;
            d1 *= 2D;
        }

        return (int)d;
    }

    public synchronized void update(Graphics g)
    {
        paint(g);
    }

    public void paint(Graphics g)
    {
        Dimension dimension = size();
        if(image == null || dimension.width != image.getWidth(null) || dimension.height != image.getHeight(null))
        {
            center.x = dimension.width / 2;
            center.y = dimension.height / 8;
            placeNodes();
            image = createImage(dimension.width, dimension.height);
            bg = image.getGraphics();
            bg.setFont(getFont());
        }
        bg.setColor(Color.white);
        bg.fill3DRect(0, 0, dimension.width, dimension.height, true);
        bg.setColor(LINECOLOR);
        bg.drawRect(0, 0, dimension.width - 1, dimension.height - 1);
        drawLines(bg);
        drawNodes(bg);
        g.drawImage(image, 0, 0, null);
    }

    public void drawLines(Graphics g)
    {
        if(tree.root != null)
            g.drawLine(center.x, 0, tree.root.x, tree.root.y);
        for(BTNode btnode = tree.root; btnode != null; btnode = btnode.nextPrO())
            if(btnode.parent != null)
                g.drawLine(btnode.x, btnode.y, btnode.parent.x, btnode.parent.y);

    }

    public void drawNodes(Graphics g)
    {
        FontMetrics fontmetrics = g.getFontMetrics();
        inverse = !inverse;
        for(BTNode btnode = tree.root; btnode != null; btnode = btnode.nextPrO())
            drawNode(g, btnode, fontmetrics);

        if(picknode != null)
            drawNode(g, picknode, fontmetrics);
        if(prevpick != null)
            drawNode(g, prevpick, fontmetrics);
        if(nextpick != null)
            drawNode(g, nextpick, fontmetrics);
        if(treenode.mode == 1 || treenode.mode == 3 || treenode.mode == 5)
            drawNode(g, treenode, fontmetrics);
        if(treenode.mode == 99)
        	drawNode(g, nextnode, fontmetrics);
    }

    public void drawNode(Graphics g, BTNode btnode, FontMetrics fontmetrics)
    {
        int i = btnode.x;
        int j = btnode.y;
        int k = fontmetrics.stringWidth(btnode.data.toString()) + 10;
        int l = fontmetrics.getHeight() + 4;
        Color color;
        if(btnode == picknode)
            color = ROOTCOLOR;
        else
        if(btnode == prevpick || btnode == nextpick)
            color = PICKCOLOR;
        else
        if(btnode == treenode || btnode.mode == 3)
            color = BALLCOLOR;
        else
        if(btnode.mode == 2)
            color = inverse ? FINDCOLOR : NODECOLOR;
        else
        if(btnode.mode == 4)
            color = inverse ? FINDCOLOR : NODECOLOR;
        else
        if(btnode.mode == 98)
        	//color = inverse ? BALLCOLOR : NODECOLOR;
        	color = BALLCOLOR;
        else
            color = NODECOLOR;
        Color color1 = EDGECOLOR;
        g.setColor(color);
        g.fillOval(i - k / 2, j - l / 2, k, l);
        g.setColor(color1);
        g.drawOval(i - k / 2, j - l / 2, k - 1, l - 1);
        g.drawString(btnode.data.toString(), i - (k - 10) / 2, (j - (l - 4) / 2) + fontmetrics.getAscent());
        if(btnode == prevpick)
            g.drawString("prev", i - k / 2, j - l / 2 - 1);
        if(btnode == nextpick)
            g.drawString("next", i - k / 2, j - l / 2 - 1);
    }

    public synchronized boolean mouseDown(Event event, int i, int j)
    {
        if(tree.root != null)
        {
            double d = 1.7976931348623157E+308D;
            for(BTNode btnode = tree.root; btnode != null; btnode = btnode.nextPrO())
            {
                double d1 = (btnode.x - i) * (btnode.x - i) + (btnode.y - j) * (btnode.y - j);
                if(d1 < d)
                {
                    picknode = btnode;
                    d = d1;
                }
                btnode.mode = 0;
            }

            picknode.X = picknode.x = i;
            picknode.Y = picknode.y = j;
            int k = applet.CLpick.getSelectedIndex();
            if(k == 0)
            {
                prevpick = picknode.prevInO();
                nextpick = picknode.nextInO();
            } else
            if(k == 1)
            {
                prevpick = picknode.prevPrO();
                nextpick = picknode.nextPrO();
            } else
            if(k == 2)
            {
                prevpick = picknode.prevPoO();
                nextpick = picknode.nextPoO();
            } else
            {
                prevpick = null;
                nextpick = null;
            }
            placeNodes();
            applet.TFdata.setText(picknode.data.toString());
            repaint();
        }
        return true;
    }

    public synchronized boolean mouseDrag(Event event, int i, int j)
    {
        if(tree.root != null)
        {
            picknode.X = picknode.x = i;
            picknode.Y = picknode.y = j;
            placeNodes();
            repaint();
        }
        return true;
    }

    public synchronized boolean mouseUp(Event event, int i, int j)
    {
        if(tree.root != null)
        {
            picknode = null;
            prevpick = null;
            nextpick = null;
            placeNodes();
            repaint();
        }
        return true;
    }

    public void run()
    {
        for(Thread thread = Thread.currentThread(); engine == thread;)
        {
            moveNodes();
            repaint();
            try
            {
                Thread.sleep(sleeptime);
            }
            catch(InterruptedException _ex) { }
        }

    }

    public void start()
    {
        if(engine == null)
        {
            engine = new Thread(this);
            engine.start();
        }
        Dimension dimension = size();
        center.x = dimension.width / 2;
        center.y = dimension.height / 8;
    }

    public void stop()
    {
        if(engine != null && engine.isAlive())
            engine.stop();
        engine = null;
    }

    static final int XROOTSTEP = 128;
    static final int XSTEP = 16;
    static final int YSTEP = 32;
    final Color NODECOLOR = new Color(250, 220, 100);
    final Color LINECOLOR;
    final Color EDGECOLOR;
    final Color ROOTCOLOR = new Color(255, 100, 100);
    final Color PICKCOLOR;
    final Color BALLCOLOR = new Color(110, 120, 255);
    final Color FINDCOLOR;
    int sleeptime;
    boolean inverse;
    BTApplet applet;
    Thread engine;
    BTTree tree;
    Point center;
    BTNode picknode;
    BTNode prevpick;
    BTNode nextpick;
    BTNode treenode;
    BTNode nextnode;
    Image image;
    int traversal;
    Graphics bg;
}

⌨️ 快捷键说明

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