Algorithm-two binary search tree related operations and summary

Source: Internet
Author: User

One of the important reasons for the widespread use of binary search trees is that it maintains the ordering of keys, so it can be used as a basis for implementing many of the methods in the ordered symbol table API. This allows the use cases of the symbol table to access key-value pairs not only through the keys but also by the relative order of the keys. Below, we want to study the implementation of the various methods in the ordered symbol table API.

1. Maximum key and minimum key

If the left link of the root node is empty, then the smallest key in a binary lookup tree is the root node, and if the left link is not empty, the smallest key in the tree is the smallest key in the left dial hand tree. Simple loops can also be equivalent to this description, but we use recursion in order to maintain consistency. The way to find the maximum key is similar, just to find the right subtree.

2. Rounding up and rounding down

If the given key is less than two fork to find the root node of the tree, then the maximum key (floor) less than or equal to the key must be at the root node of the record; If a given key key is greater than the root node of the binary lookup tree, then only if a node of the root node right subtree is less than or equal to key The maximum key that is less than or equal to key appears in the right subtree, otherwise the root node is the maximum key that is less than or equal to key. This description illustrates the recursive implementation of the floor () method, and it also demonstrates that it can calculate the expected results. The ceiling () algorithm can be obtained by turning left to right (and less than to greater than). The calculation of the rounded up function is as shown.

3. Select action

The selection operation in the binary lookup tree is similar to the segmentation-based array selection we learned before. The subtree node counter variable n that we maintain in each node of the binary lookup tree is used to support this operation.

4.select () method

5. Delete the maximum key and delete the minimum key

The most difficult method to implement in a binary lookup tree is the Delete () method, which removes a key-value pair from the symbol table. As a warm-up exercise, we first consider the Deletemin () method (delete the key value pair corresponding to the minimum key), as shown, and put (), our recursive method accepts a link to the node and returns a link to the node. This allows us to easily change the structure of the tree and assign the returned link to the link as a parameter. For Deletemin (), we need to go deep into the left subtree of the root node until we meet an empty link, and then point the link to the right subtree of that node (just return its right link in the recursive call). There is no link at this point to the node to be deleted, so it will be cleaned up by the garbage collector. The standard recursive code we give will correctly set the link to its parent node after deleting the node and update its value to the counter of all nodes on the path to the root node. The implementation of the Deletemax () method is exactly the same as Deletemin ().

6. Delete operation

We can delete any node with only one sub-node (or no sub-node) in a similar way, but how do we delete a node with two sub-nodes? After deletion we have to process the two subtrees tree, but the parent node of the deleted node has only one empty link. The first method, after deleting the node x, fills its position with its subsequent nodes. Because X has a right child node, its successor is the smallest node in its right subtree. Such a substitution can still guarantee the order of the tree, because there is no other key between the keys of the X.key and its successor nodes. We were able to complete the task of replacing X with its successor node in 4 simple steps (as shown in the process):

0 Save the link to the node that will be deleted as t;

0 point X to its successor node min (t.right);

0 point the right link of X (a two-fork lookup tree that points to a tree with all nodes greater than X.key) to Deletemin (T.right), that is, all nodes are still larger than the X.key sub-binary lookup tree after the deletion.

0 Set the left link of X (this is empty) to T.left (all keys below it are smaller than the node being deleted and its successor).

7. Scope Lookup

To ensure that all keys within the specified range in the subtree with the given node are joined to the queue, we will (recursively) look for the left subtree of the root node, look for the root node, and then (recursively) find the right subtree of the root node.

8. Performance analysis

How efficient are the operations associated with ordering in binary search trees? To study this problem, we first need to know the height of the tree (that is, the maximum depth of any node in the tree). Given a tree, the height of the tree determines the performance of all operations in the worst case scenario (except for range lookups, since it is proportional to the extra cost and the number of keys returned).

proposition: in a binary lookup tree, all operations in the worst case time are proportional to the height of the tree.

Proof: All operations of the tree travel along one or two paths of the tree. By definition, the length of the path cannot be greater than the height of the tree.

9. Summary

In some scenarios, the poor performance of binary search trees in worst-case scenarios is still unacceptable. The good performance of the binary lookup tree's basic implementation relies on the distribution of the keys in it to be random enough to eliminate the long path. For fast sorting, we can break the array first, and for the API of the symbol table, we can't do it, because the use case of the symbol table controls the order of the various operations.

Algorithm-two binary search tree related operations and summary

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.