Java inserts, searches, and deletes binary sorting trees.

Source: Internet
Author: User

Java inserts, searches, and deletes binary sorting trees.

Import java. util. random;/*** binary sorting tree (also called Binary Search Tree) * (1) can be an empty tree * (2) If the left subtree is not empty, then the value of all the nodes in the left subtree is less than the value of her root node * (3) if the right subtree is not empty, then, the values of all the nodes on the right subtree are greater than the values of her root node * (4) the left and right subtree are also binary sorting trees. ** performance analysis: * search performance: * The average length of the binary sorting Tree Containing n nodes is related to the tree structure. * (worst case) when the key words inserted in sequence are ordered, the binary tree is transformed into a single tree. The search performance is O (n) * (Best case). The binary sorting tree has the same shape as the decision tree for semi-query. Its average search length is the same as that for log2 (n) directly proportional to ** insert and delete performance: * The complexity between insert and delete operations is O (log (n) level, * that is, after O (log (n )) time: the location where you want to insert and delete nodes * directly insert and delete nodes at O (1) level * compared with the sequence table, insert and delete O (n) (query time O (log (n), move node time O (n )) faster * compared with the insertion time O (1) and deletion time O (n) of unordered sequence tables, the search speed is much faster because it is ordered. *** Author: ainiao * Creation Time: **/public class BinarySortTree {private Node root = null; /** search for keys in the binary sorting tree */public boolean searchBST (int key) {Node Current = root; while (current! = Null) {if (key = current. getValue () return true; else if (key <current. getValue () current = current. getLeft (); elsecurrent = current. getRight ();} return false;}/** insert Node to the binary sorting tree */public void insertBST (int key) {Node p = root; /** record the previous Node of the query Node */Node prev = null;/** keep searching until the Node location that meets the conditions */while (p! = Null) {prev = p; if (key <p. getValue () p = p. getLeft (); else if (key> p. getValue () p = p. getRight (); elsereturn;}/** prve is the parent node where the node is to be placed. Put it in the corresponding position */if (root = null) based on the node value) root = new Node (key); else if (key <prev. getValue () prev. setLeft (new Node (key); else prev. setRight (new Node (key);}/*** delete a Node in the binary tree is divided into three types: (delete a Node as * p and its parent Node as * f) * (1) the * p node to be deleted is a leaf node. You only need to modify its parent node pointer to null * (2) if * p only has the left subtree or only the right subtree, replace * p * (3) with the left/right subtree. * P has both the left and right subtree * replace p with the maximum value (I .e., the rightmost end S) in the left subtree of P, delete s, reconnect the left subtree **/public void deleteBST (int key) {deleteBST (root, key);} private boolean deleteBST (Node node, int key) {if (node = null) return false; else {if (key = node. getValue () {return delete (node);} else if (key <node. getValue () {return deleteBST (node. getLeft (), key);} else {return deleteBST (node. getRight (), key) ;}} private boolean delete (Node node) {Node temp = Null;/** the right subtree is empty. You only need to re-connect the left subtree. * if it is a leaf node, the leaf node is also deleted here. **/if (node. getRight () = null) {temp = node; node = node. getLeft ();}/** left subtree is empty, and its right subtree */else if (node. getLeft () = null) {temp = node; node = node. getRight ();}/** left and right subtree are not empty */else {temp = node; Node s = node;/** switch to left subtree, then go to the right to "end" */s = s. getLeft (); while (s. getRight ()! = Null) {temp = s; s = s. getRight ();} node. setValue (s. getValue (); if (temp! = Node) {temp. setRight (s. getLeft ();} else {temp. setLeft (s. getLeft () ;}} return true;}/** ordinal non-recursive traversal binary tree * Get ordered sequence **/public void nrInOrderTraverse () {Stack
 
  
Stack = new Stack
  
   
(); Node node = root; while (node! = Null |! Stack. isEmpty () {while (node! = Null) {stack. push (node); node = node. getLeft ();} node = stack. pop (); System. out. println (node. getValue (); node = node. getRight () ;}} public static void main (String [] args) {BinarySortTree bst = new BinarySortTree (); /** the constructed binary tree does not have the same element */int [] num = {, 6, 9, 3, 2, 0,-2 }; for (int I = 0; I <num. length; I ++) {bst. insertBST (num [I]);} bst. nrInOrderTraverse (); System. out. println (bst. searchBST (10); bst. deleteBST (2); bst. nrInOrderTraverse ();}/** Tree Node definition */public class Node {private int value; private Node left; private Node right; public Node () {} public Node (Node left, Node right, int value) {this. left = left; this. right = right; this. value = value;} public Node (int value) {this (null, null, value);} public Node getLeft () {return this. left;} public void setLeft (Node left) {this. left = left;} public Node getRight () {return this. right;} public void setRight (Node right) {this. right = right;} public int getValue () {return this. value;} public void setValue (int value) {this. value = value ;}}}
  
 


Related Article

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.