Binary Tree function implementation (JAVA)

Source: Internet
Author: User
Binary Tree function implementation (JAVA)
It mainly implements the common usage of binary trees and may cause some errors. I hope to correct them.

  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 () {// invalid variable
  10. System. out. print ('{');
  11. System. out. print (idata );
  12. System. out. print (',');
  13. System. out. print (ddata );
  14. System. out. print ('}');
  15. }
  16. }

[Code] package structure. tree; import java. util. stack; public class Tree {/* Node attribute. the Tree consists of */private Node 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 a specified node to the tree structure * Description: true is returned after successful insertion, if insertion fails, false is returned ** @ param key * @ param data * @ return */public boolean insert (int key, double data) {boolean flag = true; boolean isLeftNode = false; node node = new Node (); node. idata = key; node. ddata = data; Node parent = new Node (); I F (root = null) root = node; else {Node current = root; while (current! = Null) {parent = current; if (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;}/*** specify a node in the deletion tree * Description: true is returned if the deletion is successful. if the deletion fails or fails to be found, false is returned * algorithm: * | -- Locate the node to be deleted * | -- delete a leaf node * | -- delete a node with only one subnode * | -- delete a node with two subnodes ** @ 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 = current. leftNode;} else {isLeftNode = false; current = current. rightNode;} if (current = null) {// The specified node flag is not found; return flag ;}/// the while loop is ended. // The execution ends here, it means that the current node to be deleted is found to be the leaf node if (current. leftNode = null & current. rightNode = null) {if (isLeftNode = true) parent. leftNode = null; elseparent. rightNode = null;} // delete Only the left subnode 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 only has the right sub-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 subnode and the right subnode else {N Ode replacedNode = getReplacedNode (current); if (current = root) root = replacedNode; else if (isLeftNode) parent. leftNode = replacedNode; else parent. rightNode = replacedNode;} return flag;}/*** determine the traversal method ** @ param traverseType */public void traverse (int traverseType) {switch (traverseType) {case 1: system. out. print ("\ n sequential traversal:"); preOrder (root); break; case 2: System. out. print ("\ n middle-order traversal:"); inOrder (root); bre Ak; case 3: System. out. print ("\ n post-order traversal:"); postOrder (root); break;} System. out. println ();}/*** sort first */private void preOrder (Node node) {if (node! = Null) {System. out. print (node. idata + ""); preOrder (node. leftNode); preOrder (node. rightNode) ;}}/*** central order */private void inOrder (Node node) {if (node! = Null) {preOrder (node. leftNode); System. out. print (node. idata + ""); preOrder (node. rightNode) ;}}/*** sort back */private void postOrder (Node node) {if (node! = Null) {preOrder (node. leftNode); preOrder (node. rightNode); System. out. print (node. idata + "") ;}}/*** find the node to replace the deleted node and build a subtree with the replaced node as the root * Description: find the point with the smallest key value in the right subtree of the deleted node as the "replace node", which is 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 replacedParentNode to replace the deleted node. leftNode = replacedNode. rightNode; replacedNode. rightNode = delNode. rightNode;} replacedNode. leftNode = delNode. leftNode; return replacedNode;}/*** display 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.