45 |
import java.io.ObjectOutputStream; |
import java.io.ObjectOutputStream; |
46 |
import java.io.Serializable; |
import java.io.Serializable; |
47 |
import java.util.ArrayList; |
import java.util.ArrayList; |
48 |
|
import java.util.Collections; |
49 |
import java.util.Enumeration; |
import java.util.Enumeration; |
50 |
|
import java.util.LinkedList; |
51 |
|
import java.util.NoSuchElementException; |
52 |
import java.util.Stack; |
import java.util.Stack; |
53 |
import java.util.Vector; |
import java.util.Vector; |
54 |
|
|
55 |
|
|
56 |
/** |
/** |
57 |
* DefaultMutableTreeNode |
* DefaultMutableTreeNode |
58 |
* |
* |
59 |
* @author Andrew Selkirk |
* @author Andrew Selkirk |
60 |
|
* @author Robert Schuster (robertschuster@fsfe.org) |
61 |
*/ |
*/ |
62 |
public class DefaultMutableTreeNode |
public class DefaultMutableTreeNode |
63 |
implements Cloneable, MutableTreeNode, Serializable |
implements Cloneable, MutableTreeNode, Serializable |
358 |
*/ |
*/ |
359 |
public void removeFromParent() |
public void removeFromParent() |
360 |
{ |
{ |
361 |
// FIXME: IS this implementation really correct ? |
parent.remove(this); |
362 |
parent = null; |
parent = null; |
363 |
} |
} |
364 |
|
|
661 |
*/ |
*/ |
662 |
public Enumeration preorderEnumeration() |
public Enumeration preorderEnumeration() |
663 |
{ |
{ |
664 |
return null; // TODO: Implement me. |
return new PreorderEnumeration(this); |
665 |
} |
} |
666 |
|
|
667 |
/** |
/** |
671 |
*/ |
*/ |
672 |
public Enumeration postorderEnumeration() |
public Enumeration postorderEnumeration() |
673 |
{ |
{ |
674 |
return null; // TODO: Implement me. |
return new PostorderEnumeration(this); |
675 |
} |
} |
676 |
|
|
677 |
/** |
/** |
681 |
*/ |
*/ |
682 |
public Enumeration breadthFirstEnumeration() |
public Enumeration breadthFirstEnumeration() |
683 |
{ |
{ |
684 |
return null; // TODO: Implement me. |
return new BreadthFirstEnumeration(this); |
685 |
} |
} |
686 |
|
|
687 |
/** |
/** |
918 |
if (parent == null) |
if (parent == null) |
919 |
return null; |
return null; |
920 |
|
|
921 |
|
// TODO: Fix implementation. |
922 |
return null; |
return null; |
923 |
//return parent.getChildAfter(this); |
//return parent.getChildAfter(this); |
924 |
} |
} |
932 |
{ |
{ |
933 |
if (parent == null) |
if (parent == null) |
934 |
return null; |
return null; |
935 |
|
|
936 |
|
// TODO: Fix implementation. |
937 |
return null; |
return null; |
938 |
//return parent.getChildBefore(this); |
//return parent.getChildBefore(this); |
939 |
} |
} |
958 |
|
|
959 |
return count; |
return count; |
960 |
} |
} |
961 |
|
|
962 |
|
/** Provides an enumeration of a tree in breadth-first traversal |
963 |
|
* order. |
964 |
|
*/ |
965 |
|
static class BreadthFirstEnumeration implements Enumeration |
966 |
|
{ |
967 |
|
|
968 |
|
LinkedList queue = new LinkedList(); |
969 |
|
|
970 |
|
BreadthFirstEnumeration(TreeNode node) |
971 |
|
{ |
972 |
|
queue.add(node); |
973 |
|
} |
974 |
|
|
975 |
|
public boolean hasMoreElements() |
976 |
|
{ |
977 |
|
return !queue.isEmpty(); |
978 |
|
} |
979 |
|
|
980 |
|
public Object nextElement() |
981 |
|
{ |
982 |
|
if(queue.isEmpty()) |
983 |
|
throw new NoSuchElementException("No more elements left."); |
984 |
|
|
985 |
|
TreeNode node = (TreeNode) queue.removeFirst(); |
986 |
|
|
987 |
|
Enumeration children = node.children(); |
988 |
|
while (children.hasMoreElements()) |
989 |
|
queue.add(children.nextElement()); |
990 |
|
|
991 |
|
return node; |
992 |
|
} |
993 |
|
} |
994 |
|
|
995 |
|
/** Provides an enumeration of a tree traversing it |
996 |
|
* preordered. |
997 |
|
*/ |
998 |
|
static class PreorderEnumeration implements Enumeration |
999 |
|
{ |
1000 |
|
TreeNode next; |
1001 |
|
|
1002 |
|
Stack childrenEnums = new Stack(); |
1003 |
|
|
1004 |
|
PreorderEnumeration(TreeNode node) |
1005 |
|
{ |
1006 |
|
next = node; |
1007 |
|
childrenEnums.push(node.children()); |
1008 |
|
} |
1009 |
|
|
1010 |
|
public boolean hasMoreElements() |
1011 |
|
{ |
1012 |
|
return next != null; |
1013 |
|
} |
1014 |
|
|
1015 |
|
public Object nextElement() |
1016 |
|
{ |
1017 |
|
if( next == null ) |
1018 |
|
throw new NoSuchElementException("No more elements left."); |
1019 |
|
|
1020 |
|
Object current = next; |
1021 |
|
|
1022 |
|
Enumeration children = (Enumeration) childrenEnums.peek(); |
1023 |
|
|
1024 |
|
// Retrieves the next element. |
1025 |
|
next = traverse(children); |
1026 |
|
|
1027 |
|
return current; |
1028 |
|
} |
1029 |
|
|
1030 |
|
private TreeNode traverse(Enumeration children) |
1031 |
|
{ |
1032 |
|
// If more children are available step down. |
1033 |
|
if( children.hasMoreElements() ) |
1034 |
|
{ |
1035 |
|
TreeNode child = (TreeNode) children.nextElement(); |
1036 |
|
childrenEnums.push(child.children()); |
1037 |
|
|
1038 |
|
return child; |
1039 |
|
} |
1040 |
|
|
1041 |
|
// If no children are left, we return to a higher level. |
1042 |
|
childrenEnums.pop(); |
1043 |
|
|
1044 |
|
// If there are no more levels left, there is no next |
1045 |
|
// element to return. |
1046 |
|
if ( childrenEnums.isEmpty() ) |
1047 |
|
return null; |
1048 |
|
else |
1049 |
|
{ |
1050 |
|
return traverse((Enumeration) childrenEnums.peek()); |
1051 |
|
} |
1052 |
|
} |
1053 |
|
} |
1054 |
|
|
1055 |
|
/** Provides an enumeration of a tree traversing it |
1056 |
|
* postordered (= depth-first). |
1057 |
|
*/ |
1058 |
|
static class PostorderEnumeration implements Enumeration |
1059 |
|
{ |
1060 |
|
|
1061 |
|
Stack nodes = new Stack(); |
1062 |
|
Stack childrenEnums = new Stack(); |
1063 |
|
|
1064 |
|
PostorderEnumeration(TreeNode node) |
1065 |
|
{ |
1066 |
|
nodes.push(node); |
1067 |
|
childrenEnums.push(node.children()); |
1068 |
|
} |
1069 |
|
|
1070 |
|
public boolean hasMoreElements() |
1071 |
|
{ |
1072 |
|
return !nodes.isEmpty(); |
1073 |
|
} |
1074 |
|
|
1075 |
|
public Object nextElement() |
1076 |
|
{ |
1077 |
|
if( nodes.isEmpty() ) |
1078 |
|
throw new NoSuchElementException("No more elements left!"); |
1079 |
|
|
1080 |
|
Enumeration children = (Enumeration) childrenEnums.peek(); |
1081 |
|
|
1082 |
|
return traverse(children); |
1083 |
|
} |
1084 |
|
|
1085 |
|
private Object traverse(Enumeration children) |
1086 |
|
{ |
1087 |
|
if ( children.hasMoreElements() ) |
1088 |
|
{ |
1089 |
|
TreeNode node = (TreeNode) children.nextElement(); |
1090 |
|
nodes.push(node); |
1091 |
|
|
1092 |
|
Enumeration newChildren = node.children(); |
1093 |
|
childrenEnums.push(newChildren); |
1094 |
|
|
1095 |
|
return traverse(newChildren); |
1096 |
|
} |
1097 |
|
else |
1098 |
|
{ |
1099 |
|
childrenEnums.pop(); |
1100 |
|
|
1101 |
|
// Returns the node whose children |
1102 |
|
// have all been visited. (= postorder) |
1103 |
|
Object next = nodes.peek(); |
1104 |
|
nodes.pop(); |
1105 |
|
|
1106 |
|
return next; |
1107 |
|
} |
1108 |
|
} |
1109 |
|
|
1110 |
|
} |
1111 |
|
|
1112 |
} |
} |