Java how to implement the data structure of the algorithm Tree

Someone recently asked how I always implement data structure algorithms Tree in Java, the Internet today read an article about the code in this area, out and share with you now!

Code for [a] section

package ChapterEight;   
  
class Tree {   
    class Node {   
        public long value;   
  
        public Node leftChild;   
  
        public Node rightChild;   
  
        public Node(long value) {   
            this.value = value;   
            leftChild = null;   
            rightChild = null;   
        }   
    }   
  
    public Node root;   
  
    public Tree() {   
        root = null;   
    }   
  
    // 向树中插入一个节点   
    public void insert(long value) {   
        Node newNode = new Node(value);   
        // 树是空的   
        if (root == null)   
            = the newNode the root;   
        the else {   
            the Node Current = the root;   
            the Node the parentNode;   
            the while (to true) {   
                the parentNode = Current;   
                if (value <current.value) {   
                    Current = current.leftChild;   
                    // node to be inserted to the left child node   
                    if ( == null Current) {   
                        parentNode.leftChild = the newNode;   
                        return;   
                    }   
                } the else {   
                    // node to be inserted is a right child node   
                    current = current.rightChild;   
                    IF (Current == null) {   
                        parentNode.rightChild = the newNode;   
                        return;   
                    }   
                }   
            }   
        }   
    }   
  
    // continue to traverse all nodes in the tree   
    public void preOrder (the Node currentRoot) {   
        IF (currentRoot = null!) {   
            the System.out .print (currentRoot.value + "");   
            preOrder (currentRoot.leftChild);   
            preOrder (currentRoot.rightChild);   
        }   
    }   
  
    // the tree in preorder traversal of all nodes in the   
    public void inOrder (node currentNode) {   
        if (currentNode != null) {   
            inOrder(currentNode.leftChild);   
            System.out.print(currentNode.value + " ");   
            inOrder(currentNode.rightChild);   
        }   
    }   
  
    // 后续遍历树中的所有节点   
    public void postOrder(Node currentNode) {   
        if (currentNode != null) {   
            postOrder(currentNode.leftChild);   
            postOrder(currentNode.rightChild);   
            System.out.print(currentNode.value + " ");   
        }   
    }   
  
    public void traverse(int traverseType) {   
        switch (traverseType) {   
        case 1:   
            preOrder(root);   
            break;   
        case 2:   
            inOrder(root);   
            break;   
        case 3:   
            postOrder(root);   
            break;   
        default:   
            break;   
        }   

// value according to a tree node to node in the tree delete   
    public Boolean Delete (int value) {   
        // traversing the tree during the current node   
        the Node Current = the root;   
        // To delete a parent node   
        the Node parent = the root;   
        // record node of the tree to the left or right child node child node   
        boolean = isLeftChild to true;   
        the while (! current.value = value) {   
            parent = current;   
            // node to be removed in the left sub-tree of the current node   
            if (value <current .Value) {   
                isLeftChild = to true;   
                current = current.leftChild;   
            }   
            // to delete a node in the right subtree of the current node in   
            the else {   
                isLeftChild = to false;   
                = current.rightChild Current;   
            }   
            // node was not found to be deleted in the tree   
            IF (Current == null)   
                return false;   
        }   
        node // to be deleted is a leaf node   
        if (current.leftChild == null && current.rightChild == null) {   
            node // to remove the root node   
            IF (Current == root)   
                root = null;   
            // node to be removed is left child node of   
            the else IF (isLeftChild)   
                parent.leftChild = null;   
            // you want to delete the right child node to node   
            the else  
                parent.rightChild = null;   
        }   
        // node to be removed has left child node, there is no right child node   
        else if (current.rightChild == null) {   
            node // to remove the root node   
            IF (Current == null)   
                root = current.leftChild;   
            // to delete a node is left child node of   
            the else IF (isLeftChild)   
                parent.leftChild = current.leftChild;   
            // node to be removed for the right child node of   
            the else  
                parent.rightChild = current.leftChild;   
        }   
        node // to remove left no children node, right child node has   
        the else IF (current.leftChild == null) {   
            // node to remove the root node   
            IF (Current == root)   
                root = root.rightChild;   
            // node to be removed is left child node of   
            the else IF (isLeftChild)   
                parent.leftChild = current.rightChild;   
            // node to be removed for the right child node of   
            the else  
                parent.rightChild = current.rightChild;   
        }   
        then // node to be deleted both left and right child nodes child nodes have   
        the else {   
            the node by successor = getSuccessor (Current);   
            node // to remove the root node   
            IF (Current == root)   
                root = by successor;   
            node // to remove the left child node   
            IF else (isLeftChild)   
                parent.leftChild = by successor;   
            // node to be removed for the right child node   
            else  
                = by successor parent.rightChild;   
        }   
        return to true;   
    }   
  
    // find the node to be deleted node Substituted   
    Private getSuccessor the Node (the Node delNode) {   
        // substitute parent node   
        the Node successorParent = delNode;   
        // Substituted node deletes the node   
        Node successor = delNode;   
        the node current = delNode.rightChild;   
        the while (current = null!) {   
            // successorParent point to the current node on a node   
            successorParent = by successor;   
            // by successor becomes the current node   
            by successor = current;   
            current = current.leftChild;   
        }   
        // 替补节点的右孩子节点不为空   
        if (successor != delNode.rightChild) {   
            successorParent.leftChild = successor.rightChild;   
            successor.rightChild = delNode.rightChild;   
        }   
        return successor;   
    }   
}   
  
public class TreeApp {   
    public static void main(String[] args) {   
        Tree tree = new Tree();   
        tree.insert(8);   
        tree.insert(50);   
        tree.insert(45);   
        tree.insert(21);   
        tree.insert(32);   
        tree.insert(18);   
        tree.insert(37);   
        tree.insert(64);   
        tree.insert(88);   
        tree.insert(5);   
        tree.insert(4);   
        tree.insert(7);   
  
        System.out.print("PreOrder : ");   
        tree.traverse(1);   
        System.out.println();   
  
        System.out.print("InOrder : ");   
        tree.traverse(2);   
        System.out.println();   
  
        System.out.print("PostOrder : ");   
        tree.traverse(3);   
        System.out.println();   
  
        System.out.println(tree.delete(7));   
  
        System.out.print("PreOrder : ");   
        tree.traverse(1);   
        System.out.println();   
  
        System.out.print("InOrder : ");   
        tree.traverse(2);   
        System.out.println();   
  
        System.out.print("PostOrder : ");   
        tree.traverse(3);   
        System.out.println();   
  
    }   
}

Reproduced in: https: //www.cnblogs.com/licheng/archive/2010/04/06/1705547.html

Guess you like

Origin blog.csdn.net/weixin_34168880/article/details/92634167
Recommended