public class TreeParent<E> { public static class Node<T> { T data; // record the position of its parent node int parent; public Node () { } public Node (T data) { this.data = data; } public Node(T data, int parent) { this.data = data; this.parent = parent; } public String toString() { return "TreeParent$Node[data=" + data + ", parent=" + parent + "]"; } } private final int DEFAULT_TREE_SIZE = 100; private int treeSize = 0; // use a Node[] array to record all nodes in the tree private Node<E>[] nodes; // record the number of nodes private int nodeNums; // create tree with specified root node public TreeParent(E data) { treeSize = DEFAULT_TREE_SIZE; nodes = new Node[treeSize]; nodes[0] = new Node<E>(data, -1); nodeNums ++; } // Create a tree with the specified root node and specified treeSize public TreeParent(E data, int treeSize) { this.treeSize = treeSize; nodes = new Node[treeSize]; nodes[0] = new Node<E>(data, -1); nodeNums ++; } // add child nodes to the specified node public void addNode(E data, Node parent) { for (int i = 0; i < treeSize; i++) { // Find the first null element in the array that holds the new node if (nodes[i] == null) { // Create a new node and save it with the specified array element nodes[i] = new Node(data, pos(parent)); nodeNums ++; return; } } throw new RuntimeException("The tree is full and new nodes cannot be added"); } // Check if the tree is empty public boolean empty() { // Whether the root node is null return nodes[0] == null; } // return the root node public Node<E> root() { return nodes[0]; } // Returns the parent node of the specified node (non-root node) public Node<E> parent(Node node) { // The parent of each node records the position of its parent node return nodes[node.parent]; } // Returns all child nodes of the specified node (non-leaf node) public List<Node<E>> children(Node parent) { List<Node<E>> list = new ArrayList<Node<E>>(); for (int i = 0; i < treeSize; i++) { // If the position of the current node's parent node is equal to the position of the parent node if (nodes[i] != null && nodes[i].parent == pos(parent)) { list.add(nodes[i]); } } return list; } // return the depth of the tree public int deep() { // Used to record the maximum depth of the node int max = 0; for (int i = 0; i < treeSize && nodes[i] != null; i++) { // Initialize the depth of this node int def = 1; // m records the position of the parent node of the current node int m = nodes[i].parent; // if its parent exists while (m != -1 && nodes[m] != null) { // Continue to search for the parent node upwards m = nodes[m].parent; def++; } if (max < def) { max = def; } } // return max depth return max; } // return the node containing the specified value public int pos(Node node) { for (int i = 0; i < treeSize; i++) { // find the specified node if (nodes[i] == node) { return i; } } return -1; } }
public class TreeChild<E> { private static class SonNode { // record the position of the current node private int pos; private SonNode next; public SonNode(int pos, SonNode next) { this.pos = pos; this.next = next; } } public static class Node<T> { T data; // record the first child node SonNode first; public Node(T data) { this.data = data; this.first = null; } public String toString() { if (first != null) { return "ThreeChild$Node[data=" + data + ", first=" + first.pos + "]"; } else { return "ThreeChild$Node[data=" + data + ", first=-1"; } } } private final int DEFAULT_TREE_SIZE = 100; private int treeSize = 0; // use a Node[] array to record all nodes in the tree private Node<E>[] nodes; // record the number of nodes private int nodeNums; // create tree with specified root node public TreeChild(E data) { treeSize = DEFAULT_TREE_SIZE; nodes = new Node[treeSize]; nodes[0] = new Node<E>(data); nodeNums ++; } // Create a tree with the specified root node and specified treeSize public TreeChild(E data, int treeSize) { this.treeSize = treeSize; nodes = new Node[treeSize]; nodes[0] = new Node<E>(data); nodeNums ++; } // add child nodes to the specified node public void addNode(E data, Node parent) { for (int i = 0; i < treeSize; i++) { // Find the first null element in the array that holds the new node if (nodes[i] == null) { // Create a new node and save it with the specified array element nodes[i] = new Node(data); if (parent.first == null) { parent.first = new SonNode(i, null); } else { SonNode next = parent.first; while (next.next != null) { next = next.next; } next.next = new SonNode(i, null); } nodeNums ++; return; } } throw new RuntimeException("The tree is full and new nodes cannot be added"); } // Check if the tree is empty public boolean empty() { // Whether the root node is null return nodes[0] == null; } // return the root node public Node<E> root() { // return the root node return nodes[0]; } // Returns all child nodes of the specified node (non-leaf node) public List<Node<E>> children(Node parent) { List<Node<E>> list = new ArrayList<Node<E>>(); // Get the first child node of the parent node SonNode next = parent.first; // Keep searching for the next child node along the child chain while (next != null) { // add node in child chain list.add(nodes[next.pos]); next = next.next; } return list; } // Returns the index-th child node of the specified node (non-leaf node) public Node<E> child(Node parent, int index) { // Get the first child node of the parent node SonNode next = parent.first; // Keep searching for the next child node along the child chain for (int i = 0; next != null; i++) { if (index == i) { return nodes[next.pos]; } next = next.next; } return null; } // return the depth of the tree public int deep() { // Get the depth of the tree return deep(root()); } // recursive method: the depth of each subtree is the maximum depth of all subtrees + 1 private int deep(Node node) { if (node.first == null) { return 1; } else { // record the maximum depth of all its subtrees int max = 0; SonNode next = node.first; // Keep searching for the next child node along the child chain while (next != null) { // Get the depth of the subtree rooted at its child node int tmp = deep(nodes[next.pos]); if (tmp > max) { max = tmp; } next = next.next; } // Finally return the maximum depth of all its subtrees + 1 return max + 1; } } // return the node containing the specified value public int pos(Node node) { for (int i = 0; i < treeSize; i++) { // find the specified node if (nodes[i] == node) { return i; } } return -1; } }
public class TreeTest { public static void main(String[] args) { TreeParent<String> tp = new TreeParent<String>("root"); TreeParent.Node root = tp.root(); System.out.println("Root element after initialization: " + root); tp.addNode("Node 1", root); System.out.println("The depth of this tree: " + tp.deep()); tp.addNode("Node 2", root); // Get all child nodes of all root nodes List<TreeParent.Node<String>> nodes = tp.children(root); System.out.println("The first child of the root node: " + nodes.get(0)); // Add a child node to the first child node of the root node tp.addNode("节点3", nodes.get(0)); System.out.println("The depth of this tree: " + tp.deep()); System.out.println("================================"); TreeChild<String> tc = new TreeChild<String>("root"); TreeChild.Node tcRoot = tc.root(); System.out.println("Root node: " + tcRoot); tc.addNode("Node 1", tcRoot); tc.addNode("Node 2", tcRoot); tc.addNode("Node 3", tcRoot); System.out.println("Root node after adding child node: " + tcRoot); System.out.println("The depth of this tree: " + tc.deep()); // Get all the child nodes of the root node List<TreeChild.Node<String>> nodes2 = tc.children(tcRoot); System.out.println("The first child of the root node: " + nodes2.get(0)); // Add a child node to the first child node of the root node tc.addNode("节点4", nodes2.get(0)); System.out.println("The first child of the root node: " + nodes2.get(0)); System.out.println("The depth of this tree: " + tc.deep()); } }