BST (Binary Search Tree)

Source: Internet
Author: User

If you need to maintain a list of objects that is sorted and unique

& If you need to is able to quickly inserts and retrieve objects to and from the This list--The ideal data structure would be a tree set (or a tree map, if you consider each object a key and associate another Object called a value to it).implementation in Java:treeset<t>, Treemap<k, v>a binary tree is a BST iff, for every node n, in the tree:
    • All keys n ' s left subtree is less than the key in N, and
    • All keys in n ' s right subtree is greater than the key in N.
Insertion-o (log n)From Root all the "to" leaf, compare and decide which side to go. The new node is always a leaf node. Deletion-o (1)-O (log n)If N has no children, we are only having to remove N from the tree. If N has a single child, we remove N and connect its parent to its child.           If N has both children, we need to:find the smallest node that's larger than N, call it M.          Remove m from the tree, Replace the value of N with M. (Think:m always has no left child) Retrieval-o (log n)For BST (binary search trees), although the Average-caseTimes for theLookup,Insert, andDeleteMethods is all O (log N), where N is the number of nodes in the Tree,the worst-caseTime is O (N).We can guarantee O (log N) time for all three methods by using a Balanced tree -a tree tha t always have height O (log N)--instead of a binary search tree. Balanced tree- AVL tree, 2-4 tree, red-black tree and B trees "fully populated" means that every internal node have exactly the children, and all terminal nodes is at the Same depth.
1 classBST {2     Private classNode {3         intVal;4 Node left;5 Node right;6         7          PublicNode () {}8         9          PublicNode (intval) {Ten              This. val =Val; One         } A          -          Public voidcopy (Node N) { -              This. val =N.val; the              This. left =N.left; -              This. right =N.right; -         } -     } +      -      Public StaticNode Root; +      A     //Insert, O (lg N) at      Public voidInsertintval) { -Root =Insert (Root, Val); -     } -      -     PrivateNode Insert (node node,intval) { -         if(node = =NULL) { innode =NewNode (val); -             returnnode; to         } +          -         if(Node.val > val) node.left =Insert (Node.left, Val); the         if(Node.val < val) Node.right =Insert (Node.right, Val); *          $         returnnode;Panax Notoginseng     } -      the     //Search, O (lg N) +      PublicNode Search (intval) { A         returnSearch (Root, Val); the     } +      -     PrivateNode Search (node node,intval) { $         if(node = =NULL)return NULL; $          -         if(Node.val = = val)returnnode; -         Else if(Node.val > Val)returnSearch (Node.left, Val); the         Else returnSearch (Node.right, Val); -     }Wuyi      the     //Delete, O (1)-O (LG N) -      PublicNode Delete (intval) { WuRoot =Delete (root, Val); -         returnRoot; About     } $      -     PrivateNode Delete (node node,intval) { -         if(node = =NULL)return NULL; -         if(Node.val > val) node.left =Delete (Node.left, Val); A         Else if(Node.val < val) Node.right =Delete (Node.right, Val); +          the         Else { -Node del =NewNode (); $ del.copy (node); the          the             if(Node.left = =NULL) {node.copy (node.right); node.right =NULL;returnnode;} the             if(Node.right = =NULL) {node.copy (node.left); node.left =NULL;returnnode;} the          - node.copy (min (del.right)); inNode.right =deletemin (del.right); theNode.left =Del.left; the         } About         returnnode; the     } the      the     Privatenode min (node node) { +         if(node = =NULL|| Node.left = =NULL)returnnode; -         returnmin (node.left); the     }Bayi      the     //Remove the smallest node and return new root; the     Privatenode Deletemin (node node) { -         if(node = =NULL)return NULL; -         if(Node.left = =NULL) { the             returnNode.right;//node is deleted the         } theNode.left =deletemin (node.left); the         returnnode; -     } the}

BST (Binary Search Tree)

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.