Step-by-step write algorithm (hash binary tree)

Source: Internet
Author: User

Text: Step by step write algorithm (hash binary tree)

"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


Friends who have used a balanced binary tree know that the biggest advantage of a balanced binary tree is the sort. When data is inserted or when data is deleted, we have to consider the sort of data. However, as important as the addition and deletion of data, there is also data query. Unfortunately, a balanced binary tree often gets very low query efficiency due to the addition and deletion of nodes. Friends can take a look at an extreme scenario where all branch nodes have only one side of the data:

/**         7        /        * 6/*     5                  7*    /                    *   2                     12*  /                        * 1                         20*/

The above picture is a good illustration of the problem, although query 7, 6 is very convenient, but query 5, 2, 1 when the efficiency is very low, the right of the two fork tree is also the case. So there is no way to make the data between the search efficiency does not differ too much? Until now, there are three main methods:

(1) Hash binary tree

(2) AVL tree

(3) Red and black trees

What we are mainly explaining today is hash tree. The other two content will be described in the following blog post.

So what is a hash tree? In fact, it is also very simple, we add a next pointer in the binary tree node, while creating a hash table, so that we can query the data directly using hash query instead of the balanced binary tree query. In general, hash tree nodes should be defined like this:

typedef struct _HASH_TREE{INT data;struct _hash_tree* next;struct _hash_tree* left;struct _hash_tree* right;} Hash_tree;
In fact, compared to the more common balanced binary tree, which is more than a next pointer, then when the next pointer to deal with it? The main point is to add nodes and delete nodes when processing.

STATUS Add_node_into_tree (hash_tree** pphash, int data) {/* Add hash node into tree *//* Add hash node into HASH table */re Turn TRUE;}
The added code is similar to the deletion work.

STATUS Delete_node_from_tree (hash_tree** pphash, int data) {hash_tree* pnode;/* Delete HASH node from tree, and not free SP ace*//* Delete hash node from hash table */free (pnode); return TRUE;}

Description

(1) Hash binary tree idea is more important, students better understand why to build a hash two tree?

(2) The above code is not very complete, the hash table is not familiar with friends can refer to my writing this blog (hash table), binary tree Add delete unfamiliar friends can also refer to the other blog I write (add, delete 1, delete 2, delete 3), By combining the two parts of the code with the structure given above, we can basically implement a hash binary tree.




Step-by-step write algorithm (hash binary 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.