ludc
2024-09-14 36c2449aec5b51e5ed4e5c6841154b746060e09a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.vci.client.ui.util;
 
import java.util.Enumeration;
import java.util.NoSuchElementException;
 
import javax.swing.tree.TreeNode;
 
 
public class PostorderEnumeration implements Enumeration<TreeNode> {
    static public final Enumeration<TreeNode> EMPTY_ENUMERATION = new Enumeration<TreeNode>() {
        public boolean hasMoreElements() {
            return false;
        }
 
        public TreeNode nextElement() {
            throw new NoSuchElementException("No more elements");
        }
    };
    
    protected TreeNode root;
    protected Enumeration<TreeNode> children;
    protected Enumeration<TreeNode> subtree;
 
    @SuppressWarnings("unchecked")
    public PostorderEnumeration(TreeNode rootNode) {
        super();
        root = rootNode;
        children = root.children();
        subtree = EMPTY_ENUMERATION;
    }
 
    public boolean hasMoreElements() {
        return root != null;
    }
 
    public TreeNode nextElement() {
        TreeNode retval;
 
        if (subtree.hasMoreElements()) {
            retval = subtree.nextElement();
        } else if (children.hasMoreElements()) {
            subtree = new PostorderEnumeration(
                    (TreeNode) children.nextElement());
            retval = subtree.nextElement();
        } else {
            retval = root;
            root = null;
        }
 
        return retval;
    }
 
} // End of class PostorderEnumeration