public class BinaryTreeNode { private int data;//数据 private BinaryTreeNode leftChild;//左孩子 private BinaryTreeNode rightChild;//右孩子 public int getData() { return data; } public void setData(int data) { this.data=data; } public BinaryTreeNode getLeftChild() { return leftChild; } public void setLeftChild(BinaryTreeNode leftChirld) { this.leftChild=leftChirld; } public BinaryTreeNode getRightChild() { return rightChild; } public void setRightChild(BinaryTreeNode rightChild) { this.rightChild=rightChild; } }
BinaryTree class {public Private BinaryTreeNode the root; public BinaryTree (BinaryTreeNode the root) { this.root = the root; } public void setRoot (BinaryTreeNode the root) { this.root = the root; } public BinaryTreeNode the getRoot () { return the root; } / ** * Clear binary tree: * providing a first clear to a node as the root node of the subtree method, each node both recursively deleted; * delete tree then provides a method to delete the root node by the first method to * / // clear a sub-tree of all nodes public void the Clear (BinaryTreeNode the node) { IF (the node = null!) { the Clear (node.getLeftChild ()); the Clear (node.getRightChild ()); the node = null ; // delete node } } // empty tree void Clear public () { node 1 * when the number of nodes required, we look to obtain a node's subtree achieve numbers. Clear (the root); } // determines whether the binary tree is empty public Boolean isEmpty () { return the root == null; } // Get the height of a node in the subtree public int HEIGH (BinaryTreeNode Node) { IF (Node == null) { return 0; // recursive end, empty tree height 0 } the else { // recursive acquired left subtree height int L = HEIGH (node.getLeftChild ()); // right subtree recursively obtaining height int r = HEIGH (node.getRightChild ()); // height should be considered a higher side, (+ 1 because they want to own this layer count) return L> r (L + 1) :( r + 1);? } } public int HEIGH () { return HEIGH (the root); } / ** * binary tree of nodes: * 2. the first node is empty, the number of affirmative 0; * 3. If not empty, then count after this node recursively all child nodes of the left and right sub-tree, * 4 are all added to the given number of nodes is the root node of the subtree * 5. If a binary tree of nodes, the root node to the input * / public int size (BinaryTreeNode Node) { IF (Node == null) { return 0; // If the node is empty, returns to the nodes 0 } the else { // computing node so to +1 // acquired recursively left subtree right subtree nodes and nodes, adding the final + size. 1 return (node.getLeftChild ()) + size (node.getRightChild ()); } } // Get the number of nodes of the binary tree public int size () { return size (the root); } // returns a father node node // node subtree node subTree parent public BinaryTreeNode getParent (BinaryTreeNode subTree, BinaryTreeNode node) { IF (subTree == null) {if //// tree is empty, there is no parent node return null; } // if one of the child around the root of the subtree is a node of unknown origin, the root of the subtree return return node.getLeftChild (); IF (subTree.getLeftChild () == subTree.getRightChild Node || () == Node) { return subTree; } BinaryTreeNode parent = null; IF (! getParent (subTree.getLeftChild (), Node) = null) { parent = getParent (subTree.getLeftChild (), node); return parent; } the else { // recursive left subtree return getParent (subTree.getRightChild (), node); } } // find the parent node of the node in the binary tree node public BinaryTreeNode getParent (BinaryTreeNode Node) { return (the root == == null || the root Node) null:? getParent (the root, Node); } // return the left subtree public BinaryTreeNode getLeftTree (BinaryTreeNode Node) { } // returns the right child tree Public BinaryTreeNode getRightTree (BinaryTreeNode Node) { return node.getRightChild (); } / ** * is inserted into the binary tree: * two cases: the insertion of the left child node of a node; into the right child node of a node * worth It noted that, when the node itself has child nodes, such insertion will overwrite the previous node in this position. * In addition, although the child node is inserted, but can also represent a child node sub-tree. * However, this does not seem to know whether this node node about the existence of sub-tree, so although a node is inserted, but it is possible to insert many nodes (insertion of a subtree) * / // to insert a node left node public void insertLeft (BinaryTreeNode parent, BinaryTreeNode the newNode) { parent.setLeftChild (the newNode); } // inserted into the right node to a node public void insertRight (BinaryTreeNode parent, BinaryTreeNode the newNode) { parent.setRightChild (the newNode); } / / preorder void preOrder public (BinaryTreeNode Node) { IF (Node! = null) { IF (Node = null!) { System.out.println (node.getData ()); // first root access PreOrder (node.getLeftChild ()); // first Traversal left subtree PreOrder (node.getRightChild ( )); // right subtree first preorder traversal } } // preorder public void the inOrder (BinaryTreeNode Node) { IF (Node = null) {! the inOrder (node.getLeftChild ()); // root traversing the left subtree System.out.println (node); // root access InOrder (node.getRightChild ()); // right subtree root traversing } } // postorder public void postorder (BinaryTreeNode Node) { postOrder (node.getRightChild ()); // the right subtree traversal PostOrder (node.getLeftChild ()); // After traversing the left subtree root System.out.println (node); // access root } } }