📄 persongroup.java
字号:
case 4: // '\004'
if(curIn > 14 || treeArray[2 * curIn + 1] == null && treeArray[2 * curIn + 2] == null)
{
note = "Mozemo da obrisemo ovaj cvor!";//"Will delete node without complication";
codePart = 5;
return;
}
if(treeArray[2 * curIn + 1] == null)
{
note = "Zamenjujemo cvor sa njegovim DESNIM podstablom!";//"Will replace node with its right subtree";
codePart = 6;
return;
}
if(treeArray[2 * curIn + 2] == null)
{
note = "Zamenjujemo cvor sa njegovim LEVIM podstablom!";//"Will replace node with its left subtree";
codePart = 7;
return;
} else
{
successor = inorderSuccessor(curIn);
note = "Zamenjujemo cvor sa " + treeArray[successor].getHeight();
codePart = 8;
return;
}
case 5: // '\005'
treeArray[curIn] = null;
note = "Cvor je obrisan!";
drawMode = 2;
codePart = 10;
return;
case 6: // '\006'
treeArray[curIn] = null;
moveUpSubTree(1, curIn);
note = "Cvor je zamenjujen sa njegovim DESNIM podstablom!";
drawMode = 2;
codePart = 10;
return;
case 7: // '\007'
treeArray[curIn] = null;
moveUpSubTree(0, curIn);
note = "Cvor je zamenjujen sa njegovim LEVIM podstablom!";//"Node was replaced by its left subtree";
drawMode = 2;
codePart = 10;
return;
case 8: // '\b'
treeArray[curIn] = treeArray[successor];
int j = 2 * successor + 2;
if(successor < 15 && treeArray[j] != null)
{
int k = treeArray[successor].getHeight();
note = "Menjamo " + k + " sa njegovim DESNIM podstablom!";
drawMode = 0;
codePart = 9;
return;
} else
{
treeArray[successor] = null;
note = "Cvor je zamenjen svojim naslednikom!";
drawMode = 2;
codePart = 10;
return;
}
case 9: // '\t'
moveUpSubTree(1, successor);
note = "Cvor je zamenjen u 2 koraka!";
drawMode = 2;
codePart = 10;
return;
case 10: // '\n'
note = "Izaberite neku od operacija!";
oldArrow = curInOld;
curInOld = 0;
codePart = 1;
return;
}
}
public void moveUpSubTree(int i, int j)
{
if(j > 14 || j < 0)
return;
int k;
if(i == 1)
k = 2 * j + 2;
else
k = 2 * j + 1;
byte byte0;
if(k > 0 && k < 3)
byte0 = 1;
else
if(k > 2 && k < 7)
byte0 = 2;
else
if(k > 6 && k < 15)
byte0 = 3;
else
byte0 = 4;
int j1 = k;
int l = j1;
int i1 = 1;
for(int j2 = byte0; j2 < 5; j2++)
{
for(int k2 = 0; k2 < i1; k2++)
{
int l1 = (l - 1) / 2;
int k1 = i1 - k2 - 1;
int i2;
if(i == 1)
i2 = l1 - (k1 + 1) / 2;
else
i2 = l1 + (k2 + 1) / 2;
treeArray[i2] = treeArray[l];
if(j2 == 4)
treeArray[l] = null;
l++;
}
j1 = 2 * j1 + 1;
l = j1;
i1 *= 2;
}
}
public int inorderSuccessor(int i)
{
int j = i;
for(int k = 2 * i + 2; k < 31 && treeArray[k] != null; k = 2 * k + 1)
j = k;
return j;
}
public void traverse()
{
if(opMode != 5)
{
opMode = 5;
codePart = 1;
}
switch(codePart)
{
case 1: // '\001'
visitIndex = 0;
note = "Zapocinjemo \"INORDER\" obilazak stabla!";
curIn = 0;
oldArrow = curInOld;
curInOld = 0;
drawMode = 0;
codePart = 2;
return;
case 2: // '\002'
note = "Proveravamo LEVO dete.";
codePart = 3;
return;
case 3: // '\003'
if(curIn > 14 || treeArray[2 * curIn + 1] == null)
{
note = "Upisujemo ovaj cvor!";
codePart = 4;
return;
} else
{
theStack.push(curIn);
curIn = 2 * curIn + 1;
oldArrow = curInOld;
curInOld = curIn;
note = "Proveravamo LEVO dete.";
codePart = 3;
return;
}
case 4: // '\004'
visitArray[visitIndex++] = treeArray[curIn].getHeight();
note = "Proveravamo DESNO dete.";
codePart = 5;
return;
case 5: // '\005'
if(curIn > 14 || treeArray[2 * curIn + 2] == null)
{
note = "Prelazimo na koren poslednjeg podstabla.";
codePart = 6;
return;
} else
{
curIn = 2 * curIn + 2;
oldArrow = curInOld;
curInOld = curIn;
note = "Proveravamo LEVO dete.";
codePart = 3;
return;
}
case 6: // '\006'
if(theStack.isEmpty())
{
note = "INORDER obilazak je zavrsen!";
codePart = 7;
return;
} else
{
curIn = theStack.pop();
oldArrow = curInOld;
curInOld = curIn;
note = "Upisujemo ovaj cvor!";
codePart = 4;
return;
}
case 7: // '\007'
note = "Izaberite neku od operacija!";
oldArrow = curInOld;
curInOld = 0;
codePart = 1;
return;
}
}
public void drawOneNode(Graphics g, int i)
{
if(treeArray[i] == null)
return;
int j = treeArray[i].getHeight();
Color color = treeArray[i].getColor();
int k = i % 2;
int l = 15;
byte byte0 = 0;
int i1 = -1;
if(i > 0 && i < 3)
{
l = 7 + (i - 1) * 16;
byte0 = 1;
i1 = k != 1 ? l - 8 : l + 8;
} else
if(i > 2 && i < 7)
{
l = 3 + (i - 3) * 8;
byte0 = 2;
i1 = k != 1 ? l - 4 : l + 4;
} else
if(i > 6 && i < 15)
{
l = 1 + (i - 7) * 4;
byte0 = 3;
i1 = k != 1 ? l - 2 : l + 2;
} else
if(i > 14 && i < 31)
{
l = (i - 15) * 2;
byte0 = 4;
i1 = k != 1 ? l - 1 : l + 1;
}
int j1 = 10 + (l * 26) / 2;
int k1 = 70 + byte0 * 40;
int l1 = 10 + (i1 * 26) / 2;
int i2 = 70 + (byte0 - 1) * 40;
if(byte0 > 0)
{
g.setColor(Color.black);
g.drawLine(j1 + 10, k1 + 10, l1 + 10, i2 + 10);
}
g.setColor(color);
g.fillOval(j1, k1, 20, 20);
g.setColor(Color.black);
g.drawOval(j1, k1, 20, 20);
if(j < 10)
{
g.drawString(String.valueOf(j), j1 + 7, (k1 + 20) - 5);
return;
} else
{
g.drawString(String.valueOf(j), j1 + 4, (k1 + 20) - 5);
return;
}
}
/**
* @param g
*/
/**
* @param g
*/
public void draw(Graphics g)
{
int i = !drawAll ? drawMode : 2;
switch(i)
{
case 0: // '\0'
default:
break;
case 1: // '\001'
for(int j = curInOld; j > 0; j = (j - 1) / 2)
drawOneNode(g, j);
drawOneNode(g, 0);
break;
case 2: // '\002'
g.setColor(Color.lightGray);//pozadinski panel
g.fillRect(0, 0, 450, 310);
for(int k = 30; k >= 0; k--)
drawOneNode(g, k);
break;
}
g.setColor(Color.white);
g.fillRect(10, 270, 430, 22);
g.setColor(Color.red);//boja slova za obavestavanje
g.setFont(f);
g.drawString(note, 16, 285); //tekst koji opisuje operacije
g.setColor(Color.lightGray);
g.fillRect(10, 292, 430, 22);
g.setColor(Color.black);
String s = "";
for(int l = 0; l < visitIndex; l++)
s = s + visitArray[l] + " ";
g.drawString(s, 16, 305);
drawArrow(g, oldArrow, false);
drawArrow(g, curInOld, true);
drawAll = true;
}
public void drawArrow(Graphics g, int i, boolean flag)
{
if(treeArray[i] == null)
return;
int j = 15;
byte byte0 = 0;
if(i > 0 && i < 3)
{
j = 7 + (i - 1) * 16;
byte0 = 1;
} else
if(i > 2 && i < 7)
{
j = 3 + (i - 3) * 8;
byte0 = 2;
} else
if(i > 6 && i < 15)
{
j = 1 + (i - 7) * 4;
byte0 = 3;
} else
if(i > 14 && i < 31)
{
j = (i - 15) * 2;
byte0 = 4;
}
int k = 10 + (j * 26) / 2;
int l = 70 + byte0 * 40;
if(flag)
g.setColor(Color.red);
else
g.setColor(Color.lightGray);
int i1 = k + 10;
int j1 = l - 2;
byte byte1 = 20;
g.drawLine(i1, j1, i1, j1 - byte1);
g.drawLine(i1 - 1, j1, i1 - 1, j1 - byte1);
g.drawLine(i1, j1, i1 - 3, j1 - 6);
g.drawLine(i1 - 1, j1, i1 - 4, j1 - 6);
g.drawLine(i1, j1, i1 + 3, j1 - 6);
g.drawLine(i1 - 1, j1, i1 + 2, j1 - 6);
}
private Thread runner;
Font f = new Font("Tahoma", Font.BOLD, 12);
private final int appletWidth = 440;
private final int appletHeight = 310;
private final int maxHeight = 200;
private final int topMargin = 70;
private final int leftMargin = 7;
private final int textHeight = 13;
private final int nodeDiameter = 20;
private final int levelSeparation = 40;
private final int horizSeparation = 26;
private final int noteBoxTop = 15;
private final int noteBoxHeight = 25;
private final int noteBoxWidth = 200;
private final int visitBoxTop = 280;
private final int visitBoxHeight = 25;
private final int visitBoxWidth = 430;
private final int ASIZE = 31;
private final int MAX_KEY = 99;
private person treeArray[];
stack theStack;
private int filledNodes;
private String note;
private boolean isRand;
private int value;
private int codePart;
private int opMode;
private int curIn;
private int curInOld;
private int oldArrow;
private int visitArray[];
private int visitIndex;
private int successor;
private boolean drawAll;
private int drawMode;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -