原创误删,写了个N叉树,格式输出了下二叉树,抛砖引玉而已, 求各种实现方式
package org.benson.dto; import java.util.ArrayList; import java.util.List; /** * @author BenSon He * @email qing878@gmail.com ,qq 107966750 * @since 19/21/2012 */ public class Node { List<Node> list = new ArrayList<Node>();; private int value; private int level; public int getLevel() { return level; } public void setLevel(int level) { this.level = level; } public Node getNodeByIndex(int index) { return list.get(index); } public void addNode(Node node) { list.add(node); } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public int getSize() { return list.size(); } }
package org.benson.util; import org.benson.dto.Node; /** * @author BenSon He * @email qing878@gmail.com ,qq 107966750 * @since 19/21/2012 */ public class XTreeUtil { static int index; static String toStrArray[][]; public static void getXTree(Node node, int level, int nx) { level--; index = node.getValue(); for (int i = 0; i <= nx; i++) { // index=index+1; // System.out.println(index); Node chirdNode = new Node(); chirdNode.setValue(++index); node.addNode(chirdNode); node.setLevel(level); if (level > 0) getXTree(chirdNode, level, nx); } } /** * get node by level and index * * @param node * , start of node * @param level * @param index * , the index of chirdNode * @return */ public static Node getNode(Node node, int level, int index) { level--; if (level > 0) return getNode(node.getNodeByIndex(index), level, index); else return node.getNodeByIndex(index); } public static void displayTree(Node node, int level, int nx) { int indexLenth = getNode(node, level, nx).getValue() + 1; toStrArray = new String[level+1][indexLenth]; // init array for (int i = 0; i < toStrArray.length; i++) { for (int j = 0; j < toStrArray[i].length; j++) { toStrArray[i][j]=" "; } } toStrArray[level][toStrArray[0].length/2]=String.valueOf(node.getValue()); fillTree(node, level, toStrArray[0].length/2); for (int i = 0; i < toStrArray.length; i++) { for (int j = 0; j < toStrArray[i].length; j++) { System.out.print(toStrArray[i][j]); } System.out.println(); } System.out.println(); } public static void fillTree(Node node,int level,int index){ level--; int spaceCount=(int) Math.pow(2, level); Node leftNode=node.getNodeByIndex(0); toStrArray[level][index+spaceCount]=String.valueOf(leftNode.getValue()); Node rightNode=node.getNodeByIndex(1); toStrArray[level][index-spaceCount]=String.valueOf(rightNode.getValue()); if(level>0){ fillTree(leftNode, level, index+spaceCount); fillTree(rightNode, level, index-spaceCount); } } public static void main(String[] args) { Node node = new Node(); int level=4; int nIndex=1; // start with 0. 1 is 2 leaf node.setValue(1);// start value . root value XTreeUtil.getXTree(node, level, nIndex); // level and index .the level start element index is 0 displayTree(node,level, nIndex); } }
输出如下
31 30 28 27 24 23 21 20 16 15 13 12 9 8 6 5 29 26 22 19 14 11 7 4 25 18 10 3 17 2 1
可以改变nIndex实现N叉树结构
nIndex=1 二叉
nIndex= 2 三叉
只做了二叉的格式化输出,但是N叉格式化输出就没做了,觉得还是有点麻烦
写着玩而已,没去查其他实现,或许有BUG,求指出。
不喜勿喷
如果有兴趣贴上各种实现方式
类似应用,比如生成XML的递归 也不错
当然,递归在时间和空间上都不怎么好
View Code
package org.benson.another; import java.util.ArrayList; import java.util.List; /** * * @author BensonHe QQ277803242 * * TODO manage node and value.and format the node to xml file * */ public class XMLSimpleNode implements SimpleNode { private String nodeName; private String value; List<SimpleNode> childs; private String header; public XMLSimpleNode(String nodeName) { this.nodeName = nodeName; childs = new ArrayList<SimpleNode>(2); } public String getNodeName() { return nodeName; } /* * (non-Javadoc) * * @see org.benson.another.SimpleXMLFileUtil#setNodeName(java.lang.String) */ public void setNodeName(String nodeName) { this.nodeName = nodeName; } public String getValue() { return value; } /* * (non-Javadoc) * * @see org.benson.another.SimpleXMLFileUtil#setValue(java.lang.String) */ public void setValue(String value) { this.value = value; } /* * (non-Javadoc) * * @see org.benson.another.SimpleXMLFileUtil#addChild(org.benson.another.XMLItem) */ public void addChild(SimpleNode node) { childs.add(node); } public String startTag() { StringBuffer tagSart = new StringBuffer("<"); tagSart.append(nodeName); tagSart.append(">"); return tagSart.toString(); } public String endTag() { StringBuffer tagEnd = new StringBuffer("</"); tagEnd.append(nodeName); tagEnd.append(">"); return tagEnd.toString(); } public String getHeader() { return header; } /* * (non-Javadoc) * * @see org.benson.another.SimpleXMLFileUtil#setHeader(java.lang.String) */ public void setHeader(String header) { this.header = header; } /** * ToDo format the node to xml file */ public String formartXMLFile() { StringBuffer xmlFileBf = new StringBuffer(); xmlFileBf.append(this.getHeader()); return this.getNode(this, xmlFileBf).toString(); } /** * * @param node * @param xmlFileBf * @return get XML content by node */ private StringBuffer getNode(SimpleNode node, StringBuffer xmlFileBf) { XMLSimpleNode xmlNode = (XMLSimpleNode) node; XMLSimpleNode xmlNodeItem = null; xmlFileBf.append(xmlNode.startTag()); if (xmlNode.childs.size() != 0) { for (int i = 0; i < xmlNode.childs.size(); i++) { xmlNodeItem = (XMLSimpleNode) xmlNode.childs.get(i); getNode(xmlNodeItem, xmlFileBf); } } if (xmlNode.getValue() != null) xmlFileBf.append(xmlNode.getValue()); xmlFileBf.append(xmlNode.endTag()); return xmlFileBf; } public static void main(String[] args) { SimpleNode xmlNode = new XMLSimpleNode("root"); xmlNode.setHeader("<?xml version=\"1.0\" encoding=\"UTF-8\" ?> "); SimpleNode chridNode1 = new XMLSimpleNode("chrid1"); SimpleNode chridNode2 = new XMLSimpleNode("chrid2"); SimpleNode chridNode3 = new XMLSimpleNode("chrid3"); SimpleNode chridNode11 = new XMLSimpleNode("chrid11"); SimpleNode chridNode12 = new XMLSimpleNode("chrid12"); SimpleNode chridNode13 = new XMLSimpleNode("chrid13"); chridNode11.setValue("chridNode11 Value"); chridNode12.setValue("chridNode12 Value"); chridNode13.setValue("chridNode13 Value"); chridNode2.setValue("chridNode2 Value"); chridNode3.setValue("chridNode3 value"); chridNode1.addChild(chridNode11); chridNode1.addChild(chridNode12); chridNode1.addChild(chridNode13); xmlNode.addChild(chridNode1); xmlNode.addChild(chridNode2); xmlNode.addChild(chridNode3); System.out.println(xmlNode.formartXMLFile()); } }
输出效果如下
<?xml version="1.0" encoding="UTF-8" ?> <root><chrid1><chrid11>chridNode11 Value</chrid11><chrid12>chridNode12 Value</chrid12><chrid13>chridNode13 Value</chrid13></chrid1><chrid2></chrid2><chrid3></chrid3></root>