Binary Tree partial function implementation (JAVA)

Source: Internet
Author: User
The main implementation of the two-fork tree general usage, there may be some errors, but also hope to correct a bit.
    1. Package structure.tree;
    2. public class Node {
    3. public int idata;
    4. public double ddata;
    5. Public Node Leftnode;
    6. Public Node Rightnode;
    7. Public Node () {
    8. }
    9. public void display () {//отй╬╫з╣ц
    10. System.out.print (' {');
    11. System.out.print (idata);
    12. System.out.print (', ');
    13. System.out.print (Ddata);
    14. System.out.print ('} ');
    15. }
    16. }
Copy Code [Code]package Structure.tree;import Java.util.stack;public class Tree {/* node attribute, tree is a node-composed */private nodes Root;public tree ( {root = null;} /** * Find the tree node with the specified key value * * @param key * @return */public node find (int key) {Node current = Root;while (current.idata! = key) { if (Key < current.idata) Current = Current.leftnode;elsecurrent = current.rightnode;if (current = = NULL) return null;} return current;} /** * Insert specified node into tree structure * Description: Insert successful return true, insert failed return false * * @param key * @param data * @return */public boolean insert (int key, Doub Le data) {Boolean flag = True;boolean Isleftnode = false; Node node = new node (); node.idata = Key;node.ddata = data; Node parent = new Node (), if (root = null) root = node;else {Node current = Root;while (current! = null) {parent = Current;i F (Current.idata > Key) {isleftnode = True;current = Current.leftnode;} else if (Current.idata < key) {Isleftnode = False;current = Current.rightnode;} Elseflag = false;} End While loop}//end Else condition if ((flag = = true) && Isleftnode) parent.leftnOde = Node;else if ((flag = = true) &&!isleftnode) Parent.rightnode = Node;return flag;} /** * Delete the specified node in the tree * Description: Delete Successful return true, delete failed or not found return false * algorithm: * |--find node to delete * |--Delete leaf node * |--Delete node with only one child node * |--Delete node with two child nodes * * @param key * @return */public boolean Delete (int key) {Boolean flag = True;boolean Isleftnode = false; Node current = root; Node parent = Root;while (current.idata! = key) {parent = Current;if (Current.idata > key) {isleftnode = True;current = C Urrent.leftnode;} else {Isleftnode = False;current = Current.rightnode;} if (current = = null) {//does not find the corresponding specified node flag = False;return flag;}} End While loop//execute to this, meaning to find the node to be deleted current//deleted node is the leaf node if (Current.leftnode = = NULL && Current.rightnode = = null) {if (Isleftnode = = true) Parent.leftnode = Null;elseparent.rightnode = null;} The deleted node is only left dial hand node else if (Current.rightnode = = null) {if (current = = root) root = Current.leftnode;else if (isleftnode) Parent.leftnode = Current.leftnode;elseparent.rightnode = Current.leftnode;} The deleted node has only the right child node else if (CURrent.leftnode = = null) {if (current = = root) root = Current.rightnode;else if (isleftnode) Parent.leftnode = Current.rightnode;elseparent.rightnode = Current.rightnode;} The deleted node has the left Dial hand node and the right child node else {node Replacednode = Getreplacednode (current), if (current = = root) root = Replacednode, else if (is Leftnode) Parent.leftnode = Replacednode; else Parent.rightnode = Replacednode;} return flag;} /** * Determine the selection traversal method * * @param traversetype */public void traverse (int traversetype) {switch (traversetype) {case 1:SYSTEM.OUT.P Rint ("\ n First Order traversal:");p reorder (root), break;case 2:system.out.print ("\ n-Sequence traversal:"); inorder (root); Break;case 3: System.out.print ("\ n post-traversal:");p Ostorder (root); System.out.println ();} /** * First Order */private void Preorder (node node) {if (node! = null) {System.out.print (Node.idata + "");p Reorder (node.leftnode );p reorder (Node.rightnode);}} /** * ordered */private void Inorder (node node) {if (node! = null) {preorder (Node.leftnode); System.out.print (Node.idata + "");p reorder (Node.rightnode);}} /** * Order */private VoiD Postorder (node node) {if (node! = null) {preorder (Node.leftnode);p reorder (node.rightnode); System.out.print (Node.idata + "");}} /** * Find the node that replaces the "deleted node" and build a subtree with the root of "replacement point" * Description: Look for the point with the lowest key value in the right subtree of the "deleted node" as the "replacement node", obviously the left leaf node in the right subtree (if any) * * @param delnode * @ Return */private node Getreplacednode (node Delnode) {node current = Delnode.rightnode; Node Replacednode = Delnode; Node Replacedparentnode = Delnode;while (current! = null) {Replacedparentnode = Replacednode;replacednode = current; current = Current.leftnode;} if (replacednode! = Delnode.rightnode) {//Replacednode is the node to replace the "deleted node" Replacedparentnode.leftnode = Replacednode.rightnode;replacednode.rightnode = Delnode.rightnode;} Replacednode.leftnode = Delnode.leftnode;return Replacednode;} /** * Show tree structure * * @param node */@SuppressWarnings ("unchecked") public void DisplayTree () {System.out.println ("
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.