/* * MyData1.java * */ import net.datastructures.*; import java.util.Iterator; public class MyData1 { char A; int B; /* constructor */ public MyData1 (char a) { A = a; B = 0; } public static void printTree(LinkedBinaryTree t) { int i, j; // get all nodes BTPosition[] node = new BTPosition[31]; node[0] = (BTPosition)t.root(); for (i=0;i<15;i++) { if (node[i] == null ) { node[i*2+1] = null; node[i*2+2] = null; } else { node[i*2+1] = (BTPosition)node[i].getLeft(); node[i*2+2] = (BTPosition)node[i].getRight(); } } int space = 32; int intSpace; int nodeOneLine = 1; int nodeNum = 0; int cursor = 0; // edgeLine is initialized as a string of 64 spaces. char[] edgeLine = new char[64]; for (j=0;j < 64; j++) edgeLine[j] =' '; // print the nodes for (i = 0; i < 31; i++) { if (node[i] == null) intSpace = 0; else intSpace = 1; if (nodeNum == 0) { for (j=0; j < space - intSpace; j++) System.out.print(' '); cursor += space; } else { for (j=0; j < space*2 - intSpace; j++) System.out.print(' '); cursor += space *2; } if (node[i] != null) { System.out.print( ((MyData1)node[i].element()).A ); if ( t.hasLeft(node[i]) ) edgeLine[cursor - 1 - space / 4] ='/'; if ( t.hasRight(node[i]) ) edgeLine[cursor -1 + space / 4] ='\\'; } nodeNum ++; if (nodeNum==nodeOneLine) { System.out.print("\n"); System.out.println(edgeLine); for (j=0;j < 64; j++) edgeLine[j] =' '; space /= 2; nodeOneLine *=2; nodeNum = 0; cursor = 0; } } } }