Data Structure in algorithm Learning

Source: Internet
Author: User

First, we will introduce some concepts and properties of the Binary Search Tree.
The time when the binary tree performs basic operations is proportional to the height of the tree.
Set x to a node in the binary search tree. If y is a node in the left subtree of x, key [y] <= key [x]; if y is a node in the right subtree of x, key [y]> = key [x].
Note that this property indicates that all nodes in the left subtree of the root node of the binary search tree are smaller than the root node, and all nodes in the right subtree are greater than the root node. Therefore, based on this nature, you can use the central order to access the binary lookup count from small to large.
Operations on the binary search tree:
SEARCH: SEARCH for nodes with the same keyword as the key
MINIMUM: Find the node with the smallest keyword
MAXIMUM: Find the node with the largest keyword
SUCCESSOR: Find the next y of node x
INSERT: INSERT node z in the binary search tree
DELETE: DELETE node z in the binary search tree
The INSERT and DELETE operations are troublesome.
2. Use pseudo code to perform the above operations.
Sequential Traversal
[Plain]
Inorder-tree-walk (x)
If x! = Null
Then inorder-tree-walk (left [x])
Print (x)
Inorder-tree-walk (right [x])


Search
[Plain]
Tree-search (x, k)
If x = null or k = key [x]
Then return x;
If k <key [x]
Then return tree-search (left [x], k)
Else return tree-search (right [x], k)
Time is the height of the tree h = logn
The while version does not require recursion.
While x! = Null and k! = Key [x]
Do if (k <key [x])
Then x = left [x]
Else x = right [x]
Return x


Pointer to the smallest element (left node of the Left subtree)
[Plain]
Tree-minimum (x)
While left [x]! = Null
Do x = left [x]
Return x


Successor
[Plain]
Tree-successor (x)
1 if right [x]! = Null // If x has the right subtree, find the minimum value in the right subtree.
2 then return tree-minimum (right [x])
3 y = parent [x]
4 while y! = Null and x = right [y] // if there is no right subtree, its successor y is the first right subtree of x's parent node.
5 do x = y
6 y = parent [y]
7 return y

Based on its successor features, the successor of node x is the node with the smallest keyword in key [x.
1st ~ In the two rows, the right subtree of x has more nodes than the key [x]. Therefore, if x has the right subtree, find the minimum value in the right subtree.
3rd ~ Six rows. If there is no right subtree, its successor y is the first right subtree of x's parent node (based on the subsequent features: the successor of node x is the node with the smallest keyword in key [x. Because x does not have the right subtree, the next node of x that is traversed in the middle order is the successor. It should be the root node y of such a subtree, And the ancestor of x is its left child, in this way, y is greater than all the nodes in the left subtree, and x is the largest node in the left subtree of y ).

Insert:
Insert the z node into the count T of the binary query. The steps are as follows:
1. compare the key [z] From the root node x and use y to record the parent node of x until a leaf node at the last layer is compared. Then, y points to a leaf node, x is NULL.
2. If y is NULL, it indicates that it is an empty tree, and the root node is z.
3. otherwise, if key [z] is smaller than key [y], left [y] = z; when key [z] is greater than or equal to key [y], right [y] = z.
The Code is as follows:
[Plain]
Tree-insert (T, z)
Y = null
X = root [T]
While x! = Null
Do y = x
If (key [x] <key [z])
Then x = right [x]
Else x = left [x]
Parent [z] = y
If (y = null) // The tree is empty.
Then root [T] = z
Else if key [z] <key [y]
Then left [y] = z
Else right [y] = z


Delete:
There are three scenarios:
1. No left or right subtree. This is to delete z directly, and set the parent node parent [z] of z to NULL, pointing to the Child tree of z. ,

2. Only the left subtree or the right subtree is supported. This is to connect the subtree of z to its parent node. Just delete z.

3. There are both left and right subtree. This is to first use the succeor method to find the successor y of the node z to be deleted, because y must not have the left subtree (Because y is the successor of z, refer to the previous definition of the successor, y is the smallest node in the right subtree of z. If y has a left subtree, the node in the left subtree of y must be smaller than y !), Therefore, you can delete y and make y's parent node the father node of y's right subtree (similar to the case in 2nd), and replace z's key with y's key.

The specific pseudocode is as follows:
[Plain]
Tree-delete (T, z)
If left [z] = null or right [z] = null // if there is no child node or right child node, and there is one child tree
Then y = z
Else y = tree-successor (z) // if both left and right children exist, find the next node of the child.
If left [y]! = Null
Then x = left [y]
Else x = right [y]
If x! = Null // if there is only one child node
Then parent [x] = parent [y] // connect the child tree x of z with its parent node and delete z.
If parent [y] = null // if y does not have a Father's Day, it indicates it is the root node.
Then root [T] = x // set the child node to Father's Day.
Else if y = left [parent [y] // if y is the left subtree of its father's day, you can delete y, and let y's father node become the father node of y's right subtree.
Then left [parent [y] = x
Else right [parent [y] = x
If y! = Z
Then key [z] = key [y] // Replace the key of z with the key of y.
Return y

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.