Java JTree Node sortTreeDepthFirst(DefaultMutableTreeNode root, Comparator comparator)

Here you can find the source of sortTreeDepthFirst(DefaultMutableTreeNode root, Comparator comparator)

Description

sort Tree Depth First

License

Open Source License

Declaration

public static void sortTreeDepthFirst(DefaultMutableTreeNode root,
            Comparator<DefaultMutableTreeNode> comparator) 

Method Source Code


//package com.java2s;
import java.util.Comparator;
import java.util.Enumeration;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode;

public class Main {
    public static void sortTreeDepthFirst(DefaultMutableTreeNode root,
            Comparator<DefaultMutableTreeNode> comparator) {
        Enumeration e = root.depthFirstEnumeration();
        while (e.hasMoreElements()) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
            if (!node.isLeaf()) {
                sortNode(node, comparator);
            }// ww w.  ja v  a2  s  . c  om
        }
    }

    public static void sortNode(DefaultMutableTreeNode parent, Comparator<DefaultMutableTreeNode> comparator) {
        int n = parent.getChildCount();
        for (int i = 0; i < n - 1; i++) {
            int min = i;
            for (int j = i + 1; j < n; j++) {
                if (comparator.compare((DefaultMutableTreeNode) parent.getChildAt(min),
                        (DefaultMutableTreeNode) parent.getChildAt(j)) > 0) {
                    min = j;
                }
            }
            if (i != min) {
                MutableTreeNode a = (MutableTreeNode) parent.getChildAt(i);
                MutableTreeNode b = (MutableTreeNode) parent.getChildAt(min);
                parent.insert(b, i);
                parent.insert(a, min);
            }
        }
    }
}

Related

  1. RemoveChildren(DefaultMutableTreeNode node)
  2. searchForNode(TreeNode node, String query)
  3. setTreeSelectedNode(JTree tree, TreeNode node)
  4. sort(DefaultMutableTreeNode node)
  5. sortNode(DefaultMutableTreeNode parent, Comparator comparator)
  6. sortTreeNode(DefaultMutableTreeNode root)
  7. sortTreeNodesAlphaNumeric(final DefaultMutableTreeNode node)
  8. toArray(final TreeNode node)
  9. treeNodeTest(DefaultMutableTreeNode n)