Binary search tree-An introduction to Algorithms (14)

Source: Internet
Author: User

1. What is a two-fork search tree

As the name implies, binary search tree is organized by a binary search tree. For example, such a tree can be represented by a linked list of data structures, where each node is an object. In addition to key and satellite data, each node also contains attributes left (child), right (child), and P (parent) (nil if not present).

The keywords in the binary search tree are always stored in a way that satisfies the nature of the two-fork search tree :

Set X is a node of a two-fork search tree. If Y is a node in the X left dial hand tree, then Y.key≤x.key. If Y is a node in the X right subtree, then Y.key≥x.key.

Binary search tree Nature allows us to use a simple ghost algorithm to output all the keywords in a binary search tree in a certain order. We used to have the ordinal traversal (the key position of the output subtree is between the left subtree keyword and the right subtree keyword), the middle sequence traversal, the post-order traversal .

The recursive algorithm for the first order traversal is given below:

We can prove that traversing a two-fork search tree with n nodes takes a time of θ (n) (proves slightly).

2. Query Binary search tree

(1)

In this section, we discuss binary search trees such as: search,minimum,maximun,successor,perdecessor operations.

(2) Search

We use one of the following algorithms to query a binary search tree. This method requires a pointer to the root node x and the keyword k to be searched, and the output to a pointer to a node with the keyword K, if present. otherwise output nil).

It is easy to know that the time of the query algorithm is O (h), where H is the height of the tree.

Better, we can replace recursion with the following iterations, because recursion can cause stack memory to overflow, and on most computers, iterative versions are more efficient.

(3) Minimum and maximum

Because of the nature of the binary search tree, we can easily find the largest and smallest key elements in the tree.

Similarly, both of the above methods can be completed in O (h) time.

(4) successor and predecessor

Given a node of a binary search tree, sometimes we need to find the precursor and rear drive of the node in the order of the sequence traversal. As in, it is easy to see that the key is 4 of the node's predecessor is a key 3 node, the rear drive is a key 6 of the node, the key is 7 of the node's predecessor is a key 6 node, the rear drive is a key 9 node.

The following is an algorithm for finding the post-drive of a node:

Explain the process of the above-seeking drive. We discuss in two different situations:

① if the right subtree of X is not nil, then the successor of X is the element with the smallest key in the right child tree.

② if the right subtree of x is nil, then the successor of X will be generated in its ancestor node (including the parent node). And the ancestor node must be from bottom to top, for the first time to satisfy itself as the right node of its parent node.

In the same vein, we can find the precursor node of x, and we don't give an algorithm here.

It is easy to see that the time required for them is O (h).

Thus, we conclude that the set operation Search,minimum,maximun,successor,perdecessor can be completed within O (h) time on a two-fork search tree with a height of H.

3. Inserting and deleting

(1) Insert

The insert operation is relatively straightforward compared to the delete operation. The insert algorithm is given below. The function of this algorithm is to insert a new node Z into a binary search tree T.

The algorithm is simple and does not explain here.

As you can see, the time to insert an operation is O (h).

(2) Delete

We discuss the following three scenarios in terms of the number of child nodes of the node being deleted:

① is removed from the node without children. Just modify the parent node and replace it with nil.

② deleted node has a child. You only need to modify the parent node to replace yourself with the child.

③ deleted node has two children. Then find the successor Y of Z (must be in the right subtree of z) and let y occupy the position of z in the tree. The original right subtree of Z is called the New Right sub-tree of Y, and the left subtree of Z becomes the left subtree of y.

The first two cases are relatively simple, and for the third case, we can also subdivide:

① if Z's successor Y is the right child of Z (that is, Y has no left child), use Y instead of Z, and leave the right subtree of y as shown:

② if the successor Y of Z is not the right child of Z, first replace y with the right child of Y, and then replace Z with Y. As shown in the following:

In order to implement the above process, we first implement the transplant process. This process enables the subtree V to be substituted for the subtree μ. The implementation algorithm is as follows:

Using transplant, we implement the delete operation according to the above discussion:

Note that the above-mentioned deletion procedure does not give us the situation in the above discussion where the situation ① Z has no children. In fact, the situation in the above discussion ① has been included in the situation ②, that is, Z has no children is equivalent to Z has a key nil left child or have a key nil right child.

Analysis of the algorithm, we found that in addition to transplant, other operations have spent constant time. Therefore, the delete operation will take an O (h) time.

Combined with all of the above analysis, we know that each of the basic operations on the two-fork search tree can be completed in O (h) time (where h is the height of the tree).

4. Randomly build binary search tree

We first give the definition of a randomly constructed binary search tree: A tree that is randomly inserted into an empty tree by inserting n keywords. The random meaning here is that the n! of the N keyword can appear.

We want to prove the following theorem:

The desired height of a randomly constructed binary search tree with n different keywords is O (lgn).

Define three random variable xn,yn,rn, where xn represents the height of a randomly constructed binary search tree with n different keywords; yn=2^xn represents the exponential height of the binary search tree (exponential height). RN indicates the rank (rank) of the keyword in the set of n keywords when it is selected as a root in n different keywords (that is, RN denotes the position that the keyword should occupy when the keyword is sorted). So if rn = I, then the left subtree of the root has i-1 elements, the right subtree has n-i elements, at this point:

Yn = 2 · Max (Yi-1, Yn-i)

We then define an indicator random variable zn,i,zn,i = I{RN = i}. Because RN is possible for any element in the collection {1,2,...,n}, there are:

P{rn = i} = 1/n, (i=1,2,...,n)

E (zn,i) = 1/n

Since zn,i is only equal to 1 or 0,yn = 2 · Max (Yi-1, yn-i) =

So there are:

Y[0],y[1],...,y[n-1] will appear two times (e[yi-1] and e[yn-i] in the last formula, so:

The above formula is a recursive formula that we can guess, and mathematically inductive to prove (here slightly).

With Jensen inequalities, we can get further

So

The two sides go to the logarithm, eventually:

Binary search tree-An introduction to Algorithms (14)

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.