Java implementation Two-fork sorting tree Insert, find, delete

Source: Internet
Author: User

Import java.util.random;/** * Binary sort tree (also called binary search tree) * (1) can be an empty tree * (2) The Joz tree is not empty, then the value of all nodes on the left subtree is less than the value of her root node * (3) If the right subtree is not empty, The value of all nodes on the right subtree is greater than the value of her root node * (4) The left and right subtrees are also two-fork sorting trees * * Performance Analysis: * Find performance: * The average lookup length of a two-fork sort tree with n nodes is related to the morphology of the tree. * (worst case) when the successively inserted keyword is ordered. The composition of the two-fork sorting tree is transformed into a single branch tree. Find performance for O (n) * (best case) binary sorting tree morphology and binary lookup of the decision tree Likewise, its average lookup length is proportional to the LOG2 (n) * * * INSERT, delete performance: * Insert, delete operation between the complexity of both O (log (n)) level. * That is, the O (log (n)) time has been searched for the location to insert the deletion node and delete the node position * by the O (1) level of time directly inserted and deleted * Compared to the sequential table.  Order table Insert Delete O (n) (Find time O (log (n)) Move node time O (n)) fast * with unordered order table Insert time O (1), delete time O (n) compared to, because is ordered, the search speed is much faster * * * * SMALL rookie * created: 2014-08-17 * */public class Binarysorttree {private Node root = null;/** Find if there is a key value in the binary sort tree */public boolean searchbst (int key) {Node Curren t = root;while (current! = null) {if (key = = Current.getvalue ()) return True;else if (Key < Current.getvalue ()) current = cur Rent.getleft (); elsecurrent = Current.getright ();} return false;} /** inserts a node into a binary sort tree */public void insertbst (int key) {Node p = root;/** records the previous node of the lookup node */node prev = null;/** continues to look until it reaches the node where the condition is met * /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 to be placed, based on the node is worth the size, placed in the corresponding position */if (root = null) root = new node (key); else if (Key < Prev.getvalue ()) Prev.setleft (new node); else Prev.setright (key);} /** * Delete a node in a binary sort tree is divided into three cases: (delete node is *p.) The parent node is *f) * (1) The *p node to be deleted is the leaf node, only the pointer to change its parent node is empty * (2) If *p only have Zuozi or just the right subtree, directly let the Zuozi/right subtree replace *p * (3) If *p have left dial hand tree, and there is a right sub-tree * Replace P with the largest value in the P left dial hand tree (that is, the right side s). Delete S, re-connect its 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;/** Right subtree empty, just need to re-connect its left subtree * Assuming it is a leaf node, where the leaf node is also deleted * */if (node.getright () = = N ull) {temp = Node;node = Node.getleft ();} /** Shora, re-connect its right subtree */else if (node.getleft () = = null) {temp = Node;node = Node.getright ();} /** the left and right sub-trees are not empty */else{temp = node; Node s = node;/** to the left subtree, then 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;} /** in-sequence non-recursive traversal binary tree * Get ordered sequence * */public void Nrinordertraverse () {stack<node> Stack = new stack<node> (); 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 ()/** constructed two-fork tree does not have the same element */int[] num = {4,7,2,1,10 , 6,9,3,8,11,2, 0, -2};for (int i = 0; i < num.length; i++) {Bst.insertbst (num[i]);} Bst.nrinordertraverse (); System.out.println (BST.SEARCHBST); Bst.deletebst (2); Bst.nrinordertraverse (); /** two fork tree node definition */public class Node{private int value;private node left;private node right;public node () {}public Node (node lef T, 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;}}}


Java implementation Two-fork sorting tree Insert, find, delete

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.