defaultmutabletreenode.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 1,021 行 · 第 1/2 页
JAVA
1,021 行
current = root;
index = 0;
// while (current != root) {
do {
// if (random.nextInt(3) < 2) {
if (random.nextBoolean()) {
node = new DefaultMutableTreeNode(String.valueOf(index));
index++;
current.add(node);
current = node;
} else {
current = (DefaultMutableTreeNode) current.getParent();
} // if
// } // while
} while (current != root && current != null);
System.out.println("Number of nodes: " + index);
/*
// Calc # children
size = random.nextInt(4);
for (index = 0; index < size; index++) {
// Create Node
node = new DefaultMutableTreeNode(String.valueOf(index));
growTree(node);
// Add Node to root
root.add(node);
} // for
*/
} // growTree()
public static void main(String[] argv) {
/*
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("node1");
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("node2");
DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("node3");
DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("node4");
DefaultMutableTreeNode node5 = new DefaultMutableTreeNode("node5");
DefaultMutableTreeNode node6 = new DefaultMutableTreeNode("node6");
DefaultMutableTreeNode node7 = new DefaultMutableTreeNode("node7");
DefaultMutableTreeNode node8 = new DefaultMutableTreeNode("node8");
node1.add(node2);
node1.add(node3);
node2.add(node4);
node2.add(node5);
node3.add(node6);
node3.add(node7);
node5.add(node8);
System.out.println("Depth (node1): " + node1.getDepth());
System.out.println("Depth (node2): " + node2.getDepth());
System.out.println("Depth (node3): " + node3.getDepth());
*/
System.out.println("Create tree...");
DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
growTree(root);
System.out.println("Find depth...");
System.out.println("Depth (root): " + root.getDepth());
} // main
/**
* getLevel
* @returns int
*/
public int getLevel() {
// Variables
TreeNode current;
int count;
// Lookup Parent
count = -1;
current = this;
do {
current = current.getParent();
count++;
} while (current != null);
return count;
} // getLevel()
/**
* getPathToRoot
* @param value0 TODO
* @param value1 TODO
* @returns TreeNode[]
*/
protected TreeNode[] getPathToRoot(TreeNode value0, int value1) {
return null; // TODO
} // getPathToRoot()
/**
* getUserObjectPath
* @returns Object[]
*/
public Object[] getUserObjectPath() {
// Variables
TreeNode[] path;
Object[] object;
int size;
int index;
// Get Path for Tree Nodes
path = getPath();
// Construct Object Path
object = new Object[path.length];
for (index = 0; index < path.length; index++) {
object[index] = ((DefaultMutableTreeNode) path[index]).getUserObject();
} // for
// Return Object Path
return object;
} // getUserObjectPath()
/**
* getRoot
* @returns TreeNode
*/
public TreeNode getRoot() {
// Variables
TreeNode current;
TreeNode check;
// Lookup Parent
current = this;
check = current.getParent();
while (check != null) {
current = check;
check = current.getParent();
} // while
return current;
} // getRoot()
/**
* isRoot
* @returns boolean
*/
public boolean isRoot() {
return (parent == null);
} // isRoot()
/**
* getNextNode
* @returns DefaultMutableTreeNode
*/
public DefaultMutableTreeNode getNextNode() {
return null; // TODO
} // getNextNode()
/**
* getPreviousNode
* @returns DefaultMutableTreeNode
*/
public DefaultMutableTreeNode getPreviousNode() {
return null; // TODO
} // getPreviousNode()
/**
* preorderEnumeration
* @returns Enumeration
*/
public Enumeration preorderEnumeration() {
return null; // TODO
} // preorderEnumeration()
/**
* postorderEnumeration
* @returns Enumeration
*/
public Enumeration postorderEnumeration() {
return null; // TODO
} // postorderEnumeration()
/**
* breadthFirstEnumeration
* @returns Enumeration
*/
public Enumeration breadthFirstEnumeration() {
return null; // TODO
} // breadthFirstEnumeration()
/**
* depthFirstEnumeration
* @returns Enumeration
*/
public Enumeration depthFirstEnumeration() {
return null; // TODO
} // depthFirstEnumeration()
/**
* pathFromAncestorEnumeration
* @param value0 TODO
* @returns Enumeration
*/
public Enumeration pathFromAncestorEnumeration(TreeNode value0) {
return null; // TODO
} // pathFromAncestorEnumeration()
/**
* isNodeChild
* @param value0 TODO
* @returns boolean
*/
public boolean isNodeChild(TreeNode node) {
// Variables
TreeNode current;
int index;
// Sanity Check
if (node == null) {
return false;
} // if
// Process Path
current = node;
while (current != null) {
if (current == this) {
return true;
} // if
current = current.getParent();
} // while
// Node not located in path, not child
return false;
} // isNodeChild()
/**
* getFirstChild
* @returns TreeNode
*/
public TreeNode getFirstChild() {
return (TreeNode) children.firstElement();
} // getFirstChild()
/**
* getLastChild
* @returns TreeNode
*/
public TreeNode getLastChild() {
return (TreeNode) children.lastElement();
} // getLastChild()
/**
* getChildAfter
* @param value0 TODO
* @returns TreeNode
*/
public TreeNode getChildAfter(TreeNode node) {
// Variables
int index;
// Check node
if (node == null || node.getParent() != this) {
throw new IllegalArgumentException();
} // if
// Get index of child node
index = getIndex(node);
// Check for child after
index++;
if (index == getChildCount()) {
return null;
} // if
// Retrieve Child After
return getChildAt(index);
} // getChildAfter()
/**
* getChildBefore
* @param value0 TODO
* @returns TreeNode
*/
public TreeNode getChildBefore(TreeNode node) {
// Variables
int index;
// Check node
if (node == null || node.getParent() != this) {
throw new IllegalArgumentException();
} // if
// Get index of child node
index = getIndex(node);
// Check for child before
index--;
if (index < 0) {
return null;
} // if
// Retrieve Child Before
return getChildAt(index);
} // getChildBefore()
/**
* isNodeSibling
* @param value0 TODO
* @returns boolean
*/
public boolean isNodeSibling(TreeNode node) {
// Variables
int index;
// Check for null
if (node == null) {
return false;
} // if
// Check if nodes share a parent
if (node.getParent() == getParent() && getParent() != null) {
return true;
} // if
// Nodes are not siblings
return false;
} // isNodeSibling()
/**
* getSiblingCount
* @returns int
*/
public int getSiblingCount() {
// Variables
// Check for no parent
if (parent == null) {
return 1;
} // if
// Calculate sibling count from parent's child count
return parent.getChildCount();
} // getSiblingCount()
/**
* getNextSibling
* @returns DefaultMutableTreeNode
*/
public DefaultMutableTreeNode getNextSibling() {
// Variables
int index;
int size;
// Check for Parent
if (parent == null) {
return null;
} // if
// Get Index of this node
index = parent.getIndex(this);
// Check for Next Sibling
size = parent.getChildCount();
index++;
if (index == size) {
return null;
} // if
return (DefaultMutableTreeNode) parent.getChildAt(index);
} // getNextSibling()
/**
* getPreviousSibling
* @returns DefaultMutableTreeNode
*/
public DefaultMutableTreeNode getPreviousSibling() {
// Variables
int index;
// Check for Parent
if (parent == null) {
return null;
} // if
// Get Index of this node
index = parent.getIndex(this);
// Check for Previous Sibling
index--;
if (index < 0) {
return null;
} // if
return (DefaultMutableTreeNode) parent.getChildAt(index);
} // getPreviousSibling()
/**
* isLeaf
* @returns boolean
*/
public boolean isLeaf() {
return (children.size() == 0); // TODO: check allowsChildren??
} // isLeaf()
/**
* getFirstLeaf
* @returns DefaultMutableTreeNode
*/
public DefaultMutableTreeNode getFirstLeaf() {
// Variables
TreeNode current;
current = this;
while (current.getChildCount() > 0) {
current = current.getChildAt(0);
} // while
return (DefaultMutableTreeNode) current;
} // getFirstLeaf()
/**
* getLastLeaf
* @returns DefaultMutableTreeNode
*/
public DefaultMutableTreeNode getLastLeaf() {
// Variables
TreeNode current;
int size;
current = this;
size = current.getChildCount();
while (size > 0) {
current = current.getChildAt(size - 1);
size = current.getChildCount();
} // while
return (DefaultMutableTreeNode) current;
} // getLastLeaf()
/**
* getNextLeaf
* @returns DefaultMutableTreeNode
*/
public DefaultMutableTreeNode getNextLeaf() {
return null; // TODO
} // getNextLeaf()
/**
* getPreviousLeaf
* @returns DefaultMutableTreeNode
*/
public DefaultMutableTreeNode getPreviousLeaf() {
return null; // TODO
} // getPreviousLeaf()
/**
* getLeafCount
* @returns int
*/
public int getLeafCount() {
// Variables
Enumeration e;
int count;
TreeNode current;
// Get Enumeration of all descendants
e = depthFirstEnumeration();
// Process Nodes
count = 0;
while (e.hasMoreElements() == true) {
current = (TreeNode) e.nextElement();
if (current.isLeaf() == true) {
count++;
} // if
} // if
return count;
} // getLeafCount()
} // DefaultMutableTreeNode
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?