Java 按字母顺序打印树数据结构

Java 按字母顺序打印树数据结构,java,algorithm,data-structures,tree,Java,Algorithm,Data Structures,Tree,作为数据结构和算法的练习,我有一个很大的任务要做,其中一部分是修改这个树的数据结构,以按字母顺序打印树。我不会发布整个任务,因为它是巨大的。我被最后一部分卡住了,它要求我修改给定的树数据结构,以按字母顺序打印树。我坚持了几天,根本不知道怎么做。任何帮助都将不胜感激,谢谢。我的意见是,我必须以某种方式修改printTreeCursive()方法 例如,当前数据结构将打印如下树: c: d c b a c: a b c d (最后打印添加的第一个子项) 其中c:是根,d c是他的孩子 但我应该把

作为数据结构和算法的练习,我有一个很大的任务要做,其中一部分是修改这个树的数据结构,以按字母顺序打印树。我不会发布整个任务,因为它是巨大的。我被最后一部分卡住了,它要求我修改给定的树数据结构,以按字母顺序打印树。我坚持了几天,根本不知道怎么做。任何帮助都将不胜感激,谢谢。我的意见是,我必须以某种方式修改printTreeCursive()方法

例如,当前数据结构将打印如下树:

c: d c b a
c: a b c d
(最后打印添加的第一个子项)

其中c:是根,d c是他的孩子

但我应该把它改成这样:

c: d c b a
c: a b c d
以下是数据结构:

public class SLLTree<E> implements Tree<E> {

    // SLLNode is the implementation of the Node interface
    class SLLNode<P> implements Node<P> {

        // Holds the links to the needed nodes
        SLLNode<P> parent, sibling, firstChild;

        // Hold the data
        P element;

        public SLLNode(P o) {
            element = o;
            parent = sibling = firstChild = null;
        }

        public P getElement() {
            return element;
        }

        public void setElement(P o) {
            element = o;
        }

    }

    protected SLLNode<E> root;

    public SLLTree() {
        root = null;
    }

    public Node<E> root() {
        return root;
    }

    public Tree.Node<E> parent(Tree.Node<E> node) {
        return ((SLLNode<E>) node).parent;
    }

    public int childCount(Tree.Node<E> node) {
        SLLNode<E> tmp = ((SLLNode<E>) node).firstChild;
        int num = 0;
        while (tmp != null) {
            tmp = tmp.sibling;
            num++;
        }
        return num;
    }

    public void makeRoot(E elem) {
        root = new SLLNode<E>(elem);
    }

    public Node<E> addChild(Node<E> node, E elem) {
        SLLNode<E> tmp = new SLLNode<E>(elem);
        SLLNode<E> curr = (SLLNode<E>) node;
        tmp.sibling = curr.firstChild;
        curr.firstChild = tmp;
        tmp.parent = curr;
        return tmp;
    }

    public void remove(Tree.Node<E> node) {
        SLLNode<E> curr = (SLLNode<E>) node;
        if (curr.parent != null) {
            if (curr.parent.firstChild == curr) {
                // The node is the first child of its parent
                // Reconnect the parent to the next sibling
                curr.parent.firstChild = curr.sibling;
            } else {
                // The node is not the first child of its parent
                // Start from the first and search the node in the sibling list
                // and remove it
                SLLNode<E> tmp = curr.parent.firstChild;
                while (tmp.sibling != curr) {
                    tmp = tmp.sibling;
                }
                tmp.sibling = curr.sibling;
            }
        } else {
            root = null;
        }
    }

    class SLLTreeIterator<T> implements Iterator<T> {

        SLLNode<T> start, current;

        public SLLTreeIterator(SLLNode<T> node) {
            start = node;
            current = node;
        }

        public boolean hasNext() {
            return (current != null);
        }

        public T next() throws NoSuchElementException {
            if (current != null) {
                SLLNode<T> tmp = current;
                current = current.sibling;
                return tmp.getElement();
            } else {
                throw new NoSuchElementException();
            }
        }

        public void remove() {
            if (current != null) {
                current = current.sibling;
            }
        }
    }

    public Iterator<E> children(Tree.Node<E> node) {
        return new SLLTreeIterator<E>(((SLLNode<E>) node).firstChild);
    }

    void printTreeRecursive(Node<E> node, int level) {
        if (node == null)
            return;
        int i;
        SLLNode<E> tmp;

        for (i = 0; i < level; i++)
            System.out.print("  ");
        System.out.println(node.getElement().toString());
        tmp = ((SLLNode<E>) node).firstChild;

        while (tmp != null) {
            printTreeRecursive(tmp, level + 1);
            tmp = tmp.sibling;
        }
    }

