Data structure, data structure and Algorithm

Source: Internet
Author: User

Data structure, data structure and Algorithm
Dynamic search

When a query table is stored in an ordered Storage Structure and needs to be ordered, if you insert, delete, or sort a query table, you must move a large number of records. When there are many records, this kind of movement costs a lot.
If the query table is unordered, you do not need to move a large number of records to insert or delete the table, but it is not conducive to the query.
You can search tables dynamically and efficiently by using the tree.

Binary sorting tree

Binary Sort Tree or Binary Search Tree is defined as a Binary Tree or a Binary Tree that meets the following requirements.
(1): If the left subtree is not empty, the value (keyword) of all nodes on the left subtree is smaller than the value of the root node;
(2): If the right subtree is not empty, the value (keyword) of all nodes on the right subtree is greater than the value of the root node;
(3): Left and Right decision trees are binary decision trees.
Conclusion: If a binary sorting tree is traversed in the middle order, the resulting node sequence is an incremental sequence.

BST tree search
1. Search idea ① if the root node is empty, the search fails. ② If the root node is not empty, compare the given K value with the keywords of the root node of the binary sorting tree if they are equal: the search is successful. ③ the keyword of the given root node whose K value is less than BST: continue to search on the left subtree of the node; ④ the keyword of the given root node whose K value is greater than BST: continue searching on the right subtree of the node.
2 Algorithm Implementation BSTNode * BST_Serach (BSTNode * T, KeyType key) {if (T = NULL) return (NULL); else {if (T-> key = key )) return (T); else if (key <T-> key) return (BST_Serach (T-> Lchild, key); else return (BST_Serach (T-> Rchild, key ));}}
Insert a BST tree
The Insert Process is similar to the search process, but when the search fails, the new node (the value of the keyword) is added based on the size of the keyword and the root node) insert the location of the corresponding child to the root node (left or right ). to insert a new node into the BST tree, ensure that the nature of the BST is still satisfied after insertion.

1. Insert an idea
When a new node x is inserted in the BST tree, if the BST tree is empty, the new node x is the root node of the inserted BST tree; otherwise, compare the keyword of node x with the keyword of root node T:
① If equal: No need to insert;
② If x. keykey: node x is inserted into the left subtree of T;
③ If x. key> T-> key: node x is inserted into the right subtree of T.

Algorithm Implementation (1) recursive algorithm (2) Non-recursive algorithm void Insert_BST (BSTNode * T, KeyType key) {BSTNode * x, * p, * q; x = (BSTNode *) malloc (sizeof (BSTNode); X-> key = key; x-> Lchild = x-> Rchild = NULL; if (T = NULL) T = x; else {p = T; while (p! = NULL) {if (p-> key = x-> key) return; q = p; /* q is the parent node of p */if (x-> key <p-> key) p = p-> Lchild; else p = p-> Rchild ;} if (x-> key <q-> key) q-> Lchild = x; else q-> Rchild = x ;}}

An unordered sequence can be changed to an ordered sequence by constructing a BST tree.
According to the algorithm, the new node inserted each time is the leaf node of the BST tree. That is, you do not have to move other nodes during the insertion. You only need to modify the pointer of a node.
With the BST Tree insertion Operation, each node can be inserted one by one from the empty tree to create a BST tree.

Binary sorting tree Performance

The number of times the binary sorting tree compares the search keywords, which is equal to the number of layers where the node is located (the search is successful). If the search fails, the maximum number of comparisons is the depth of the tree.
For a tree with n nodes, its depth is between 2n + 1 and n.
The shape of the binary sorting tree is crucial to the search efficiency. In other words, a binary sorting tree does not necessarily increase the search speed, but depends on the shape of the tree.

Balanced binary sorting tree

If a tree with relatively balanced left and right Subtrees can be constructed, the depth of the tree will be relatively small, which will reflect the good nature of the binary tree, the best efficiency can be achieved when searching, which is to balance the binary sorting tree.
The Balanced Binary Tree or Height-Balanced Tree was proposed by Russian mathematicians Adelson-Velskii and Landis in 1962, also known as AVL trees.

Balanced binary tree Definition

A balanced binary tree, an empty tree, or a binary tree that meets the following requirements.
(1) the absolute value of the depth difference between the left and right subtree is not greater than 1;
(2) The left and right subtree are both balanced binary trees.
Balance Factor: the depth of the Left subtree of the node on the binary tree minus the depth of the right subtree.
The equilibrium factors of each node on a balanced binary tree can only be-1, 0, and 1. If the absolute value of the equilibrium factor of one node is greater than 1, the binary tree is not a balanced binary tree.

If a Binary Tree is both a Binary and a Balanced Binary Tree, it is called a Balanced Binary Sort Tree ).
The node type is defined as follows:

Typedef struct BNode {KeyType key;/* keyword field */int Bfactor;/* balanced factor field */... /* Other data domains */struct BNode * Lchild, * Rchild;} BSTNode;

The search process on the balanced binary sorting tree is exactly the same as that on the binary sorting tree. That is, when the search is performed on the AVL Tree, the number of times compared with the given K value does not exceed the depth of the tree.

What is the maximum depth of an AVL Tree with n nodes?

Meaning the maximum depth of a balanced binary tree with n nodes is O (2n)

Average search length and
㏒ 2n is the same order of magnitude, and the average time complexity is O (㏒ 2n ).

Constructing a balanced binary sorting tree

Basic Ideas
Each time a knot is inserted (Deleted,
First, check whether the balance is broken due to insertion,
If yes, find the minimum unbalanced subtree. Adjust the link between nodes in the minimum unbalanced subtree while maintaining the binary sorting tree feature, and rotate accordingly, make it a new balance subtree.

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.