// Lab35ast.java // The Graphical Tree Program // Student Version import java.awt.*; import java.awt.event.*; public class Lab35ast { public static void main(String args[]) { GfxApp gfx = new GfxApp(); gfx.setSize(800,600); gfx.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}}); gfx.show(); } } class GfxApp extends Frame { private int td = 500; // time delay to slow down graphics display String title = ""; String oldTitle = ""; String process = ""; String oldProcess = ""; String output = ""; public void paint (Graphics g) { g.setFont(new Font("ARIAL",Font.BOLD,20)); displayTitle(g,"DRAWING A BINARY TREE"); GfxTreeNode eins=new GfxTreeNode(g,380,100,5,150); eins.enterData(g,'M',5); eins.drawLeftNull(g,5); eins.drawRightNull(g,5); eins.drawRootPointer(g,5); GfxTreeNode zwei=new GfxTreeNode(g,230,150,2,150); zwei.enterData(g,'G',2); zwei.drawLeftNull(g,2); zwei.drawRightNull(g,2); eins.drawLeftNull(g,9); eins.drawLeftLink(g,zwei,5); GfxTreeNode drei=new GfxTreeNode(g,80,200,5,150); drei.enterData(g,'D',5); drei.drawLeftNull(g,5); drei.drawRightNull(g,5); zwei.drawLeftNull(g,9); zwei.drawLeftLink(g,drei,2); GfxTreeNode vier=new GfxTreeNode(g,530,150,2,150); vier.enterData(g,'S',2); vier.drawLeftNull(g,2); vier.drawRightNull(g,2); eins.drawRightNull(g,9); eins.drawRightLink(g,vier,5); GfxTreeNode funf=new GfxTreeNode(g,480,200,5,150); funf.enterData(g,'P',5); funf.drawLeftNull(g,5); funf.drawRightNull(g,5); vier.drawLeftNull(g,9); vier.drawLeftLink(g,funf,2); GfxTreeNode sechs=new GfxTreeNode(g,30,250,2,150); sechs.enterData(g,'A',2); sechs.drawLeftNull(g,2); sechs.drawRightNull(g,2); drei.drawLeftNull(g,9); drei.drawLeftLink(g,sechs,5); GfxTreeNode sieb=new GfxTreeNode(g,130,250,2,150); sieb.enterData(g,'F',2); sieb.drawLeftNull(g,2); sieb.drawRightNull(g,2); drei.drawRightNull(g,9); drei.drawRightLink(g,sieb,5); GfxTreeNode B8B8=new GfxTreeNode(g,530,250,2,150); B8B8.enterData(g,'Q',2); B8B8.drawLeftNull(g,2); B8B8.drawRightNull(g,2); funf.drawRightNull(g,9); funf.drawRightLink(g,B8B8,5); GfxTreeNode neun=new GfxTreeNode(g,280,200,5,150); neun.enterData(g,'J',5); neun.drawLeftNull(g,5); neun.drawRightNull(g,5); zwei.drawRightNull(g,9); zwei.drawRightLink(g,neun,2); GfxTreeNode zehn=new GfxTreeNode(g,230,250,2,150); zehn.enterData(g,'H',2); zehn.drawLeftNull(g,2); zehn.drawRightNull(g,2); neun.drawLeftNull(g,9); neun.drawLeftLink(g,zehn,5); GfxTreeNode elf=new GfxTreeNode(g,330,250,2,150); elf.enterData(g,'K',2); elf.drawLeftNull(g,2); elf.drawRightNull(g,2); neun.drawRightNull(g,9); neun.drawRightLink(g,elf,5); GfxTreeNode zwolf=new GfxTreeNode(g,680,200,5,150); zwolf.enterData(g,'W',5); zwolf.drawLeftNull(g,5); zwolf.drawRightNull(g,5); vier.drawRightNull(g,9); vier.drawRightLink(g,zwolf,2); GfxTreeNode dzeh=new GfxTreeNode(g,630,250,2,150); dzeh.enterData(g,'T',2); dzeh.drawLeftNull(g,2); dzeh.drawRightNull(g,2); zwolf.drawLeftNull(g,9); zwolf.drawLeftLink(g,dzeh,5); GfxTreeNode vzeh=new GfxTreeNode(g,430,250,2,150); vzeh.enterData(g,'N',2); vzeh.drawLeftNull(g,2); vzeh.drawRightNull(g,2); funf.drawLeftNull(g,9); funf.drawLeftLink(g,vzeh,5); GfxTreeNode fzeh=new GfxTreeNode(g,730,250,2,150); fzeh.enterData(g,'Z',2); fzeh.drawLeftNull(g,2); fzeh.drawRightNull(g,2); zwolf.drawRightNull(g,9); zwolf.drawRightLink(g,fzeh,5); displayTitle(g,"Inorder Traversal"); eins.drawPointer(g,'P',5); displayProcess(g,"Start At The Root"); movePointer(g,eins,zwei); displayProcess(g,"Traverse Left"); movePointer(g,zwei,drei); displayProcess(g,"Traverse Left"); movePointer(g,drei,sechs); displayProcess(g,"Display Data"); displayOutput(g,"A"); movePointer(g,sechs,drei); displayProcess(g,"Back to Parent"); displayOutput(g,"A, D"); movePointer(g,drei,sieb); displayProcess(g,"Traverse Right"); displayProcess(g,"Display Data"); displayOutput(g,"A, D, F"); movePointer(g,sieb,drei); displayProcess(g,"Back to Parent"); movePointer(g,drei,zwei); displayProcess(g,"Back to Parent"); displayOutput(g,"A, D, F, G"); movePointer(g,zwei,neun); displayProcess(g,"Traverse Right"); movePointer(g,neun,zehn); displayProcess(g,"Traverse Left"); displayOutput(g,"A, D, F, G, H"); movePointer(g,zehn,neun); displayProcess(g,"Back to Parent"); displayOutput(g,"A, D, F, G, H, J"); movePointer(g,neun,elf); displayProcess(g,"Traverse Right"); displayOutput(g,"A, D, F, G, H, J, K"); movePointer(g,elf,neun); displayProcess(g,"Back to Parent"); movePointer(g,neun,zwei); displayProcess(g,"Back to Parent"); movePointer(g,zwei,eins); displayProcess(g,"Back to Parent"); displayOutput(g,"A, D, F, G, H, J, K, M"); movePointer(g,eins,vier); displayProcess(g,"Traverse Right"); movePointer(g,vier,funf); displayProcess(g,"Traverse Left"); movePointer(g,funf,vzeh); displayProcess(g,"Traverse Left"); displayOutput(g,"A, D, F, G, H, J, K, M, N"); movePointer(g,vzeh,funf); displayProcess(g,"Back to Parent"); displayOutput(g,"A, D, F, G, H, J, K, M, N, P"); movePointer(g,funf,B8B8); displayProcess(g,"Traverse Right"); displayOutput(g,"A, D, F, G, H, J, K, M, N, P, Q"); movePointer(g,B8B8,funf); displayProcess(g,"Back to Parent"); movePointer(g,funf,vier); displayProcess(g,"Back to Parent"); displayOutput(g,"A, D, F, G, H, J, K, M, N, P, Q, S"); movePointer(g,vier,zwolf); displayProcess(g,"Traverse Right"); movePointer(g,zwolf,dzeh); displayProcess(g,"Traverse Left"); displayOutput(g,"A, D, F, G, H, J, K, M, N, P, Q, S, T"); movePointer(g,dzeh,zwolf); displayProcess(g,"Back to Parent"); displayOutput(g,"A, D, F, G, H, J, K, M, N, P, Q, S, T, W"); movePointer(g,zwolf,fzeh); displayProcess(g,"Traverse Right"); displayOutput(g,"A, D, F, G, H, J, K, M, N, P, Q, S, T, W, Z"); displayProcess(g,"finished"); } public void displayTitle(Graphics g, String t) { oldTitle = title; title = t; g.setColor(Color.white); g.drawString(oldTitle,50,50); g.setColor(Color.black); g.drawString(title,50,50); } public void displayProcess(Graphics g, String p) { oldProcess = process; process = p; g.setColor(Color.white); g.drawString(oldProcess,100,500); g.setColor(Color.black); g.drawString(process,100,500); } public void displayOutput(Graphics g, String output) { g.setColor(Color.black); g.drawString(output,100,570); } public void movePointer(Graphics g, GfxTreeNode oldTreeNode, GfxTreeNode newTreeNode) { oldTreeNode.drawPointer(g,'P',9); newTreeNode.drawPointer(g,'P',0); } }