Binary search Tree

Source: Internet
Author: User

Binary search Tree

Also called a binary sort tree, it is either an empty tree or a two-tree with the following properties: If its left subtree is not empty, then the value of all nodes on the left subtree is less than the value of its parent node, if its right tree is not empty, then the value of all nodes on the right subtree is greater than the value of its parent node, and its left and right sub-leaves are two-fork sorting tree. Binary sorting tree can be efficiently implemented to find, insert, delete. What is more complicated is how the child nodes are scheduled after the delete operation.

Inserting nodes

    • If the current binary tree is empty, the inserted element is the root node
    • If the inserted element is small, the root node is inserted in the left subtree, and if greater than the heel node is inserted in the right subtree, recursive execution of this procedure is known to find the correct location to insert the element

There are several cases of deleting nodes:

    • Leaf node: Simply point the pointer to the node to null
    • Node with only one child node: Simply point the pointer to the node's child node to
    • Node with two child nodes: find the precursor node in the left subtree, such as Node A, swap the value of a with the deleted node, delete the A node

Two concepts:

Precursor node: The largest node in the left Dial hand tree

Successor node: the smallest node of the right sub-tree

Source CodeNamespace bst{ Public classNode { Public intValue; PublicNode left; PublicNode right; } Public classBstree {PrivateNode Root; Public voidInsert (intx) {Node v =NewNode () {Value = x};if(Root = =NULL) {root = v;return; } Node Prenode = Postoinsert (x);if(Prenode.value > x)            {prenode.left = v; }Else{prenode.right = v; }        }//return decessor node, the biggest node in left subtree of x        PrivateNode predecessor (node X) {if(x = =NULL|| X.left = =NULL)            {return NULL; } node node = x.left; while(node. Right! =NULL) {node = node.            right; }returnNode }PrivateNode ParentNode (node node) {returnParentNode (node.        Value); }PrivateNode ParentNode (intx) {node node = root; Node parent = node; while(Node! =NULL)            {if(node. Value = = x) {returnParent } parent = node;if(node. Value > x) {node = node.                Left; }Else{node = node.                right; }            }Throw NewException (String. Format ("{0} has no parent", x)); }//return A node, should insert x as a child to it        PrivateNode Postoinsert (intx) {node node = root; Node Postoinsert =NULL; while(Node! =NULL) {Postoinsert = node;if(node. Value = = x) {Throw NewException (String. Format ("{0} already exist in tree", x)); }Else if(node. Value > x) {node = node.                Left; }Else{node = node.                right; }            }returnPostoinsert; } PublicNode Search (intx) {returnSearch (root, x); } PublicNode Search (node node,intx) {if(node = =NULL)            {return NULL; }if(node. Value = = x) {returnNode }Else if(node. Value > x) {returnSearch (node.            Left, x); }Else{returnSearch (node.            right, x); }        } Public voidRemove (intx) {Node node=search (x);if(node = =NULL)            {Throw NewException (String. Format ("{0} not exist in tree", x)); }if(node. left = =NULL&& node. right = =NULL) {node =NULL; }Else if(node. Left! =NULL&& node. Right! =NULL) {Node Prenode = predecessor (node);                Node parentnode = parentnode (Prenode); Swap (ref prenode.value,ref node. Value);if(Object.referenceequals (Prenode, Parentnode.left)) {Parentnode.left =NULL; }Else{parentnode.right =NULL; }            }Else if(node. Left! =NULL) {Node parentnode = parentnode (node);if(Object.referenceequals (node, parentnode.left)) {parentnode.left = node.                Left; }Else{parentnode.right = node.                Left; }                            }Else if(node. Right! =NULL) {Node parentnode = parentnode (node);if(Object.referenceequals (node, parentnode.left)) {parentnode.left = node.                right; }Else{parentnode.right = node.                right; }            }        } PublicOverride string ToString () {returnTree (root); }PrivateString Tree (node node) {if(node = =NULL)            {returnString.            Empty; The string left = Tree (node.            left); string right = Tree (node. right);if(!string. IsNullOrEmpty (left) | | !string. IsNullOrEmpty (right)) {returnString. Format ("{0} ({1},{2})", node. Value, Tree (node. left), Tree (node.            right)); }Else{returnNode.            Value.tostring (); }        } Public Static voidSwap (refintX, refintY) {inttemp = x;            x = y;        y = temp; }    }}

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.