- 所有已实现的接口:
-
Serializable,Cloneable,MutableTreeNode,TreeNode
- 直接已知的子类:
-
JTree.DynamicUtilTreeNode
DefaultMutableTreeNode 是树数据结构中的通用节点。有关使用默认可变树节点的示例,请参阅The Java Tutorial中的How to Use Trees。
树节点最多可以有一个父节点和0个或多个子节点。 DefaultMutableTreeNode 提供了用于检查和修改节点的父节点和子节点以及用于检查节点所属树的操作。节点的树是从节点开始并沿着所有可能的父节点和子节点链接到达的所有节点的集合。没有父节点的节点是其树的根节点;没有子节点的节点是叶子节点。一棵树可以由许多子树组成,每个节点充当其自己子树的根节点。
此类提供了用于有效地按各种顺序遍历树或子树或遵循两个节点之间路径的枚举。 DefaultMutableTreeNode 还可以保存对用户对象的引用,其使用由用户决定。使用toString() 请求DefaultMutableTreeNode的字符串表示将返回其用户对象的字符串表示。
这不是一个线程安全的类。如果您打算在多个线程中使用 DefaultMutableTreeNode(或一组 TreeNodes),则需要自行同步。一个良好的约定是在树的根节点上同步。
虽然 DefaultMutableTreeNode 实现了 MutableTreeNode 接口并允许您添加任何 MutableTreeNode 实现,但并非所有 DefaultMutableTreeNode 方法都适用于所有 MutableTreeNodes 实现。特别是对于提供的一些枚举,使用其中一些方法假定 DefaultMutableTreeNode 仅包含 DefaultMutableNode 实例。无论添加了什么实现,所有 TreeNode/MutableTreeNode 方法都将按照定义的方式运行。
警告: 此类的序列化对象将不兼容未来的 Swing 版本。当前的序列化支持适用于短期存储或在运行相同 Swing 版本的应用程序之间的 RMI。从 1.4 版开始,已将所有 JavaBeans 的长期存储支持添加到 java.beans 包中。请参阅XMLEncoder。
- 参见:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected boolean如果节点能够有子节点,则为 true子节点数组,如果此节点没有子节点,则可能为 nullstatic final Enumeration<TreeNode> 一个始终为空的枚举。protected MutableTreeNode此节点的父节点,如果此节点没有父节点则为 nullprotected Object可选的用户对象 -
Constructor Summary
ConstructorsConstructorDescription创建一个没有父节点和子节点但允许有子节点的树节点。DefaultMutableTreeNode(Object userObject) 创建一个没有父节点、没有子节点但允许有子节点,并使用指定的用户对象进行初始化的树节点。DefaultMutableTreeNode(Object userObject, boolean allowsChildren) 创建一个没有父节点、没有子节点但使用指定的用户对象进行初始化的树节点,并仅在指定时允许有子节点。 -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(MutableTreeNode newChild) 从其父节点中移除newChild,并通过将其添加到此节点的子节点数组末尾,使其成为此节点的子节点。创建并返回一个以广度优先顺序遍历以此节点为根的子树的枚举。children()创建并返回此节点子节点的正序枚举。clone()重写以使克隆公开。创建并返回一个以深度优先顺序遍历以此节点为根的子树的枚举。boolean如果此节点允许有子节点,则返回 true。getChildAfter(TreeNode aChild) 返回此节点子节点数组中紧随aChild之后的子节点,aChild必须是此节点的子节点。getChildAt(int index) 返回此节点子节点数组中指定索引处的子节点。getChildBefore(TreeNode aChild) 返回此节点子节点数组中紧随aChild之前的子节点,aChild必须是此节点的子节点。int返回此节点的子节点数。intgetDepth()返回以此节点为根的树的深度 -- 从此节点到叶子节点的最长距离。返回此节点的第一个子节点。查找并返回是此节点的后代的第一个叶子节点 -- 可能是此节点或其第一个子节点的第一个叶子节点。int返回此节点子节点数组中指定子节点的索引。返回此节点的最后一个子节点。查找并返回是此节点的后代的最后一个叶子节点 -- 可能是此节点或其最后一个子节点的最后一个叶子节点。int返回是此节点的后代的所有叶子节点的总数。intgetLevel()返回此节点上面的级别数 -- 从根节点到此节点的距离。返回此节点之后的叶子节点,如果此节点是树中的最后一个叶子节点则返回 null。返回在此节点树的先序遍历中跟随此节点的节点。返回此节点在父节点的子节点数组中的下一个同级节点。返回此节点的父节点,如果此节点没有父节点则返回 null。TreeNode[]getPath()返回从根节点到达此节点的路径。protected TreeNode[]getPathToRoot(TreeNode aNode, int depth) 构建节点的父节点直到包括根节点,其中原始节点是返回数组中的最后一个元素。返回此节点之前的叶子节点,如果此节点是树中的第一个叶子节点则返回 null。返回在此节点树的先序遍历中跟随此节点的前一个节点。返回此节点在父节点的子节点数组中的前一个同级节点。getRoot()返回包含此节点的树的根节点。返回此节点和aNode的最近公共祖先。int返回此节点的兄弟节点数。返回此节点的用户对象。Object[]返回从根节点到达此节点的用户对象路径。voidinsert(MutableTreeNode newChild, int childIndex) 从其当前父节点中移除newChild(如果有父节点),将子节点的父节点设置为此节点,然后将子节点添加到此节点的子节点数组中的索引childIndex处。newChild不能为 null,也不能是此节点的祖先。booleanisLeaf()Returns true if this node has no children.booleanisNodeAncestor(TreeNode anotherNode) Returns true ifanotherNodeis an ancestor of this node -- if it is this node, this node's parent, or an ancestor of this node's parent.booleanisNodeChild(TreeNode aNode) Returns true ifaNodeis a child of this node.booleanisNodeDescendant(DefaultMutableTreeNode anotherNode) Returns true ifanotherNodeis a descendant of this node -- if it is this node, one of this node's children, or a descendant of one of this node's children.booleanReturns true if and only ifaNodeis in the same tree as this node.booleanisNodeSibling(TreeNode anotherNode) Returns true ifanotherNodeis a sibling of (has the same parent as) this node.booleanisRoot()Returns true if this node is the root of the tree.pathFromAncestorEnumeration(TreeNode ancestor) Creates and returns an enumeration that follows the path fromancestorto this node.Creates and returns an enumeration that traverses the subtree rooted at this node in postorder.Creates and returns an enumeration that traverses the subtree rooted at this node in preorder.voidremove(int childIndex) Removes the child at the specified index from this node's children and sets that node's parent to null.voidremove(MutableTreeNode aChild) RemovesaChildfrom this node's child array, giving it a null parent.voidRemoves all of this node's children, setting their parents to null.voidRemoves the subtree rooted at this node from the tree, giving this node a null parent.voidsetAllowsChildren(boolean allows) Determines whether or not this node is allowed to have children.voidsetParent(MutableTreeNode newParent) Sets this node's parent tonewParentbut does not change the parent's child array.voidsetUserObject(Object userObject) Sets the user object for this node touserObject.toString()Returns the result of sendingtoString()to this node's user object, or the empty string if the node has no user object.
-
Field Details
-
EMPTY_ENUMERATION
An enumeration that is always empty. This is used when an enumeration of a leaf node's children is requested. -
parent
this node's parent, or null if this node has no parent -
children
array of children, may be null if this node has no children -
userObject
optional user object -
allowsChildren
protected boolean allowsChildrentrue if the node is able to have children
-
-
Constructor Details
-
DefaultMutableTreeNode
public DefaultMutableTreeNode()Creates a tree node that has no parent and no children, but which allows children. -
DefaultMutableTreeNode
Creates a tree node with no parent, no children, but which allows children, and initializes it with the specified user object.- Parameters:
-
userObject- an Object provided by the user that constitutes the node's data
-
DefaultMutableTreeNode
Creates a tree node with no parent, no children, initialized with the specified user object, and that allows children only if specified.- Parameters:
-
userObject- an Object provided by the user that constitutes the node's data -
allowsChildren- if true, the node is allowed to have child nodes -- otherwise, it is always a leaf node
-
-
Method Details
-
insert
RemovesnewChildfrom its present parent (if it has a parent), sets the child's parent to this node, and then adds the child to this node's child array at indexchildIndex.newChildmust not be null and must not be an ancestor of this node.- 指定者:
-
insert在接口MutableTreeNode中 - 参数:
-
newChild- 要插入在此节点下方的MutableTreeNode -
childIndex- 要插入的节点在此节点的子节点数组中的索引 - 抛出:
-
ArrayIndexOutOfBoundsException- 如果childIndex超出范围 -
IllegalArgumentException- 如果newChild为null或是此节点的祖先 -
IllegalStateException- 如果此节点不允许有子节点 - 参见:
-
remove
public void remove(int childIndex) 从此节点的子节点中删除指定索引处的子节点,并将该节点的父节点设置为null。要删除的子节点必须是一个MutableTreeNode。- 指定者:
-
remove在接口MutableTreeNode中 - 参数:
-
childIndex- 要移除的子节点在此节点的子节点数组中的索引 - 抛出:
-
ArrayIndexOutOfBoundsException- 如果childIndex超出范围
-
setParent
将此节点的父节点设置为newParent,但不更改父节点的子节点数组。此方法从insert()和remove()中调用以重新分配子节点的父节点,不应该从其他任何地方调用。- 指定者:
-
setParent在接口MutableTreeNode中 - 参数:
-
newParent- 此节点的新父节点
-
getParent
返回此节点的父节点,如果此节点没有父节点则返回null。 -
getChildAt
返回此节点子节点数组中指定索引处的子节点。- 指定者:
-
getChildAt在接口TreeNode中 - 参数:
-
index- 此节点子节点数组中的索引 - 返回:
- 此节点子节点数组中指定索引处的TreeNode
- 抛出:
-
ArrayIndexOutOfBoundsException- 如果index超出范围
-
getChildCount
public int getChildCount()返回此节点的子节点数量。- 指定者:
-
getChildCount在接口TreeNode中 - 返回:
- 一个int,表示此节点的子节点数量
-
getIndex
返回此节点子节点数组中指定子节点的索引。如果指定的节点不是此节点的子节点,则返回-1。此方法执行线性搜索,时间复杂度为O(n),其中n为子节点数量。- 指定者:
-
getIndex在接口TreeNode中 - 参数:
-
aChild- 在此节点的子节点中搜索的TreeNode - 返回:
-
一个int,表示节点在此节点的子节点数组中的索引,如果指定的节点不是此节点的子节点则返回
-1 - 抛出:
-
IllegalArgumentException- 如果aChild为null
-
children
创建并返回此节点子节点的正向顺序枚举。修改此节点的子节点数组会使得任何在修改之前创建的子节点枚举无效。 -
setAllowsChildren
public void setAllowsChildren(boolean allows) 确定此节点是否允许有子节点。如果allows为false,则移除所有此节点的子节点。注意:默认情况下,节点允许有子节点。
- 参数:
-
allows- 如果此节点允许有子节点则为true
-
getAllowsChildren
public boolean getAllowsChildren()如果此节点允许有子节点则返回true。- 指定者:
-
getAllowsChildren在接口TreeNode中 - 返回:
- 如果此节点允许有子节点则返回true,否则返回false
-
setUserObject
将此节点的用户对象设置为userObject。- 指定者:
-
setUserObject在接口MutableTreeNode中 - 参数:
-
userObject- 构成此节点用户指定数据的对象 - 参见:
-
getUserObject
返回此节点的用户对象。- 返回:
- 用户存储在此节点的对象
- 参见:
-
removeFromParent
public void removeFromParent()从树中移除以此节点为根的子树,使此节点的父节点为null。如果此节点是其树的根节点,则不执行任何操作。- 指定者:
-
removeFromParent在接口MutableTreeNode中
-
remove
从此节点的子节点数组中移除aChild,使其父节点为null。- 指定者:
-
remove在接口MutableTreeNode中 - 参数:
-
aChild- 要移除的此节点的子节点 - 抛出:
-
IllegalArgumentException- 如果aChild为null或不是此节点的子节点
-
removeAllChildren
public void removeAllChildren()移除所有此节点的子节点,将它们的父节点设置为null。如果此节点没有子节点,则此方法不执行任何操作。 -
add
从其父节点中移除newChild,并通过将其添加到此节点的子节点数组的末尾使其成为此节点的子节点。- 参数:
-
newChild- 要作为此节点子节点添加的节点 - 抛出:
-
IllegalArgumentException- 如果newChild为null -
IllegalStateException- 如果此节点不允许有子节点 - 参见:
-
isNodeAncestor
如果anotherNode是此节点的祖先(即为此节点、此节点的父节点或此节点父节点的祖先),则返回true。如果anotherNode为null,则此方法返回false。此操作的最坏情况时间复杂度为O(h),其中h为从根节点到此节点的距离。- 参数:
-
anotherNode- 要测试是否为此节点祖先的节点 - 返回:
-
如果此节点是
anotherNode的后代则返回true - 参见:
-
isNodeDescendant
如果anotherNode是此节点的后代(即为此节点、此节点的一个子节点或此节点的一个子节点的后代),则返回true。注意,节点被认为是其自身的后代。如果anotherNode为null,则返回false。此操作的最坏情况时间复杂度为O(h),其中h为从根节点到anotherNode的距离。- Parameters:
-
anotherNode- node to test as descendant of this node - Returns:
-
true if this node is an ancestor of
anotherNode - See Also:
-
isNodeRelated
Returns true if and only ifaNodeis in the same tree as this node. Returns false ifaNodeis null.- Parameters:
-
aNode- node to find common ancestor with - Returns:
-
true if
aNodeis in the same tree as this node; false ifaNodeis null - See Also:
-
getDepth
public int getDepth()Returns the depth of the tree rooted at this node -- the longest distance from this node to a leaf. If this node has no children, returns 0. This operation is much more expensive thangetLevel()because it must effectively traverse the entire tree rooted at this node.- Returns:
- the depth of the tree whose root is this node
- See Also:
-
getLevel
public int getLevel()Returns the number of levels above this node -- the distance from the root to this node. If this node is the root, returns 0.- Returns:
- the number of levels above this node
- See Also:
-
getPath
Returns the path from the root, to get to this node. The last element in the path is this node.- Returns:
- an array of TreeNode objects giving the path, where the first element in the path is the root and the last element is this node.
-
getPathToRoot
Builds the parents of node up to and including the root node, where the original node is the last element in the returned array. The length of the returned array gives the node's depth in the tree.- Parameters:
-
aNode- the TreeNode to get the path for -
depth- an int giving the number of steps already taken towards the root (on recursive calls), used to size the returned array - Returns:
- an array of TreeNodes giving the path from the root to the specified node
-
getUserObjectPath
Returns the user object path, from the root, to get to this node. If some of the TreeNodes in the path have null user objects, the returned path will contain nulls.- Returns:
- the user object path, from the root, to get to this node
-
getRoot
Returns the root of the tree that contains this node. The root is the ancestor with a null parent.- Returns:
- the root of the tree that contains this node
- See Also:
-
isRoot
public boolean isRoot()Returns true if this node is the root of the tree. The root is the only node in the tree with a null parent; every tree has exactly one root.- Returns:
- true if this node is the root of its tree
-
getNextNode
Returns the node that follows this node in a preorder traversal of this node's tree. Returns null if this node is the last node of the traversal. This is an inefficient way to traverse the entire tree; use an enumeration, instead.- Returns:
- the node that follows this node in a preorder traversal, or null if this node is last
- See Also:
-
getPreviousNode
Returns the node that precedes this node in a preorder traversal of this node's tree. Returnsnullif this node is the first node of the traversal -- the root of the tree. This is an inefficient way to traverse the entire tree; use an enumeration, instead.- Returns:
- the node that precedes this node in a preorder traversal, or null if this node is the first
- See Also:
-
preorderEnumeration
Creates and returns an enumeration that traverses the subtree rooted at this node in preorder. The first node returned by the enumeration'snextElement()method is this node.Modifying the tree by inserting, removing, or moving a node invalidates any enumerations created before the modification.
- Returns:
- an enumeration for traversing the tree in preorder
- See Also:
-
postorderEnumeration
Creates and returns an enumeration that traverses the subtree rooted at this node in postorder. The first node returned by the enumeration'snextElement()method is the leftmost leaf. This is the same as a depth-first traversal.Modifying the tree by inserting, removing, or moving a node invalidates any enumerations created before the modification.
- Returns:
- an enumeration for traversing the tree in postorder
- See Also:
-
breadthFirstEnumeration
Creates and returns an enumeration that traverses the subtree rooted at this node in breadth-first order. The first node returned by the enumeration'snextElement()method is this node.Modifying the tree by inserting, removing, or moving a node invalidates any enumerations created before the modification.
- Returns:
- an enumeration for traversing the tree in breadth-first order
- See Also:
-
depthFirstEnumeration
Creates and returns an enumeration that traverses the subtree rooted at this node in depth-first order. The first node returned by the enumeration'snextElement()method is the leftmost leaf. This is the same as a postorder traversal.Modifying the tree by inserting, removing, or moving a node invalidates any enumerations created before the modification.
- Returns:
- an enumeration for traversing the tree in depth-first order
- See Also:
-
pathFromAncestorEnumeration
Creates and returns an enumeration that follows the path fromancestorto this node. The enumeration'snextElement()method first returnsancestor, then the child ofancestorthat is an ancestor of this node, and so on, and finally returns this node. Creation of the enumeration is O(m) where m is the number of nodes between this node andancestor, inclusive. EachnextElement()message is O(1).Modifying the tree by inserting, removing, or moving a node invalidates any enumerations created before the modification.
- Parameters:
-
ancestor- the node to start enumeration from - Returns:
- an enumeration for following the path from an ancestor of this node to this one
- Throws:
-
IllegalArgumentException- ifancestoris not an ancestor of this node - See Also:
-
isNodeChild
Returns true ifaNodeis a child of this node. IfaNodeis null, this method returns false.- Parameters:
-
aNode- the node to determinate whether it is a child - Returns:
-
true if
aNodeis a child of this node; false ifaNodeis null
-
getFirstChild
Returns this node's first child. If this node has no children, throws NoSuchElementException.- Returns:
- the first child of this node
- Throws:
-
NoSuchElementException- if this node has no children
-
getLastChild
Returns this node's last child. If this node has no children, throws NoSuchElementException.- Returns:
- the last child of this node
- Throws:
-
NoSuchElementException- if this node has no children
-
getChildAfter
返回此节点的子节点数组中紧随之后的子节点, aChild必须是此节点的子节点。如果aChild是最后一个子节点,则返回null。此方法对此节点的子节点执行线性搜索以查找aChild,时间复杂度为O(n),其中n是子节点的数量;要遍历所有子节点的数组,请改用枚举。- 参数:
-
aChild- 要查找其后的下一个子节点的子节点 - 返回:
-
此节点中紧随
aChild之后的子节点 - 抛出:
-
IllegalArgumentException- 如果aChild为null或不是此节点的子节点 - 另请参见:
-
getChildBefore
返回此节点的子节点数组中紧随aChild之前的子节点,aChild必须是此节点的子节点。如果aChild是第一个子节点,则返回null。此方法对此节点的子节点执行线性搜索以查找aChild,时间复杂度为O(n),其中n是子节点的数量。- 参数:
-
aChild- 要查找其前的上一个子节点的子节点 - 返回:
-
此节点中紧随
aChild之前的子节点 - 抛出:
-
IllegalArgumentException- 如果aChild为null或不是此节点的子节点
-
isNodeSibling
如果anotherNode是此节点的兄弟节点(具有相同的父节点),则返回true。节点本身是其自身的兄弟节点。如果anotherNode为null,则返回false。- 参数:
-
anotherNode- 要测试是否为此节点的兄弟节点的节点 - 返回:
-
如果
anotherNode是此节点的兄弟节点,则返回true
-
getSiblingCount
public int getSiblingCount()返回此节点的兄弟节点数量。节点本身是其自身的兄弟节点(如果没有父节点或没有兄弟节点,则此方法返回1)。- 返回:
- 此节点的兄弟节点数量
-
getNextSibling
返回此节点在父节点的子节点数组中的下一个兄弟节点。如果此节点没有父节点或是父节点的最后一个子节点,则返回null。此方法执行线性搜索,时间复杂度为O(n),其中n是子节点的数量;要遍历整个数组,请改用父节点的子节点枚举。- 返回:
- 立即跟随此节点的兄弟节点
- 另请参见:
-
getPreviousSibling
返回此节点在父节点的子节点数组中的上一个兄弟节点。如果此节点没有父节点或是父节点的第一个子节点,则返回null。此方法执行线性搜索,时间复杂度为O(n),其中n是子节点的数量。- 返回:
- 立即在此节点之前的兄弟节点
-
isLeaf
public boolean isLeaf()如果此节点没有子节点,则返回true。要区分没有子节点的节点和不能有子节点的节点(例如,区分文件和空目录),请结合使用此方法和getAllowsChildren。 -
getFirstLeaf
查找并返回作为此节点后代的第一个叶子节点 - 可能是此节点或其第一个子节点的第一个叶子节点。如果它是叶子节点,则返回此节点。- 返回:
- 以此节点为根的子树中的第一个叶子节点
- 另请参见:
-
getLastLeaf
查找并返回作为此节点后代的最后一个叶子节点 - 可能是此节点或其最后一个子节点的最后一个叶子节点。如果它是叶子节点,则返回此节点。- 返回:
- 以此节点为根的子树中的最后一个叶子节点
- 另请参见:
-
getNextLeaf
返回此节点之后的叶子节点,如果此节点是树中的最后一个叶子节点,则返回null。在此
MutableNode接口的实现中,此操作效率非常低。为了确定下一个节点,此方法首先在父节点的子节点列表中执行线性搜索以找到当前节点。该实现使得该操作适用于从已知位置进行短暂遍历。但要遍历树中的所有叶子节点,应使用
depthFirstEnumeration来枚举树中的节点,并对每个节点使用isLeaf来确定哪些是叶子节点。- 返回:
- 返回此节点之后的下一个叶子节点
- 另请参见:
-
getPreviousLeaf
返回此节点之前的叶子节点,如果此节点是树中的第一个叶子节点,则返回null。在此
MutableNode接口的实现中,此操作效率非常低。为了确定前一个节点,此方法首先在父节点的子节点列表中执行线性搜索以找到当前节点。该实现使得该操作适用于从已知位置进行短暂遍历。但要遍历树中的所有叶子节点,应使用
depthFirstEnumeration来枚举树中的节点,并对每个节点使用isLeaf来确定哪些是叶子节点。- 返回:
- 返回此节点之前的叶子节点
- 另请参见:
-
getLeafCount
public int getLeafCount()返回作为此节点后代的叶子节点总数。如果此节点是叶子节点,则返回1。此方法的时间复杂度为O(n),其中n是此节点的后代数量。- 返回:
- 此节点下的叶子节点数量
- 另请参见:
-
toString
返回将toString()发送到此节点的用户对象的结果,如果节点没有用户对象,则返回空字符串。 -
clone
覆盖以使克隆公开。返回此节点的浅拷贝;新节点没有父节点或子节点,并且如果有的话,具有对相同用户对象的引用。
-