    public void printTree() {
        printTreeRecursive(root, 0);
    }

    public int countMaxChildren() {
        return countMaxChildrenRecursive(root);
    }

    int countMaxChildrenRecursive(SLLNode<E> node) {
        int t = childCount(node);
        SLLNode<E> tmp = node.firstChild;
        while (tmp != null) {
            t = Math.max(t, countMaxChildrenRecursive(tmp));
            tmp = tmp.sibling;
        }
        return t;
    }

}
公共类SLLTree实现树{
//SLLNode是节点接口的实现
类SLLNode

实现节点

{ //保存指向所需节点的链接 SLLNode

父母、兄弟姐妹、第一个孩子; //保留数据 P元素; 公共SLLNode(PO){ 元素=o; 父级=同级=第一级子级=null; } 公共P getElement(){ 返回元素; } 公共无效集合元素(PO){ 元素=o; } } 保护SLLNode根; 公共SLLTree(){ root=null; } 公共节点根(){ 返回根; } public Tree.Node父节点(Tree.Node节点){ 返回((SLLNode)节点)。父节点; } public int childCount(Tree.Node节点){ SLLNode tmp=((SLLNode)node).firstChild; int num=0; while(tmp!=null){ tmp=tmp.sibling; num++; } 返回num; } 公共void makeRoot(E元素){ 根=新SLLNode(元素); } 公共节点addChild(节点节点,E元素){ SLLNode tmp=新SLLNode(elem); SLLNode curr=(SLLNode)节点; tmp.sibling=curr.firstChild; curr.firstChild=tmp; tmp.parent=curr; 返回tmp; } 删除公共void(Tree.Node节点){ SLLNode curr=(SLLNode)节点; if(curr.parent!=null){ if(curr.parent.firstChild==curr){ //该节点是其父节点的第一个子节点 //将父节点重新连接到下一个同级节点 curr.parent.firstChild=curr.sibling; }否则{ //该节点不是其父节点的第一个子节点 //从第一个节点开始,搜索同级列表中的节点 //并将其移除 SLLNode tmp=curr.parent.firstChild; while(tmp.sibling!=curr){ tmp=tmp.sibling; } tmp.sibling=curr.sibling; } }否则{ root=null; } } 类SLLTreeIterator实现迭代器{ SLL节点启动,电流; 公共SLLTreeIterator(SLLNode节点){ 开始=节点; 电流=节点; } 公共布尔hasNext(){ 返回(当前!=null); } public T next()抛出NoTouchElementException{ 如果(当前!=null){ SLLNode tmp=电流; current=current.sibling; 返回tmp.getElement(); }否则{ 抛出新的NoTouchElementException(); } } 公共空间删除(){ 如果(当前!=null){ current=current.sibling; } } } 公共迭代器子项(Tree.Node节点){ 返回新的SLLTreeIterator(((SLLNode)节点).firstChild; } void printTreeCursive(节点,int级别){ if(node==null) 返回; int i; sllnodetmp; 对于(i=0;i

公共接口树{
////存取器////////////
public Tree.Node root();
public Tree.Node父节点(Tree.Node节点);
public int childCount(Tree.Node节点);
////变压器////////////
公共无效生成根(E元素);
public Tree.Node addChild(Tree.Node Node,E elem);
公共作废删除(Tree.Node);
////迭代器////////////
公共迭代器子项(Tree.Node节点);
//树节点的内部接口////////
公共接口节点{
公共E getElement();
公共无效集合元素(E元素);
}
}
公共类SLLTreeTest{
公共静态void main(字符串[]args){
树。节点a、b、c、d;
SLLTree t=新的SLLTree();
t、 makeRoot(“C:”);
a=t.addChild(t.root,“程序文件”);
b=t.addChild(a,“代码块”);
c=t.addChild(b,“codeblocks.dll”);
c=t.addChild(b,“codeblocks.exe”);
b=t.addChild(a,“Nodepad++”);
c=t.addChild(b,“langs.xml”);
d=c;
c=t.addChild(b,“readme.txt”);
c=t.addChild(b,“notepad++.exe”);
a=t.addChild(t.root,“用户”);
b=t.addChild(a,“Darko”);
c=t.addChild(b,“桌面”);
c=t.addChild(b,“下载”);
c=t.addChild(b,“我的文件”);
C